1 // types.GenericAlias -- used to represent e.g. list[int].
2
3 #include "Python.h"
4 #include "pycore_object.h"
5 #include "pycore_unionobject.h" // _Py_union_type_or, _PyGenericAlias_Check
6 #include "structmember.h" // PyMemberDef
7
8 typedef struct {
9 PyObject_HEAD
10 PyObject *origin;
11 PyObject *args;
12 PyObject *parameters;
13 PyObject* weakreflist;
14 } gaobject;
15
16 static void
ga_dealloc(PyObject * self)17 ga_dealloc(PyObject *self)
18 {
19 gaobject *alias = (gaobject *)self;
20
21 _PyObject_GC_UNTRACK(self);
22 if (alias->weakreflist != NULL) {
23 PyObject_ClearWeakRefs((PyObject *)alias);
24 }
25 Py_XDECREF(alias->origin);
26 Py_XDECREF(alias->args);
27 Py_XDECREF(alias->parameters);
28 Py_TYPE(self)->tp_free(self);
29 }
30
31 static int
ga_traverse(PyObject * self,visitproc visit,void * arg)32 ga_traverse(PyObject *self, visitproc visit, void *arg)
33 {
34 gaobject *alias = (gaobject *)self;
35 Py_VISIT(alias->origin);
36 Py_VISIT(alias->args);
37 Py_VISIT(alias->parameters);
38 return 0;
39 }
40
41 static int
ga_repr_item(_PyUnicodeWriter * writer,PyObject * p)42 ga_repr_item(_PyUnicodeWriter *writer, PyObject *p)
43 {
44 _Py_IDENTIFIER(__module__);
45 _Py_IDENTIFIER(__qualname__);
46 _Py_IDENTIFIER(__origin__);
47 _Py_IDENTIFIER(__args__);
48 PyObject *qualname = NULL;
49 PyObject *module = NULL;
50 PyObject *r = NULL;
51 PyObject *tmp;
52 int err;
53
54 if (p == Py_Ellipsis) {
55 // The Ellipsis object
56 r = PyUnicode_FromString("...");
57 goto done;
58 }
59
60 if (_PyObject_LookupAttrId(p, &PyId___origin__, &tmp) < 0) {
61 goto done;
62 }
63 if (tmp != NULL) {
64 Py_DECREF(tmp);
65 if (_PyObject_LookupAttrId(p, &PyId___args__, &tmp) < 0) {
66 goto done;
67 }
68 if (tmp != NULL) {
69 Py_DECREF(tmp);
70 // It looks like a GenericAlias
71 goto use_repr;
72 }
73 }
74
75 if (_PyObject_LookupAttrId(p, &PyId___qualname__, &qualname) < 0) {
76 goto done;
77 }
78 if (qualname == NULL) {
79 goto use_repr;
80 }
81 if (_PyObject_LookupAttrId(p, &PyId___module__, &module) < 0) {
82 goto done;
83 }
84 if (module == NULL || module == Py_None) {
85 goto use_repr;
86 }
87
88 // Looks like a class
89 if (PyUnicode_Check(module) &&
90 _PyUnicode_EqualToASCIIString(module, "builtins"))
91 {
92 // builtins don't need a module name
93 r = PyObject_Str(qualname);
94 goto done;
95 }
96 else {
97 r = PyUnicode_FromFormat("%S.%S", module, qualname);
98 goto done;
99 }
100
101 use_repr:
102 r = PyObject_Repr(p);
103
104 done:
105 Py_XDECREF(qualname);
106 Py_XDECREF(module);
107 if (r == NULL) {
108 // error if any of the above PyObject_Repr/PyUnicode_From* fail
109 err = -1;
110 }
111 else {
112 err = _PyUnicodeWriter_WriteStr(writer, r);
113 Py_DECREF(r);
114 }
115 return err;
116 }
117
118 static PyObject *
ga_repr(PyObject * self)119 ga_repr(PyObject *self)
120 {
121 gaobject *alias = (gaobject *)self;
122 Py_ssize_t len = PyTuple_GET_SIZE(alias->args);
123
124 _PyUnicodeWriter writer;
125 _PyUnicodeWriter_Init(&writer);
126
127 if (ga_repr_item(&writer, alias->origin) < 0) {
128 goto error;
129 }
130 if (_PyUnicodeWriter_WriteASCIIString(&writer, "[", 1) < 0) {
131 goto error;
132 }
133 for (Py_ssize_t i = 0; i < len; i++) {
134 if (i > 0) {
135 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) {
136 goto error;
137 }
138 }
139 PyObject *p = PyTuple_GET_ITEM(alias->args, i);
140 if (ga_repr_item(&writer, p) < 0) {
141 goto error;
142 }
143 }
144 if (len == 0) {
145 // for something like tuple[()] we should print a "()"
146 if (_PyUnicodeWriter_WriteASCIIString(&writer, "()", 2) < 0) {
147 goto error;
148 }
149 }
150 if (_PyUnicodeWriter_WriteASCIIString(&writer, "]", 1) < 0) {
151 goto error;
152 }
153 return _PyUnicodeWriter_Finish(&writer);
154 error:
155 _PyUnicodeWriter_Dealloc(&writer);
156 return NULL;
157 }
158
159 // isinstance(obj, TypeVar) without importing typing.py.
160 // Returns -1 for errors.
161 static int
is_typevar(PyObject * obj)162 is_typevar(PyObject *obj)
163 {
164 PyTypeObject *type = Py_TYPE(obj);
165 if (strcmp(type->tp_name, "TypeVar") != 0) {
166 return 0;
167 }
168 PyObject *module = PyObject_GetAttrString((PyObject *)type, "__module__");
169 if (module == NULL) {
170 return -1;
171 }
172 int res = PyUnicode_Check(module)
173 && _PyUnicode_EqualToASCIIString(module, "typing");
174 Py_DECREF(module);
175 return res;
176 }
177
178 // Index of item in self[:len], or -1 if not found (self is a tuple)
179 static Py_ssize_t
tuple_index(PyObject * self,Py_ssize_t len,PyObject * item)180 tuple_index(PyObject *self, Py_ssize_t len, PyObject *item)
181 {
182 for (Py_ssize_t i = 0; i < len; i++) {
183 if (PyTuple_GET_ITEM(self, i) == item) {
184 return i;
185 }
186 }
187 return -1;
188 }
189
190 static int
tuple_add(PyObject * self,Py_ssize_t len,PyObject * item)191 tuple_add(PyObject *self, Py_ssize_t len, PyObject *item)
192 {
193 if (tuple_index(self, len, item) < 0) {
194 Py_INCREF(item);
195 PyTuple_SET_ITEM(self, len, item);
196 return 1;
197 }
198 return 0;
199 }
200
201 PyObject *
_Py_make_parameters(PyObject * args)202 _Py_make_parameters(PyObject *args)
203 {
204 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
205 Py_ssize_t len = nargs;
206 PyObject *parameters = PyTuple_New(len);
207 if (parameters == NULL)
208 return NULL;
209 Py_ssize_t iparam = 0;
210 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
211 PyObject *t = PyTuple_GET_ITEM(args, iarg);
212 int typevar = is_typevar(t);
213 if (typevar < 0) {
214 Py_DECREF(parameters);
215 return NULL;
216 }
217 if (typevar) {
218 iparam += tuple_add(parameters, iparam, t);
219 }
220 else {
221 _Py_IDENTIFIER(__parameters__);
222 PyObject *subparams;
223 if (_PyObject_LookupAttrId(t, &PyId___parameters__, &subparams) < 0) {
224 Py_DECREF(parameters);
225 return NULL;
226 }
227 if (subparams && PyTuple_Check(subparams)) {
228 Py_ssize_t len2 = PyTuple_GET_SIZE(subparams);
229 Py_ssize_t needed = len2 - 1 - (iarg - iparam);
230 if (needed > 0) {
231 len += needed;
232 if (_PyTuple_Resize(¶meters, len) < 0) {
233 Py_DECREF(subparams);
234 Py_DECREF(parameters);
235 return NULL;
236 }
237 }
238 for (Py_ssize_t j = 0; j < len2; j++) {
239 PyObject *t2 = PyTuple_GET_ITEM(subparams, j);
240 iparam += tuple_add(parameters, iparam, t2);
241 }
242 }
243 Py_XDECREF(subparams);
244 }
245 }
246 if (iparam < len) {
247 if (_PyTuple_Resize(¶meters, iparam) < 0) {
248 Py_XDECREF(parameters);
249 return NULL;
250 }
251 }
252 return parameters;
253 }
254
255 /* If obj is a generic alias, substitute type variables params
256 with substitutions argitems. For example, if obj is list[T],
257 params is (T, S), and argitems is (str, int), return list[str].
258 If obj doesn't have a __parameters__ attribute or that's not
259 a non-empty tuple, return a new reference to obj. */
260 static PyObject *
subs_tvars(PyObject * obj,PyObject * params,PyObject ** argitems)261 subs_tvars(PyObject *obj, PyObject *params, PyObject **argitems)
262 {
263 _Py_IDENTIFIER(__parameters__);
264 PyObject *subparams;
265 if (_PyObject_LookupAttrId(obj, &PyId___parameters__, &subparams) < 0) {
266 return NULL;
267 }
268 if (subparams && PyTuple_Check(subparams) && PyTuple_GET_SIZE(subparams)) {
269 Py_ssize_t nparams = PyTuple_GET_SIZE(params);
270 Py_ssize_t nsubargs = PyTuple_GET_SIZE(subparams);
271 PyObject *subargs = PyTuple_New(nsubargs);
272 if (subargs == NULL) {
273 Py_DECREF(subparams);
274 return NULL;
275 }
276 for (Py_ssize_t i = 0; i < nsubargs; ++i) {
277 PyObject *arg = PyTuple_GET_ITEM(subparams, i);
278 Py_ssize_t iparam = tuple_index(params, nparams, arg);
279 if (iparam >= 0) {
280 arg = argitems[iparam];
281 }
282 Py_INCREF(arg);
283 PyTuple_SET_ITEM(subargs, i, arg);
284 }
285
286 obj = PyObject_GetItem(obj, subargs);
287
288 Py_DECREF(subargs);
289 }
290 else {
291 Py_INCREF(obj);
292 }
293 Py_XDECREF(subparams);
294 return obj;
295 }
296
297 PyObject *
_Py_subs_parameters(PyObject * self,PyObject * args,PyObject * parameters,PyObject * item)298 _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObject *item)
299 {
300 Py_ssize_t nparams = PyTuple_GET_SIZE(parameters);
301 if (nparams == 0) {
302 return PyErr_Format(PyExc_TypeError,
303 "There are no type variables left in %R",
304 self);
305 }
306 int is_tuple = PyTuple_Check(item);
307 Py_ssize_t nitems = is_tuple ? PyTuple_GET_SIZE(item) : 1;
308 PyObject **argitems = is_tuple ? &PyTuple_GET_ITEM(item, 0) : &item;
309 if (nitems != nparams) {
310 return PyErr_Format(PyExc_TypeError,
311 "Too %s arguments for %R",
312 nitems > nparams ? "many" : "few",
313 self);
314 }
315 /* Replace all type variables (specified by parameters)
316 with corresponding values specified by argitems.
317 t = list[T]; t[int] -> newargs = [int]
318 t = dict[str, T]; t[int] -> newargs = [str, int]
319 t = dict[T, list[S]]; t[str, int] -> newargs = [str, list[int]]
320 */
321 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
322 PyObject *newargs = PyTuple_New(nargs);
323 if (newargs == NULL) {
324 return NULL;
325 }
326 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
327 PyObject *arg = PyTuple_GET_ITEM(args, iarg);
328 int typevar = is_typevar(arg);
329 if (typevar < 0) {
330 Py_DECREF(newargs);
331 return NULL;
332 }
333 if (typevar) {
334 Py_ssize_t iparam = tuple_index(parameters, nparams, arg);
335 assert(iparam >= 0);
336 arg = argitems[iparam];
337 Py_INCREF(arg);
338 }
339 else {
340 arg = subs_tvars(arg, parameters, argitems);
341 if (arg == NULL) {
342 Py_DECREF(newargs);
343 return NULL;
344 }
345 }
346 PyTuple_SET_ITEM(newargs, iarg, arg);
347 }
348
349 return newargs;
350 }
351
352 static PyObject *
ga_getitem(PyObject * self,PyObject * item)353 ga_getitem(PyObject *self, PyObject *item)
354 {
355 gaobject *alias = (gaobject *)self;
356 // Populate __parameters__ if needed.
357 if (alias->parameters == NULL) {
358 alias->parameters = _Py_make_parameters(alias->args);
359 if (alias->parameters == NULL) {
360 return NULL;
361 }
362 }
363
364 PyObject *newargs = _Py_subs_parameters(self, alias->args, alias->parameters, item);
365 if (newargs == NULL) {
366 return NULL;
367 }
368
369 PyObject *res = Py_GenericAlias(alias->origin, newargs);
370
371 Py_DECREF(newargs);
372 return res;
373 }
374
375 static PyMappingMethods ga_as_mapping = {
376 .mp_subscript = ga_getitem,
377 };
378
379 static Py_hash_t
ga_hash(PyObject * self)380 ga_hash(PyObject *self)
381 {
382 gaobject *alias = (gaobject *)self;
383 // TODO: Hash in the hash for the origin
384 Py_hash_t h0 = PyObject_Hash(alias->origin);
385 if (h0 == -1) {
386 return -1;
387 }
388 Py_hash_t h1 = PyObject_Hash(alias->args);
389 if (h1 == -1) {
390 return -1;
391 }
392 return h0 ^ h1;
393 }
394
395 static PyObject *
ga_call(PyObject * self,PyObject * args,PyObject * kwds)396 ga_call(PyObject *self, PyObject *args, PyObject *kwds)
397 {
398 gaobject *alias = (gaobject *)self;
399 PyObject *obj = PyObject_Call(alias->origin, args, kwds);
400 if (obj != NULL) {
401 if (PyObject_SetAttrString(obj, "__orig_class__", self) < 0) {
402 if (!PyErr_ExceptionMatches(PyExc_AttributeError) &&
403 !PyErr_ExceptionMatches(PyExc_TypeError))
404 {
405 Py_DECREF(obj);
406 return NULL;
407 }
408 PyErr_Clear();
409 }
410 }
411 return obj;
412 }
413
414 static const char* const attr_exceptions[] = {
415 "__origin__",
416 "__args__",
417 "__parameters__",
418 "__mro_entries__",
419 "__reduce_ex__", // needed so we don't look up object.__reduce_ex__
420 "__reduce__",
421 "__copy__",
422 "__deepcopy__",
423 NULL,
424 };
425
426 static PyObject *
ga_getattro(PyObject * self,PyObject * name)427 ga_getattro(PyObject *self, PyObject *name)
428 {
429 gaobject *alias = (gaobject *)self;
430 if (PyUnicode_Check(name)) {
431 for (const char * const *p = attr_exceptions; ; p++) {
432 if (*p == NULL) {
433 return PyObject_GetAttr(alias->origin, name);
434 }
435 if (_PyUnicode_EqualToASCIIString(name, *p)) {
436 break;
437 }
438 }
439 }
440 return PyObject_GenericGetAttr(self, name);
441 }
442
443 static PyObject *
ga_richcompare(PyObject * a,PyObject * b,int op)444 ga_richcompare(PyObject *a, PyObject *b, int op)
445 {
446 if (!_PyGenericAlias_Check(b) ||
447 (op != Py_EQ && op != Py_NE))
448 {
449 Py_RETURN_NOTIMPLEMENTED;
450 }
451
452 if (op == Py_NE) {
453 PyObject *eq = ga_richcompare(a, b, Py_EQ);
454 if (eq == NULL)
455 return NULL;
456 Py_DECREF(eq);
457 if (eq == Py_True) {
458 Py_RETURN_FALSE;
459 }
460 else {
461 Py_RETURN_TRUE;
462 }
463 }
464
465 gaobject *aa = (gaobject *)a;
466 gaobject *bb = (gaobject *)b;
467 int eq = PyObject_RichCompareBool(aa->origin, bb->origin, Py_EQ);
468 if (eq < 0) {
469 return NULL;
470 }
471 if (!eq) {
472 Py_RETURN_FALSE;
473 }
474 return PyObject_RichCompare(aa->args, bb->args, Py_EQ);
475 }
476
477 static PyObject *
ga_mro_entries(PyObject * self,PyObject * args)478 ga_mro_entries(PyObject *self, PyObject *args)
479 {
480 gaobject *alias = (gaobject *)self;
481 return PyTuple_Pack(1, alias->origin);
482 }
483
484 static PyObject *
ga_instancecheck(PyObject * self,PyObject * Py_UNUSED (ignored))485 ga_instancecheck(PyObject *self, PyObject *Py_UNUSED(ignored))
486 {
487 PyErr_SetString(PyExc_TypeError,
488 "isinstance() argument 2 cannot be a parameterized generic");
489 return NULL;
490 }
491
492 static PyObject *
ga_subclasscheck(PyObject * self,PyObject * Py_UNUSED (ignored))493 ga_subclasscheck(PyObject *self, PyObject *Py_UNUSED(ignored))
494 {
495 PyErr_SetString(PyExc_TypeError,
496 "issubclass() argument 2 cannot be a parameterized generic");
497 return NULL;
498 }
499
500 static PyObject *
ga_reduce(PyObject * self,PyObject * Py_UNUSED (ignored))501 ga_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
502 {
503 gaobject *alias = (gaobject *)self;
504 return Py_BuildValue("O(OO)", Py_TYPE(alias),
505 alias->origin, alias->args);
506 }
507
508 static PyObject *
ga_dir(PyObject * self,PyObject * Py_UNUSED (ignored))509 ga_dir(PyObject *self, PyObject *Py_UNUSED(ignored))
510 {
511 gaobject *alias = (gaobject *)self;
512 PyObject *dir = PyObject_Dir(alias->origin);
513 if (dir == NULL) {
514 return NULL;
515 }
516
517 PyObject *dir_entry = NULL;
518 for (const char * const *p = attr_exceptions; ; p++) {
519 if (*p == NULL) {
520 break;
521 }
522 else {
523 dir_entry = PyUnicode_FromString(*p);
524 if (dir_entry == NULL) {
525 goto error;
526 }
527 int contains = PySequence_Contains(dir, dir_entry);
528 if (contains < 0) {
529 goto error;
530 }
531 if (contains == 0 && PyList_Append(dir, dir_entry) < 0) {
532 goto error;
533 }
534
535 Py_CLEAR(dir_entry);
536 }
537 }
538 return dir;
539
540 error:
541 Py_DECREF(dir);
542 Py_XDECREF(dir_entry);
543 return NULL;
544 }
545
546 static PyMethodDef ga_methods[] = {
547 {"__mro_entries__", ga_mro_entries, METH_O},
548 {"__instancecheck__", ga_instancecheck, METH_O},
549 {"__subclasscheck__", ga_subclasscheck, METH_O},
550 {"__reduce__", ga_reduce, METH_NOARGS},
551 {"__dir__", ga_dir, METH_NOARGS},
552 {0}
553 };
554
555 static PyMemberDef ga_members[] = {
556 {"__origin__", T_OBJECT, offsetof(gaobject, origin), READONLY},
557 {"__args__", T_OBJECT, offsetof(gaobject, args), READONLY},
558 {0}
559 };
560
561 static PyObject *
ga_parameters(PyObject * self,void * unused)562 ga_parameters(PyObject *self, void *unused)
563 {
564 gaobject *alias = (gaobject *)self;
565 if (alias->parameters == NULL) {
566 alias->parameters = _Py_make_parameters(alias->args);
567 if (alias->parameters == NULL) {
568 return NULL;
569 }
570 }
571 Py_INCREF(alias->parameters);
572 return alias->parameters;
573 }
574
575 static PyGetSetDef ga_properties[] = {
576 {"__parameters__", ga_parameters, (setter)NULL, "Type variables in the GenericAlias.", NULL},
577 {0}
578 };
579
580 /* A helper function to create GenericAlias' args tuple and set its attributes.
581 * Returns 1 on success, 0 on failure.
582 */
583 static inline int
setup_ga(gaobject * alias,PyObject * origin,PyObject * args)584 setup_ga(gaobject *alias, PyObject *origin, PyObject *args) {
585 if (!PyTuple_Check(args)) {
586 args = PyTuple_Pack(1, args);
587 if (args == NULL) {
588 return 0;
589 }
590 }
591 else {
592 Py_INCREF(args);
593 }
594
595 Py_INCREF(origin);
596 alias->origin = origin;
597 alias->args = args;
598 alias->parameters = NULL;
599 alias->weakreflist = NULL;
600 return 1;
601 }
602
603 static PyObject *
ga_new(PyTypeObject * type,PyObject * args,PyObject * kwds)604 ga_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
605 {
606 if (!_PyArg_NoKeywords("GenericAlias", kwds)) {
607 return NULL;
608 }
609 if (!_PyArg_CheckPositional("GenericAlias", PyTuple_GET_SIZE(args), 2, 2)) {
610 return NULL;
611 }
612 PyObject *origin = PyTuple_GET_ITEM(args, 0);
613 PyObject *arguments = PyTuple_GET_ITEM(args, 1);
614 gaobject *self = (gaobject *)type->tp_alloc(type, 0);
615 if (self == NULL) {
616 return NULL;
617 }
618 if (!setup_ga(self, origin, arguments)) {
619 Py_DECREF(self);
620 return NULL;
621 }
622 return (PyObject *)self;
623 }
624
625 static PyNumberMethods ga_as_number = {
626 .nb_or = _Py_union_type_or, // Add __or__ function
627 };
628
629 // TODO:
630 // - argument clinic?
631 // - __doc__?
632 // - cache?
633 PyTypeObject Py_GenericAliasType = {
634 PyVarObject_HEAD_INIT(&PyType_Type, 0)
635 .tp_name = "types.GenericAlias",
636 .tp_doc = "Represent a PEP 585 generic type\n"
637 "\n"
638 "E.g. for t = list[int], t.__origin__ is list and t.__args__ is (int,).",
639 .tp_basicsize = sizeof(gaobject),
640 .tp_dealloc = ga_dealloc,
641 .tp_repr = ga_repr,
642 .tp_as_number = &ga_as_number, // allow X | Y of GenericAlias objs
643 .tp_as_mapping = &ga_as_mapping,
644 .tp_hash = ga_hash,
645 .tp_call = ga_call,
646 .tp_getattro = ga_getattro,
647 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE,
648 .tp_traverse = ga_traverse,
649 .tp_richcompare = ga_richcompare,
650 .tp_weaklistoffset = offsetof(gaobject, weakreflist),
651 .tp_methods = ga_methods,
652 .tp_members = ga_members,
653 .tp_alloc = PyType_GenericAlloc,
654 .tp_new = ga_new,
655 .tp_free = PyObject_GC_Del,
656 .tp_getset = ga_properties,
657 };
658
659 PyObject *
Py_GenericAlias(PyObject * origin,PyObject * args)660 Py_GenericAlias(PyObject *origin, PyObject *args)
661 {
662 gaobject *alias = (gaobject*) PyType_GenericAlloc(
663 (PyTypeObject *)&Py_GenericAliasType, 0);
664 if (alias == NULL) {
665 return NULL;
666 }
667 if (!setup_ga(alias, origin, args)) {
668 Py_DECREF(alias);
669 return NULL;
670 }
671 return (PyObject *)alias;
672 }
673