blob: dbe5d89b7396296f845ffa578ab0410e894fed43 [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__",
Miss Islington (bot)de4c9c02021-09-15 12:35:16 -0700421 "__copy__",
422 "__deepcopy__",
Guido van Rossum48b069a2020-04-07 09:50:06 -0700423 NULL,
424};
425
426static PyObject *
427ga_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
443static PyObject *
444ga_richcompare(PyObject *a, PyObject *b, int op)
445{
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700446 if (!_PyGenericAlias_Check(b) ||
Guido van Rossum48b069a2020-04-07 09:50:06 -0700447 (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
477static PyObject *
478ga_mro_entries(PyObject *self, PyObject *args)
479{
480 gaobject *alias = (gaobject *)self;
481 return PyTuple_Pack(1, alias->origin);
482}
483
484static PyObject *
485ga_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
492static PyObject *
493ga_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
500static PyObject *
501ga_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
Batuhan Taskaya2e877742020-09-16 00:58:32 +0300508static PyObject *
509ga_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
540error:
541 Py_DECREF(dir);
542 Py_XDECREF(dir_entry);
543 return NULL;
544}
545
Guido van Rossum48b069a2020-04-07 09:50:06 -0700546static 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},
Batuhan Taskaya2e877742020-09-16 00:58:32 +0300551 {"__dir__", ga_dir, METH_NOARGS},
Guido van Rossum48b069a2020-04-07 09:50:06 -0700552 {0}
553};
554
555static PyMemberDef ga_members[] = {
556 {"__origin__", T_OBJECT, offsetof(gaobject, origin), READONLY},
557 {"__args__", T_OBJECT, offsetof(gaobject, args), READONLY},
558 {0}
559};
560
561static PyObject *
562ga_parameters(PyObject *self, void *unused)
563{
564 gaobject *alias = (gaobject *)self;
565 if (alias->parameters == NULL) {
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300566 alias->parameters = _Py_make_parameters(alias->args);
Guido van Rossum48b069a2020-04-07 09:50:06 -0700567 if (alias->parameters == NULL) {
568 return NULL;
569 }
570 }
571 Py_INCREF(alias->parameters);
572 return alias->parameters;
573}
574
575static PyGetSetDef ga_properties[] = {
576 {"__parameters__", ga_parameters, (setter)NULL, "Type variables in the GenericAlias.", NULL},
577 {0}
578};
579
kj463c7d32020-12-14 02:38:24 +0800580/* A helper function to create GenericAlias' args tuple and set its attributes.
Serhiy Storchaka93242d72021-10-03 20:03:49 +0300581 * Returns 1 on success, 0 on failure.
kj463c7d32020-12-14 02:38:24 +0800582 */
583static inline int
584setup_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
Guido van Rossum48b069a2020-04-07 09:50:06 -0700603static PyObject *
604ga_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
605{
kj804d6892020-12-05 23:02:14 +0700606 if (!_PyArg_NoKeywords("GenericAlias", kwds)) {
Guido van Rossum48b069a2020-04-07 09:50:06 -0700607 return NULL;
608 }
Dong-hee Na02e44842020-04-24 01:25:53 +0900609 if (!_PyArg_CheckPositional("GenericAlias", PyTuple_GET_SIZE(args), 2, 2)) {
Guido van Rossum48b069a2020-04-07 09:50:06 -0700610 return NULL;
611 }
612 PyObject *origin = PyTuple_GET_ITEM(args, 0);
613 PyObject *arguments = PyTuple_GET_ITEM(args, 1);
kj463c7d32020-12-14 02:38:24 +0800614 gaobject *self = (gaobject *)type->tp_alloc(type, 0);
615 if (self == NULL) {
616 return NULL;
617 }
618 if (!setup_ga(self, origin, arguments)) {
Miss Islington (bot)68330b62021-07-04 10:55:35 -0700619 Py_DECREF(self);
kj463c7d32020-12-14 02:38:24 +0800620 return NULL;
621 }
622 return (PyObject *)self;
Guido van Rossum48b069a2020-04-07 09:50:06 -0700623}
624
kj4eb41d02020-11-09 12:00:13 +0800625static PyNumberMethods ga_as_number = {
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700626 .nb_or = _Py_union_type_or, // Add __or__ function
kj4eb41d02020-11-09 12:00:13 +0800627};
628
Guido van Rossum48b069a2020-04-07 09:50:06 -0700629// TODO:
630// - argument clinic?
631// - __doc__?
632// - cache?
633PyTypeObject 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"
Mikhail Golubev77f0a232020-10-09 00:38:36 +0300638 "E.g. for t = list[int], t.__origin__ is list and t.__args__ is (int,).",
Guido van Rossum48b069a2020-04-07 09:50:06 -0700639 .tp_basicsize = sizeof(gaobject),
640 .tp_dealloc = ga_dealloc,
641 .tp_repr = ga_repr,
kj4eb41d02020-11-09 12:00:13 +0800642 .tp_as_number = &ga_as_number, // allow X | Y of GenericAlias objs
Guido van Rossum48b069a2020-04-07 09:50:06 -0700643 .tp_as_mapping = &ga_as_mapping,
644 .tp_hash = ga_hash,
645 .tp_call = ga_call,
646 .tp_getattro = ga_getattro,
kj463c7d32020-12-14 02:38:24 +0800647 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE,
Guido van Rossum48b069a2020-04-07 09:50:06 -0700648 .tp_traverse = ga_traverse,
649 .tp_richcompare = ga_richcompare,
kj384b7a42020-11-16 11:27:23 +0800650 .tp_weaklistoffset = offsetof(gaobject, weakreflist),
Guido van Rossum48b069a2020-04-07 09:50:06 -0700651 .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
659PyObject *
660Py_GenericAlias(PyObject *origin, PyObject *args)
661{
Miss Islington (bot)d17cc1f2021-07-05 04:33:53 -0700662 gaobject *alias = (gaobject*) PyType_GenericAlloc(
663 (PyTypeObject *)&Py_GenericAliasType, 0);
Guido van Rossum48b069a2020-04-07 09:50:06 -0700664 if (alias == NULL) {
Guido van Rossum48b069a2020-04-07 09:50:06 -0700665 return NULL;
666 }
kj463c7d32020-12-14 02:38:24 +0800667 if (!setup_ga(alias, origin, args)) {
Miss Islington (bot)68330b62021-07-04 10:55:35 -0700668 Py_DECREF(alias);
kj463c7d32020-12-14 02:38:24 +0800669 return NULL;
670 }
Guido van Rossum48b069a2020-04-07 09:50:06 -0700671 return (PyObject *)alias;
672}