blob: dda53cb53386455ec96b453a66189b8c448bd51c [file] [log] [blame]
Guido van Rossum48b069a2020-04-07 09:50:06 -07001// types.GenericAlias -- used to represent e.g. list[int].
2
3#include "Python.h"
4#include "pycore_object.h"
Miss Islington (bot)03aad302021-07-17 14:10:21 -07005#include "pycore_unionobject.h" // _Py_union_type_or, _PyGenericAlias_Check
Victor Stinner4a21e572020-04-15 02:35:41 +02006#include "structmember.h" // PyMemberDef
Guido van Rossum48b069a2020-04-07 09:50:06 -07007
8typedef struct {
9 PyObject_HEAD
10 PyObject *origin;
11 PyObject *args;
12 PyObject *parameters;
kj384b7a42020-11-16 11:27:23 +080013 PyObject* weakreflist;
Guido van Rossum48b069a2020-04-07 09:50:06 -070014} gaobject;
15
16static void
17ga_dealloc(PyObject *self)
18{
19 gaobject *alias = (gaobject *)self;
20
21 _PyObject_GC_UNTRACK(self);
kj384b7a42020-11-16 11:27:23 +080022 if (alias->weakreflist != NULL) {
23 PyObject_ClearWeakRefs((PyObject *)alias);
24 }
Guido van Rossum48b069a2020-04-07 09:50:06 -070025 Py_XDECREF(alias->origin);
26 Py_XDECREF(alias->args);
27 Py_XDECREF(alias->parameters);
Victor Stinner8182cc22020-07-10 12:40:38 +020028 Py_TYPE(self)->tp_free(self);
Guido van Rossum48b069a2020-04-07 09:50:06 -070029}
30
31static int
32ga_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
41static int
42ga_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
101use_repr:
102 r = PyObject_Repr(p);
103
104done:
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
118static PyObject *
119ga_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);
Victor Stinner4a21e572020-04-15 02:35:41 +0200126
Guido van Rossum48b069a2020-04-07 09:50:06 -0700127 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);
154error:
155 _PyUnicodeWriter_Dealloc(&writer);
156 return NULL;
157}
158
Ken Jin859577c2021-04-28 23:38:14 +0800159// isinstance(obj, TypeVar) without importing typing.py.
160// Returns -1 for errors.
161static int
162is_typevar(PyObject *obj)
Guido van Rossum48b069a2020-04-07 09:50:06 -0700163{
164 PyTypeObject *type = Py_TYPE(obj);
Ken Jin859577c2021-04-28 23:38:14 +0800165 if (strcmp(type->tp_name, "TypeVar") != 0) {
Guido van Rossum48b069a2020-04-07 09:50:06 -0700166 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)
179static Py_ssize_t
180tuple_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
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300190static int
191tuple_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
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300201PyObject *
202_Py_make_parameters(PyObject *args)
Guido van Rossum48b069a2020-04-07 09:50:06 -0700203{
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300204 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
205 Py_ssize_t len = nargs;
Guido van Rossum48b069a2020-04-07 09:50:06 -0700206 PyObject *parameters = PyTuple_New(len);
207 if (parameters == NULL)
208 return NULL;
209 Py_ssize_t iparam = 0;
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300210 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
Guido van Rossum48b069a2020-04-07 09:50:06 -0700211 PyObject *t = PyTuple_GET_ITEM(args, iarg);
Ken Jin859577c2021-04-28 23:38:14 +0800212 int typevar = is_typevar(t);
Guido van Rossum48b069a2020-04-07 09:50:06 -0700213 if (typevar < 0) {
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300214 Py_DECREF(parameters);
Guido van Rossum48b069a2020-04-07 09:50:06 -0700215 return NULL;
216 }
217 if (typevar) {
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300218 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;
Guido van Rossum48b069a2020-04-07 09:50:06 -0700226 }
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300227 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(&parameters, 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);
Guido van Rossum48b069a2020-04-07 09:50:06 -0700244 }
245 }
246 if (iparam < len) {
247 if (_PyTuple_Resize(&parameters, iparam) < 0) {
248 Py_XDECREF(parameters);
249 return NULL;
250 }
251 }
252 return parameters;
253}
254
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300255/* 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. */
260static PyObject *
261subs_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 }
Ken Jin859577c2021-04-28 23:38:14 +0800282 Py_INCREF(arg);
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300283 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
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300297PyObject *
298_Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObject *item)
Guido van Rossum48b069a2020-04-07 09:50:06 -0700299{
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300300 Py_ssize_t nparams = PyTuple_GET_SIZE(parameters);
Guido van Rossum48b069a2020-04-07 09:50:06 -0700301 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);
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300307 Py_ssize_t nitems = is_tuple ? PyTuple_GET_SIZE(item) : 1;
308 PyObject **argitems = is_tuple ? &PyTuple_GET_ITEM(item, 0) : &item;
Ken Jin859577c2021-04-28 23:38:14 +0800309 if (nitems != nparams) {
310 return PyErr_Format(PyExc_TypeError,
311 "Too %s arguments for %R",
312 nitems > nparams ? "many" : "few",
313 self);
Guido van Rossum48b069a2020-04-07 09:50:06 -0700314 }
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300315 /* Replace all type variables (specified by parameters)
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300316 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 */
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300321 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
Guido van Rossum48b069a2020-04-07 09:50:06 -0700322 PyObject *newargs = PyTuple_New(nargs);
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300323 if (newargs == NULL) {
Guido van Rossum48b069a2020-04-07 09:50:06 -0700324 return NULL;
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300325 }
Guido van Rossum48b069a2020-04-07 09:50:06 -0700326 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300327 PyObject *arg = PyTuple_GET_ITEM(args, iarg);
Ken Jin859577c2021-04-28 23:38:14 +0800328 int typevar = is_typevar(arg);
Guido van Rossum48b069a2020-04-07 09:50:06 -0700329 if (typevar < 0) {
330 Py_DECREF(newargs);
331 return NULL;
332 }
333 if (typevar) {
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300334 Py_ssize_t iparam = tuple_index(parameters, nparams, arg);
Guido van Rossum48b069a2020-04-07 09:50:06 -0700335 assert(iparam >= 0);
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300336 arg = argitems[iparam];
Ken Jin859577c2021-04-28 23:38:14 +0800337 Py_INCREF(arg);
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300338 }
339 else {
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300340 arg = subs_tvars(arg, parameters, argitems);
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300341 if (arg == NULL) {
342 Py_DECREF(newargs);
343 return NULL;
Guido van Rossum48b069a2020-04-07 09:50:06 -0700344 }
345 }
Guido van Rossum48b069a2020-04-07 09:50:06 -0700346 PyTuple_SET_ITEM(newargs, iarg, arg);
347 }
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300348
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300349 return newargs;
350}
351
352static PyObject *
353ga_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
Guido van Rossum48b069a2020-04-07 09:50:06 -0700369 PyObject *res = Py_GenericAlias(alias->origin, newargs);
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300370
Guido van Rossum48b069a2020-04-07 09:50:06 -0700371 Py_DECREF(newargs);
372 return res;
373}
374
375static PyMappingMethods ga_as_mapping = {
376 .mp_subscript = ga_getitem,
377};
378
379static Py_hash_t
380ga_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
395static PyObject *
396ga_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
414static 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 NULL,
422};
423
424static PyObject *
425ga_getattro(PyObject *self, PyObject *name)
426{
427 gaobject *alias = (gaobject *)self;
428 if (PyUnicode_Check(name)) {
429 for (const char * const *p = attr_exceptions; ; p++) {
430 if (*p == NULL) {
431 return PyObject_GetAttr(alias->origin, name);
432 }
433 if (_PyUnicode_EqualToASCIIString(name, *p)) {
434 break;
435 }
436 }
437 }
438 return PyObject_GenericGetAttr(self, name);
439}
440
441static PyObject *
442ga_richcompare(PyObject *a, PyObject *b, int op)
443{
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700444 if (!_PyGenericAlias_Check(b) ||
Guido van Rossum48b069a2020-04-07 09:50:06 -0700445 (op != Py_EQ && op != Py_NE))
446 {
447 Py_RETURN_NOTIMPLEMENTED;
448 }
449
450 if (op == Py_NE) {
451 PyObject *eq = ga_richcompare(a, b, Py_EQ);
452 if (eq == NULL)
453 return NULL;
454 Py_DECREF(eq);
455 if (eq == Py_True) {
456 Py_RETURN_FALSE;
457 }
458 else {
459 Py_RETURN_TRUE;
460 }
461 }
462
463 gaobject *aa = (gaobject *)a;
464 gaobject *bb = (gaobject *)b;
465 int eq = PyObject_RichCompareBool(aa->origin, bb->origin, Py_EQ);
466 if (eq < 0) {
467 return NULL;
468 }
469 if (!eq) {
470 Py_RETURN_FALSE;
471 }
472 return PyObject_RichCompare(aa->args, bb->args, Py_EQ);
473}
474
475static PyObject *
476ga_mro_entries(PyObject *self, PyObject *args)
477{
478 gaobject *alias = (gaobject *)self;
479 return PyTuple_Pack(1, alias->origin);
480}
481
482static PyObject *
483ga_instancecheck(PyObject *self, PyObject *Py_UNUSED(ignored))
484{
485 PyErr_SetString(PyExc_TypeError,
486 "isinstance() argument 2 cannot be a parameterized generic");
487 return NULL;
488}
489
490static PyObject *
491ga_subclasscheck(PyObject *self, PyObject *Py_UNUSED(ignored))
492{
493 PyErr_SetString(PyExc_TypeError,
494 "issubclass() argument 2 cannot be a parameterized generic");
495 return NULL;
496}
497
498static PyObject *
499ga_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
500{
501 gaobject *alias = (gaobject *)self;
502 return Py_BuildValue("O(OO)", Py_TYPE(alias),
503 alias->origin, alias->args);
504}
505
Batuhan Taskaya2e877742020-09-16 00:58:32 +0300506static PyObject *
507ga_dir(PyObject *self, PyObject *Py_UNUSED(ignored))
508{
509 gaobject *alias = (gaobject *)self;
510 PyObject *dir = PyObject_Dir(alias->origin);
511 if (dir == NULL) {
512 return NULL;
513 }
514
515 PyObject *dir_entry = NULL;
516 for (const char * const *p = attr_exceptions; ; p++) {
517 if (*p == NULL) {
518 break;
519 }
520 else {
521 dir_entry = PyUnicode_FromString(*p);
522 if (dir_entry == NULL) {
523 goto error;
524 }
525 int contains = PySequence_Contains(dir, dir_entry);
526 if (contains < 0) {
527 goto error;
528 }
529 if (contains == 0 && PyList_Append(dir, dir_entry) < 0) {
530 goto error;
531 }
532
533 Py_CLEAR(dir_entry);
534 }
535 }
536 return dir;
537
538error:
539 Py_DECREF(dir);
540 Py_XDECREF(dir_entry);
541 return NULL;
542}
543
Guido van Rossum48b069a2020-04-07 09:50:06 -0700544static PyMethodDef ga_methods[] = {
545 {"__mro_entries__", ga_mro_entries, METH_O},
546 {"__instancecheck__", ga_instancecheck, METH_O},
547 {"__subclasscheck__", ga_subclasscheck, METH_O},
548 {"__reduce__", ga_reduce, METH_NOARGS},
Batuhan Taskaya2e877742020-09-16 00:58:32 +0300549 {"__dir__", ga_dir, METH_NOARGS},
Guido van Rossum48b069a2020-04-07 09:50:06 -0700550 {0}
551};
552
553static PyMemberDef ga_members[] = {
554 {"__origin__", T_OBJECT, offsetof(gaobject, origin), READONLY},
555 {"__args__", T_OBJECT, offsetof(gaobject, args), READONLY},
556 {0}
557};
558
559static PyObject *
560ga_parameters(PyObject *self, void *unused)
561{
562 gaobject *alias = (gaobject *)self;
563 if (alias->parameters == NULL) {
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300564 alias->parameters = _Py_make_parameters(alias->args);
Guido van Rossum48b069a2020-04-07 09:50:06 -0700565 if (alias->parameters == NULL) {
566 return NULL;
567 }
568 }
569 Py_INCREF(alias->parameters);
570 return alias->parameters;
571}
572
573static PyGetSetDef ga_properties[] = {
574 {"__parameters__", ga_parameters, (setter)NULL, "Type variables in the GenericAlias.", NULL},
575 {0}
576};
577
kj463c7d32020-12-14 02:38:24 +0800578/* A helper function to create GenericAlias' args tuple and set its attributes.
579 * Returns 1 on success, 0 on failure.
580 */
581static inline int
582setup_ga(gaobject *alias, PyObject *origin, PyObject *args) {
583 if (!PyTuple_Check(args)) {
584 args = PyTuple_Pack(1, args);
585 if (args == NULL) {
586 return 0;
587 }
588 }
589 else {
590 Py_INCREF(args);
591 }
592
593 Py_INCREF(origin);
594 alias->origin = origin;
595 alias->args = args;
596 alias->parameters = NULL;
597 alias->weakreflist = NULL;
598 return 1;
599}
600
Guido van Rossum48b069a2020-04-07 09:50:06 -0700601static PyObject *
602ga_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
603{
kj804d6892020-12-05 23:02:14 +0700604 if (!_PyArg_NoKeywords("GenericAlias", kwds)) {
Guido van Rossum48b069a2020-04-07 09:50:06 -0700605 return NULL;
606 }
Dong-hee Na02e44842020-04-24 01:25:53 +0900607 if (!_PyArg_CheckPositional("GenericAlias", PyTuple_GET_SIZE(args), 2, 2)) {
Guido van Rossum48b069a2020-04-07 09:50:06 -0700608 return NULL;
609 }
610 PyObject *origin = PyTuple_GET_ITEM(args, 0);
611 PyObject *arguments = PyTuple_GET_ITEM(args, 1);
kj463c7d32020-12-14 02:38:24 +0800612 gaobject *self = (gaobject *)type->tp_alloc(type, 0);
613 if (self == NULL) {
614 return NULL;
615 }
616 if (!setup_ga(self, origin, arguments)) {
Miss Islington (bot)68330b62021-07-04 10:55:35 -0700617 Py_DECREF(self);
kj463c7d32020-12-14 02:38:24 +0800618 return NULL;
619 }
620 return (PyObject *)self;
Guido van Rossum48b069a2020-04-07 09:50:06 -0700621}
622
kj4eb41d02020-11-09 12:00:13 +0800623static PyNumberMethods ga_as_number = {
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700624 .nb_or = _Py_union_type_or, // Add __or__ function
kj4eb41d02020-11-09 12:00:13 +0800625};
626
Guido van Rossum48b069a2020-04-07 09:50:06 -0700627// TODO:
628// - argument clinic?
629// - __doc__?
630// - cache?
631PyTypeObject Py_GenericAliasType = {
632 PyVarObject_HEAD_INIT(&PyType_Type, 0)
633 .tp_name = "types.GenericAlias",
634 .tp_doc = "Represent a PEP 585 generic type\n"
635 "\n"
Mikhail Golubev77f0a232020-10-09 00:38:36 +0300636 "E.g. for t = list[int], t.__origin__ is list and t.__args__ is (int,).",
Guido van Rossum48b069a2020-04-07 09:50:06 -0700637 .tp_basicsize = sizeof(gaobject),
638 .tp_dealloc = ga_dealloc,
639 .tp_repr = ga_repr,
kj4eb41d02020-11-09 12:00:13 +0800640 .tp_as_number = &ga_as_number, // allow X | Y of GenericAlias objs
Guido van Rossum48b069a2020-04-07 09:50:06 -0700641 .tp_as_mapping = &ga_as_mapping,
642 .tp_hash = ga_hash,
643 .tp_call = ga_call,
644 .tp_getattro = ga_getattro,
kj463c7d32020-12-14 02:38:24 +0800645 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE,
Guido van Rossum48b069a2020-04-07 09:50:06 -0700646 .tp_traverse = ga_traverse,
647 .tp_richcompare = ga_richcompare,
kj384b7a42020-11-16 11:27:23 +0800648 .tp_weaklistoffset = offsetof(gaobject, weakreflist),
Guido van Rossum48b069a2020-04-07 09:50:06 -0700649 .tp_methods = ga_methods,
650 .tp_members = ga_members,
651 .tp_alloc = PyType_GenericAlloc,
652 .tp_new = ga_new,
653 .tp_free = PyObject_GC_Del,
654 .tp_getset = ga_properties,
655};
656
657PyObject *
658Py_GenericAlias(PyObject *origin, PyObject *args)
659{
Miss Islington (bot)d17cc1f2021-07-05 04:33:53 -0700660 gaobject *alias = (gaobject*) PyType_GenericAlloc(
661 (PyTypeObject *)&Py_GenericAliasType, 0);
Guido van Rossum48b069a2020-04-07 09:50:06 -0700662 if (alias == NULL) {
Guido van Rossum48b069a2020-04-07 09:50:06 -0700663 return NULL;
664 }
kj463c7d32020-12-14 02:38:24 +0800665 if (!setup_ga(alias, origin, args)) {
Miss Islington (bot)68330b62021-07-04 10:55:35 -0700666 Py_DECREF(alias);
kj463c7d32020-12-14 02:38:24 +0800667 return NULL;
668 }
Guido van Rossum48b069a2020-04-07 09:50:06 -0700669 return (PyObject *)alias;
670}