blob: 6102e05c165c5d617538e8d1ff8bfe57a2607f48 [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
159// isinstance(obj, TypeVar) without importing typing.py.
160// Returns -1 for errors.
161static int
162is_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)
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
Guido van Rossum48b069a2020-04-07 09:50:06 -0700201static PyObject *
202make_parameters(PyObject *args)
203{
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);
212 int typevar = is_typevar(t);
213 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 }
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
Guido van Rossum48b069a2020-04-07 09:50:06 -0700297static PyObject *
298ga_getitem(PyObject *self, PyObject *item)
299{
300 gaobject *alias = (gaobject *)self;
301 // do a lookup for __parameters__ so it gets populated (if not already)
302 if (alias->parameters == NULL) {
303 alias->parameters = make_parameters(alias->args);
304 if (alias->parameters == NULL) {
305 return NULL;
306 }
307 }
308 Py_ssize_t nparams = PyTuple_GET_SIZE(alias->parameters);
309 if (nparams == 0) {
310 return PyErr_Format(PyExc_TypeError,
311 "There are no type variables left in %R",
312 self);
313 }
314 int is_tuple = PyTuple_Check(item);
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300315 Py_ssize_t nitems = is_tuple ? PyTuple_GET_SIZE(item) : 1;
316 PyObject **argitems = is_tuple ? &PyTuple_GET_ITEM(item, 0) : &item;
317 if (nitems != nparams) {
Guido van Rossum48b069a2020-04-07 09:50:06 -0700318 return PyErr_Format(PyExc_TypeError,
319 "Too %s arguments for %R",
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300320 nitems > nparams ? "many" : "few",
Guido van Rossum48b069a2020-04-07 09:50:06 -0700321 self);
322 }
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300323 /* Replace all type variables (specified by alias->parameters)
324 with corresponding values specified by argitems.
325 t = list[T]; t[int] -> newargs = [int]
326 t = dict[str, T]; t[int] -> newargs = [str, int]
327 t = dict[T, list[S]]; t[str, int] -> newargs = [str, list[int]]
328 */
Guido van Rossum48b069a2020-04-07 09:50:06 -0700329 Py_ssize_t nargs = PyTuple_GET_SIZE(alias->args);
330 PyObject *newargs = PyTuple_New(nargs);
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300331 if (newargs == NULL) {
Guido van Rossum48b069a2020-04-07 09:50:06 -0700332 return NULL;
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300333 }
Guido van Rossum48b069a2020-04-07 09:50:06 -0700334 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
335 PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg);
336 int typevar = is_typevar(arg);
337 if (typevar < 0) {
338 Py_DECREF(newargs);
339 return NULL;
340 }
341 if (typevar) {
342 Py_ssize_t iparam = tuple_index(alias->parameters, nparams, arg);
343 assert(iparam >= 0);
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300344 arg = argitems[iparam];
345 Py_INCREF(arg);
346 }
347 else {
348 arg = subs_tvars(arg, alias->parameters, argitems);
349 if (arg == NULL) {
350 Py_DECREF(newargs);
351 return NULL;
Guido van Rossum48b069a2020-04-07 09:50:06 -0700352 }
353 }
Guido van Rossum48b069a2020-04-07 09:50:06 -0700354 PyTuple_SET_ITEM(newargs, iarg, arg);
355 }
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300356
Guido van Rossum48b069a2020-04-07 09:50:06 -0700357 PyObject *res = Py_GenericAlias(alias->origin, newargs);
Serhiy Storchaka41a64582020-05-04 10:56:05 +0300358
Guido van Rossum48b069a2020-04-07 09:50:06 -0700359 Py_DECREF(newargs);
360 return res;
361}
362
363static PyMappingMethods ga_as_mapping = {
364 .mp_subscript = ga_getitem,
365};
366
367static Py_hash_t
368ga_hash(PyObject *self)
369{
370 gaobject *alias = (gaobject *)self;
371 // TODO: Hash in the hash for the origin
372 Py_hash_t h0 = PyObject_Hash(alias->origin);
373 if (h0 == -1) {
374 return -1;
375 }
376 Py_hash_t h1 = PyObject_Hash(alias->args);
377 if (h1 == -1) {
378 return -1;
379 }
380 return h0 ^ h1;
381}
382
383static PyObject *
384ga_call(PyObject *self, PyObject *args, PyObject *kwds)
385{
386 gaobject *alias = (gaobject *)self;
387 PyObject *obj = PyObject_Call(alias->origin, args, kwds);
388 if (obj != NULL) {
389 if (PyObject_SetAttrString(obj, "__orig_class__", self) < 0) {
390 if (!PyErr_ExceptionMatches(PyExc_AttributeError) &&
391 !PyErr_ExceptionMatches(PyExc_TypeError))
392 {
393 Py_DECREF(obj);
394 return NULL;
395 }
396 PyErr_Clear();
397 }
398 }
399 return obj;
400}
401
402static const char* const attr_exceptions[] = {
403 "__origin__",
404 "__args__",
405 "__parameters__",
406 "__mro_entries__",
407 "__reduce_ex__", // needed so we don't look up object.__reduce_ex__
408 "__reduce__",
409 NULL,
410};
411
412static PyObject *
413ga_getattro(PyObject *self, PyObject *name)
414{
415 gaobject *alias = (gaobject *)self;
416 if (PyUnicode_Check(name)) {
417 for (const char * const *p = attr_exceptions; ; p++) {
418 if (*p == NULL) {
419 return PyObject_GetAttr(alias->origin, name);
420 }
421 if (_PyUnicode_EqualToASCIIString(name, *p)) {
422 break;
423 }
424 }
425 }
426 return PyObject_GenericGetAttr(self, name);
427}
428
429static PyObject *
430ga_richcompare(PyObject *a, PyObject *b, int op)
431{
Hai Shi5e8ffe12020-05-04 21:31:38 +0800432 if (!Py_IS_TYPE(a, &Py_GenericAliasType) ||
433 !Py_IS_TYPE(b, &Py_GenericAliasType) ||
Guido van Rossum48b069a2020-04-07 09:50:06 -0700434 (op != Py_EQ && op != Py_NE))
435 {
436 Py_RETURN_NOTIMPLEMENTED;
437 }
438
439 if (op == Py_NE) {
440 PyObject *eq = ga_richcompare(a, b, Py_EQ);
441 if (eq == NULL)
442 return NULL;
443 Py_DECREF(eq);
444 if (eq == Py_True) {
445 Py_RETURN_FALSE;
446 }
447 else {
448 Py_RETURN_TRUE;
449 }
450 }
451
452 gaobject *aa = (gaobject *)a;
453 gaobject *bb = (gaobject *)b;
454 int eq = PyObject_RichCompareBool(aa->origin, bb->origin, Py_EQ);
455 if (eq < 0) {
456 return NULL;
457 }
458 if (!eq) {
459 Py_RETURN_FALSE;
460 }
461 return PyObject_RichCompare(aa->args, bb->args, Py_EQ);
462}
463
464static PyObject *
465ga_mro_entries(PyObject *self, PyObject *args)
466{
467 gaobject *alias = (gaobject *)self;
468 return PyTuple_Pack(1, alias->origin);
469}
470
471static PyObject *
472ga_instancecheck(PyObject *self, PyObject *Py_UNUSED(ignored))
473{
474 PyErr_SetString(PyExc_TypeError,
475 "isinstance() argument 2 cannot be a parameterized generic");
476 return NULL;
477}
478
479static PyObject *
480ga_subclasscheck(PyObject *self, PyObject *Py_UNUSED(ignored))
481{
482 PyErr_SetString(PyExc_TypeError,
483 "issubclass() argument 2 cannot be a parameterized generic");
484 return NULL;
485}
486
487static PyObject *
488ga_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
489{
490 gaobject *alias = (gaobject *)self;
491 return Py_BuildValue("O(OO)", Py_TYPE(alias),
492 alias->origin, alias->args);
493}
494
Batuhan Taskaya2e877742020-09-16 00:58:32 +0300495static PyObject *
496ga_dir(PyObject *self, PyObject *Py_UNUSED(ignored))
497{
498 gaobject *alias = (gaobject *)self;
499 PyObject *dir = PyObject_Dir(alias->origin);
500 if (dir == NULL) {
501 return NULL;
502 }
503
504 PyObject *dir_entry = NULL;
505 for (const char * const *p = attr_exceptions; ; p++) {
506 if (*p == NULL) {
507 break;
508 }
509 else {
510 dir_entry = PyUnicode_FromString(*p);
511 if (dir_entry == NULL) {
512 goto error;
513 }
514 int contains = PySequence_Contains(dir, dir_entry);
515 if (contains < 0) {
516 goto error;
517 }
518 if (contains == 0 && PyList_Append(dir, dir_entry) < 0) {
519 goto error;
520 }
521
522 Py_CLEAR(dir_entry);
523 }
524 }
525 return dir;
526
527error:
528 Py_DECREF(dir);
529 Py_XDECREF(dir_entry);
530 return NULL;
531}
532
Guido van Rossum48b069a2020-04-07 09:50:06 -0700533static PyMethodDef ga_methods[] = {
534 {"__mro_entries__", ga_mro_entries, METH_O},
535 {"__instancecheck__", ga_instancecheck, METH_O},
536 {"__subclasscheck__", ga_subclasscheck, METH_O},
537 {"__reduce__", ga_reduce, METH_NOARGS},
Batuhan Taskaya2e877742020-09-16 00:58:32 +0300538 {"__dir__", ga_dir, METH_NOARGS},
Guido van Rossum48b069a2020-04-07 09:50:06 -0700539 {0}
540};
541
542static PyMemberDef ga_members[] = {
543 {"__origin__", T_OBJECT, offsetof(gaobject, origin), READONLY},
544 {"__args__", T_OBJECT, offsetof(gaobject, args), READONLY},
545 {0}
546};
547
548static PyObject *
549ga_parameters(PyObject *self, void *unused)
550{
551 gaobject *alias = (gaobject *)self;
552 if (alias->parameters == NULL) {
553 alias->parameters = make_parameters(alias->args);
554 if (alias->parameters == NULL) {
555 return NULL;
556 }
557 }
558 Py_INCREF(alias->parameters);
559 return alias->parameters;
560}
561
562static PyGetSetDef ga_properties[] = {
563 {"__parameters__", ga_parameters, (setter)NULL, "Type variables in the GenericAlias.", NULL},
564 {0}
565};
566
567static PyObject *
568ga_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
569{
Dong-hee Na02e44842020-04-24 01:25:53 +0900570 if (!_PyArg_NoKwnames("GenericAlias", kwds)) {
Guido van Rossum48b069a2020-04-07 09:50:06 -0700571 return NULL;
572 }
Dong-hee Na02e44842020-04-24 01:25:53 +0900573 if (!_PyArg_CheckPositional("GenericAlias", PyTuple_GET_SIZE(args), 2, 2)) {
Guido van Rossum48b069a2020-04-07 09:50:06 -0700574 return NULL;
575 }
576 PyObject *origin = PyTuple_GET_ITEM(args, 0);
577 PyObject *arguments = PyTuple_GET_ITEM(args, 1);
578 return Py_GenericAlias(origin, arguments);
579}
580
kj4eb41d02020-11-09 12:00:13 +0800581static PyNumberMethods ga_as_number = {
582 .nb_or = (binaryfunc)_Py_union_type_or, // Add __or__ function
583};
584
Guido van Rossum48b069a2020-04-07 09:50:06 -0700585// TODO:
586// - argument clinic?
587// - __doc__?
588// - cache?
589PyTypeObject Py_GenericAliasType = {
590 PyVarObject_HEAD_INIT(&PyType_Type, 0)
591 .tp_name = "types.GenericAlias",
592 .tp_doc = "Represent a PEP 585 generic type\n"
593 "\n"
Mikhail Golubev77f0a232020-10-09 00:38:36 +0300594 "E.g. for t = list[int], t.__origin__ is list and t.__args__ is (int,).",
Guido van Rossum48b069a2020-04-07 09:50:06 -0700595 .tp_basicsize = sizeof(gaobject),
596 .tp_dealloc = ga_dealloc,
597 .tp_repr = ga_repr,
kj4eb41d02020-11-09 12:00:13 +0800598 .tp_as_number = &ga_as_number, // allow X | Y of GenericAlias objs
Guido van Rossum48b069a2020-04-07 09:50:06 -0700599 .tp_as_mapping = &ga_as_mapping,
600 .tp_hash = ga_hash,
601 .tp_call = ga_call,
602 .tp_getattro = ga_getattro,
603 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
604 .tp_traverse = ga_traverse,
605 .tp_richcompare = ga_richcompare,
kj384b7a42020-11-16 11:27:23 +0800606 .tp_weaklistoffset = offsetof(gaobject, weakreflist),
Guido van Rossum48b069a2020-04-07 09:50:06 -0700607 .tp_methods = ga_methods,
608 .tp_members = ga_members,
609 .tp_alloc = PyType_GenericAlloc,
610 .tp_new = ga_new,
611 .tp_free = PyObject_GC_Del,
612 .tp_getset = ga_properties,
613};
614
615PyObject *
616Py_GenericAlias(PyObject *origin, PyObject *args)
617{
618 if (!PyTuple_Check(args)) {
619 args = PyTuple_Pack(1, args);
620 if (args == NULL) {
621 return NULL;
622 }
623 }
624 else {
625 Py_INCREF(args);
626 }
627
628 gaobject *alias = PyObject_GC_New(gaobject, &Py_GenericAliasType);
629 if (alias == NULL) {
630 Py_DECREF(args);
631 return NULL;
632 }
633
634 Py_INCREF(origin);
635 alias->origin = origin;
636 alias->args = args;
637 alias->parameters = NULL;
kj384b7a42020-11-16 11:27:23 +0800638 alias->weakreflist = NULL;
Guido van Rossum48b069a2020-04-07 09:50:06 -0700639 _PyObject_GC_TRACK(alias);
640 return (PyObject *)alias;
641}