blob: d3d387193d3572e93ea65ebdb30cbd30b77cd8b6 [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"
kj4eb41d02020-11-09 12:00:13 +08005#include "pycore_unionobject.h" // _Py_union_as_number
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{
kj463c7d32020-12-14 02:38:24 +0800444 if (!PyObject_TypeCheck(a, &Py_GenericAliasType) ||
445 !PyObject_TypeCheck(b, &Py_GenericAliasType) ||
Guido van Rossum48b069a2020-04-07 09:50:06 -0700446 (op != Py_EQ && op != Py_NE))
447 {
448 Py_RETURN_NOTIMPLEMENTED;
449 }
450
451 if (op == Py_NE) {
452 PyObject *eq = ga_richcompare(a, b, Py_EQ);
453 if (eq == NULL)
454 return NULL;
455 Py_DECREF(eq);
456 if (eq == Py_True) {
457 Py_RETURN_FALSE;
458 }
459 else {
460 Py_RETURN_TRUE;
461 }
462 }
463
464 gaobject *aa = (gaobject *)a;
465 gaobject *bb = (gaobject *)b;
466 int eq = PyObject_RichCompareBool(aa->origin, bb->origin, Py_EQ);
467 if (eq < 0) {
468 return NULL;
469 }
470 if (!eq) {
471 Py_RETURN_FALSE;
472 }
473 return PyObject_RichCompare(aa->args, bb->args, Py_EQ);
474}
475
476static PyObject *
477ga_mro_entries(PyObject *self, PyObject *args)
478{
479 gaobject *alias = (gaobject *)self;
480 return PyTuple_Pack(1, alias->origin);
481}
482
483static PyObject *
484ga_instancecheck(PyObject *self, PyObject *Py_UNUSED(ignored))
485{
486 PyErr_SetString(PyExc_TypeError,
487 "isinstance() argument 2 cannot be a parameterized generic");
488 return NULL;
489}
490
491static PyObject *
492ga_subclasscheck(PyObject *self, PyObject *Py_UNUSED(ignored))
493{
494 PyErr_SetString(PyExc_TypeError,
495 "issubclass() argument 2 cannot be a parameterized generic");
496 return NULL;
497}
498
499static PyObject *
500ga_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
501{
502 gaobject *alias = (gaobject *)self;
503 return Py_BuildValue("O(OO)", Py_TYPE(alias),
504 alias->origin, alias->args);
505}
506
Batuhan Taskaya2e877742020-09-16 00:58:32 +0300507static PyObject *
508ga_dir(PyObject *self, PyObject *Py_UNUSED(ignored))
509{
510 gaobject *alias = (gaobject *)self;
511 PyObject *dir = PyObject_Dir(alias->origin);
512 if (dir == NULL) {
513 return NULL;
514 }
515
516 PyObject *dir_entry = NULL;
517 for (const char * const *p = attr_exceptions; ; p++) {
518 if (*p == NULL) {
519 break;
520 }
521 else {
522 dir_entry = PyUnicode_FromString(*p);
523 if (dir_entry == NULL) {
524 goto error;
525 }
526 int contains = PySequence_Contains(dir, dir_entry);
527 if (contains < 0) {
528 goto error;
529 }
530 if (contains == 0 && PyList_Append(dir, dir_entry) < 0) {
531 goto error;
532 }
533
534 Py_CLEAR(dir_entry);
535 }
536 }
537 return dir;
538
539error:
540 Py_DECREF(dir);
541 Py_XDECREF(dir_entry);
542 return NULL;
543}
544
Guido van Rossum48b069a2020-04-07 09:50:06 -0700545static PyMethodDef ga_methods[] = {
546 {"__mro_entries__", ga_mro_entries, METH_O},
547 {"__instancecheck__", ga_instancecheck, METH_O},
548 {"__subclasscheck__", ga_subclasscheck, METH_O},
549 {"__reduce__", ga_reduce, METH_NOARGS},
Batuhan Taskaya2e877742020-09-16 00:58:32 +0300550 {"__dir__", ga_dir, METH_NOARGS},
Guido van Rossum48b069a2020-04-07 09:50:06 -0700551 {0}
552};
553
554static PyMemberDef ga_members[] = {
555 {"__origin__", T_OBJECT, offsetof(gaobject, origin), READONLY},
556 {"__args__", T_OBJECT, offsetof(gaobject, args), READONLY},
557 {0}
558};
559
560static PyObject *
561ga_parameters(PyObject *self, void *unused)
562{
563 gaobject *alias = (gaobject *)self;
564 if (alias->parameters == NULL) {
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300565 alias->parameters = _Py_make_parameters(alias->args);
Guido van Rossum48b069a2020-04-07 09:50:06 -0700566 if (alias->parameters == NULL) {
567 return NULL;
568 }
569 }
570 Py_INCREF(alias->parameters);
571 return alias->parameters;
572}
573
574static PyGetSetDef ga_properties[] = {
575 {"__parameters__", ga_parameters, (setter)NULL, "Type variables in the GenericAlias.", NULL},
576 {0}
577};
578
kj463c7d32020-12-14 02:38:24 +0800579/* A helper function to create GenericAlias' args tuple and set its attributes.
580 * Returns 1 on success, 0 on failure.
581 */
582static inline int
583setup_ga(gaobject *alias, PyObject *origin, PyObject *args) {
584 if (!PyTuple_Check(args)) {
585 args = PyTuple_Pack(1, args);
586 if (args == NULL) {
587 return 0;
588 }
589 }
590 else {
591 Py_INCREF(args);
592 }
593
594 Py_INCREF(origin);
595 alias->origin = origin;
596 alias->args = args;
597 alias->parameters = NULL;
598 alias->weakreflist = NULL;
599 return 1;
600}
601
Guido van Rossum48b069a2020-04-07 09:50:06 -0700602static PyObject *
603ga_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
604{
kj804d6892020-12-05 23:02:14 +0700605 if (!_PyArg_NoKeywords("GenericAlias", kwds)) {
Guido van Rossum48b069a2020-04-07 09:50:06 -0700606 return NULL;
607 }
Dong-hee Na02e44842020-04-24 01:25:53 +0900608 if (!_PyArg_CheckPositional("GenericAlias", PyTuple_GET_SIZE(args), 2, 2)) {
Guido van Rossum48b069a2020-04-07 09:50:06 -0700609 return NULL;
610 }
611 PyObject *origin = PyTuple_GET_ITEM(args, 0);
612 PyObject *arguments = PyTuple_GET_ITEM(args, 1);
kj463c7d32020-12-14 02:38:24 +0800613 gaobject *self = (gaobject *)type->tp_alloc(type, 0);
614 if (self == NULL) {
615 return NULL;
616 }
617 if (!setup_ga(self, origin, arguments)) {
Miss Islington (bot)68330b62021-07-04 10:55:35 -0700618 Py_DECREF(self);
kj463c7d32020-12-14 02:38:24 +0800619 return NULL;
620 }
621 return (PyObject *)self;
Guido van Rossum48b069a2020-04-07 09:50:06 -0700622}
623
kj4eb41d02020-11-09 12:00:13 +0800624static PyNumberMethods ga_as_number = {
625 .nb_or = (binaryfunc)_Py_union_type_or, // Add __or__ function
626};
627
Guido van Rossum48b069a2020-04-07 09:50:06 -0700628// TODO:
629// - argument clinic?
630// - __doc__?
631// - cache?
632PyTypeObject Py_GenericAliasType = {
633 PyVarObject_HEAD_INIT(&PyType_Type, 0)
634 .tp_name = "types.GenericAlias",
635 .tp_doc = "Represent a PEP 585 generic type\n"
636 "\n"
Mikhail Golubev77f0a232020-10-09 00:38:36 +0300637 "E.g. for t = list[int], t.__origin__ is list and t.__args__ is (int,).",
Guido van Rossum48b069a2020-04-07 09:50:06 -0700638 .tp_basicsize = sizeof(gaobject),
639 .tp_dealloc = ga_dealloc,
640 .tp_repr = ga_repr,
kj4eb41d02020-11-09 12:00:13 +0800641 .tp_as_number = &ga_as_number, // allow X | Y of GenericAlias objs
Guido van Rossum48b069a2020-04-07 09:50:06 -0700642 .tp_as_mapping = &ga_as_mapping,
643 .tp_hash = ga_hash,
644 .tp_call = ga_call,
645 .tp_getattro = ga_getattro,
kj463c7d32020-12-14 02:38:24 +0800646 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE,
Guido van Rossum48b069a2020-04-07 09:50:06 -0700647 .tp_traverse = ga_traverse,
648 .tp_richcompare = ga_richcompare,
kj384b7a42020-11-16 11:27:23 +0800649 .tp_weaklistoffset = offsetof(gaobject, weakreflist),
Guido van Rossum48b069a2020-04-07 09:50:06 -0700650 .tp_methods = ga_methods,
651 .tp_members = ga_members,
652 .tp_alloc = PyType_GenericAlloc,
653 .tp_new = ga_new,
654 .tp_free = PyObject_GC_Del,
655 .tp_getset = ga_properties,
656};
657
658PyObject *
659Py_GenericAlias(PyObject *origin, PyObject *args)
660{
Miss Islington (bot)d17cc1f2021-07-05 04:33:53 -0700661 gaobject *alias = (gaobject*) PyType_GenericAlloc(
662 (PyTypeObject *)&Py_GenericAliasType, 0);
Guido van Rossum48b069a2020-04-07 09:50:06 -0700663 if (alias == NULL) {
Guido van Rossum48b069a2020-04-07 09:50:06 -0700664 return NULL;
665 }
kj463c7d32020-12-14 02:38:24 +0800666 if (!setup_ga(alias, origin, args)) {
Miss Islington (bot)68330b62021-07-04 10:55:35 -0700667 Py_DECREF(alias);
kj463c7d32020-12-14 02:38:24 +0800668 return NULL;
669 }
Guido van Rossum48b069a2020-04-07 09:50:06 -0700670 return (PyObject *)alias;
671}