blob: c744c8746cb4d7c52cb5c098939b5d217eded97c [file] [log] [blame]
Maggie Moss1b4552c2020-09-09 13:23:24 -07001// types.Union -- used to represent e.g. Union[int, str], int | str
2#include "Python.h"
Miss Islington (bot)08561342021-07-03 06:33:16 -07003#include "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK
Maggie Moss1b4552c2020-09-09 13:23:24 -07004#include "pycore_unionobject.h"
5#include "structmember.h"
6
7
Miss Islington (bot)03aad302021-07-17 14:10:21 -07008static PyObject *make_union(PyObject *);
9
10
Maggie Moss1b4552c2020-09-09 13:23:24 -070011typedef struct {
12 PyObject_HEAD
13 PyObject *args;
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +030014 PyObject *parameters;
Maggie Moss1b4552c2020-09-09 13:23:24 -070015} unionobject;
16
17static void
18unionobject_dealloc(PyObject *self)
19{
20 unionobject *alias = (unionobject *)self;
21
Miss Islington (bot)08561342021-07-03 06:33:16 -070022 _PyObject_GC_UNTRACK(self);
23
Maggie Moss1b4552c2020-09-09 13:23:24 -070024 Py_XDECREF(alias->args);
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +030025 Py_XDECREF(alias->parameters);
Neil Schemenauer0564aaf2020-10-27 11:55:52 -070026 Py_TYPE(self)->tp_free(self);
Maggie Moss1b4552c2020-09-09 13:23:24 -070027}
28
Miss Islington (bot)08561342021-07-03 06:33:16 -070029static int
30union_traverse(PyObject *self, visitproc visit, void *arg)
31{
32 unionobject *alias = (unionobject *)self;
33 Py_VISIT(alias->args);
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +030034 Py_VISIT(alias->parameters);
Miss Islington (bot)08561342021-07-03 06:33:16 -070035 return 0;
36}
37
Maggie Moss1b4552c2020-09-09 13:23:24 -070038static Py_hash_t
39union_hash(PyObject *self)
40{
41 unionobject *alias = (unionobject *)self;
Miss Islington (bot)70598802021-07-16 02:02:59 -070042 PyObject *args = PyFrozenSet_New(alias->args);
43 if (args == NULL) {
44 return (Py_hash_t)-1;
Maggie Moss1b4552c2020-09-09 13:23:24 -070045 }
Miss Islington (bot)70598802021-07-16 02:02:59 -070046 Py_hash_t hash = PyObject_Hash(args);
47 Py_DECREF(args);
48 return hash;
Maggie Moss1b4552c2020-09-09 13:23:24 -070049}
50
51static int
Miss Islington (bot)03aad302021-07-17 14:10:21 -070052is_generic_alias_in_args(PyObject *args)
53{
Maggie Moss1b4552c2020-09-09 13:23:24 -070054 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
55 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
56 PyObject *arg = PyTuple_GET_ITEM(args, iarg);
Miss Islington (bot)03aad302021-07-17 14:10:21 -070057 if (_PyGenericAlias_Check(arg)) {
Maggie Moss1b4552c2020-09-09 13:23:24 -070058 return 0;
59 }
60 }
61 return 1;
62}
63
64static PyObject *
65union_instancecheck(PyObject *self, PyObject *instance)
66{
67 unionobject *alias = (unionobject *) self;
68 Py_ssize_t nargs = PyTuple_GET_SIZE(alias->args);
69 if (!is_generic_alias_in_args(alias->args)) {
70 PyErr_SetString(PyExc_TypeError,
71 "isinstance() argument 2 cannot contain a parameterized generic");
72 return NULL;
73 }
74 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
75 PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg);
Miss Islington (bot)b42eee72021-07-13 21:55:45 -070076 if (PyType_Check(arg)) {
77 int res = PyObject_IsInstance(instance, arg);
78 if (res < 0) {
79 return NULL;
80 }
81 if (res) {
82 Py_RETURN_TRUE;
83 }
Maggie Moss1b4552c2020-09-09 13:23:24 -070084 }
85 }
86 Py_RETURN_FALSE;
87}
88
89static PyObject *
90union_subclasscheck(PyObject *self, PyObject *instance)
91{
92 if (!PyType_Check(instance)) {
93 PyErr_SetString(PyExc_TypeError, "issubclass() arg 1 must be a class");
94 return NULL;
95 }
96 unionobject *alias = (unionobject *)self;
97 if (!is_generic_alias_in_args(alias->args)) {
98 PyErr_SetString(PyExc_TypeError,
99 "issubclass() argument 2 cannot contain a parameterized generic");
100 return NULL;
101 }
102 Py_ssize_t nargs = PyTuple_GET_SIZE(alias->args);
103 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
104 PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg);
Miss Islington (bot)b42eee72021-07-13 21:55:45 -0700105 if (PyType_Check(arg)) {
106 int res = PyObject_IsSubclass(instance, arg);
107 if (res < 0) {
108 return NULL;
109 }
110 if (res) {
111 Py_RETURN_TRUE;
112 }
Maggie Moss1b4552c2020-09-09 13:23:24 -0700113 }
114 }
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700115 Py_RETURN_FALSE;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700116}
117
118static int
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700119is_typing_module(PyObject *obj)
120{
121 _Py_IDENTIFIER(__module__);
122 PyObject *module;
123 if (_PyObject_LookupAttrId(obj, &PyId___module__, &module) < 0) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700124 return -1;
125 }
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700126 int is_typing = (module != NULL &&
127 PyUnicode_Check(module) &&
128 _PyUnicode_EqualToASCIIString(module, "typing"));
129 Py_XDECREF(module);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700130 return is_typing;
131}
132
133static int
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700134is_typing_name(PyObject *obj, const char *name)
Maggie Moss1b4552c2020-09-09 13:23:24 -0700135{
136 PyTypeObject *type = Py_TYPE(obj);
137 if (strcmp(type->tp_name, name) != 0) {
138 return 0;
139 }
Miss Islington (bot)cc1a47c2021-07-15 00:25:22 -0700140 return is_typing_module((PyObject *)type);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700141}
142
143static PyObject *
144union_richcompare(PyObject *a, PyObject *b, int op)
145{
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700146 if (!_PyUnion_Check(b) || (op != Py_EQ && op != Py_NE)) {
147 Py_RETURN_NOTIMPLEMENTED;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700148 }
149
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700150 PyObject *a_set = PySet_New(((unionobject*)a)->args);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700151 if (a_set == NULL) {
152 return NULL;
153 }
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700154 PyObject *b_set = PySet_New(((unionobject*)b)->args);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700155 if (b_set == NULL) {
Serhiy Storchakac3007ab2021-07-16 14:48:20 +0300156 Py_DECREF(a_set);
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700157 return NULL;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700158 }
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700159 PyObject *result = PyObject_RichCompare(a_set, b_set, op);
160 Py_DECREF(b_set);
161 Py_DECREF(a_set);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700162 return result;
163}
164
165static PyObject*
166flatten_args(PyObject* args)
167{
Victor Stinnerd67de0a2020-09-23 23:25:54 +0200168 Py_ssize_t arg_length = PyTuple_GET_SIZE(args);
169 Py_ssize_t total_args = 0;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700170 // Get number of total args once it's flattened.
171 for (Py_ssize_t i = 0; i < arg_length; i++) {
172 PyObject *arg = PyTuple_GET_ITEM(args, i);
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700173 if (_PyUnion_Check(arg)) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700174 total_args += PyTuple_GET_SIZE(((unionobject*) arg)->args);
175 } else {
176 total_args++;
177 }
178 }
179 // Create new tuple of flattened args.
180 PyObject *flattened_args = PyTuple_New(total_args);
181 if (flattened_args == NULL) {
182 return NULL;
183 }
184 Py_ssize_t pos = 0;
185 for (Py_ssize_t i = 0; i < arg_length; i++) {
186 PyObject *arg = PyTuple_GET_ITEM(args, i);
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700187 if (_PyUnion_Check(arg)) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700188 PyObject* nested_args = ((unionobject*)arg)->args;
Victor Stinnerd73cf7c2020-09-26 12:48:41 +0200189 Py_ssize_t nested_arg_length = PyTuple_GET_SIZE(nested_args);
190 for (Py_ssize_t j = 0; j < nested_arg_length; j++) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700191 PyObject* nested_arg = PyTuple_GET_ITEM(nested_args, j);
192 Py_INCREF(nested_arg);
193 PyTuple_SET_ITEM(flattened_args, pos, nested_arg);
194 pos++;
195 }
196 } else {
Serhiy Storchaka6dec5252021-07-15 10:15:14 +0300197 if (arg == Py_None) {
198 arg = (PyObject *)&_PyNone_Type;
199 }
Maggie Moss1b4552c2020-09-09 13:23:24 -0700200 Py_INCREF(arg);
201 PyTuple_SET_ITEM(flattened_args, pos, arg);
202 pos++;
203 }
204 }
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700205 assert(pos == total_args);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700206 return flattened_args;
207}
208
209static PyObject*
210dedup_and_flatten_args(PyObject* args)
211{
212 args = flatten_args(args);
213 if (args == NULL) {
214 return NULL;
215 }
216 Py_ssize_t arg_length = PyTuple_GET_SIZE(args);
217 PyObject *new_args = PyTuple_New(arg_length);
218 if (new_args == NULL) {
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700219 Py_DECREF(args);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700220 return NULL;
221 }
222 // Add unique elements to an array.
Victor Stinnerd73cf7c2020-09-26 12:48:41 +0200223 Py_ssize_t added_items = 0;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700224 for (Py_ssize_t i = 0; i < arg_length; i++) {
225 int is_duplicate = 0;
226 PyObject* i_element = PyTuple_GET_ITEM(args, i);
Serhiy Storchaka80844d12021-07-16 16:42:04 +0300227 for (Py_ssize_t j = 0; j < added_items; j++) {
228 PyObject* j_element = PyTuple_GET_ITEM(new_args, j);
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700229 int is_ga = _PyGenericAlias_Check(i_element) &&
230 _PyGenericAlias_Check(j_element);
kj4eb41d02020-11-09 12:00:13 +0800231 // RichCompare to also deduplicate GenericAlias types (slower)
232 is_duplicate = is_ga ? PyObject_RichCompareBool(i_element, j_element, Py_EQ)
233 : i_element == j_element;
234 // Should only happen if RichCompare fails
235 if (is_duplicate < 0) {
236 Py_DECREF(args);
237 Py_DECREF(new_args);
238 return NULL;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700239 }
kj4eb41d02020-11-09 12:00:13 +0800240 if (is_duplicate)
241 break;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700242 }
243 if (!is_duplicate) {
244 Py_INCREF(i_element);
245 PyTuple_SET_ITEM(new_args, added_items, i_element);
246 added_items++;
247 }
248 }
249 Py_DECREF(args);
250 _PyTuple_Resize(&new_args, added_items);
251 return new_args;
252}
253
254static int
255is_typevar(PyObject *obj)
256{
257 return is_typing_name(obj, "TypeVar");
258}
259
260static int
261is_special_form(PyObject *obj)
262{
263 return is_typing_name(obj, "_SpecialForm");
264}
265
266static int
267is_new_type(PyObject *obj)
268{
269 PyTypeObject *type = Py_TYPE(obj);
270 if (type != &PyFunction_Type) {
271 return 0;
272 }
273 return is_typing_module(obj);
274}
275
Miss Islington (bot)7e6cad72021-06-23 02:38:49 -0700276// Emulates short-circuiting behavior of the ``||`` operator
277// while also checking negative values.
278#define CHECK_RES(res) { \
279 int result = res; \
280 if (result) { \
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700281 return result; \
Miss Islington (bot)7e6cad72021-06-23 02:38:49 -0700282 } \
283}
284
285// Returns 1 on true, 0 on false, and -1 on error.
Maggie Moss1b4552c2020-09-09 13:23:24 -0700286static int
287is_unionable(PyObject *obj)
288{
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700289 if (obj == Py_None ||
290 PyType_Check(obj) ||
291 _PyGenericAlias_Check(obj) ||
292 _PyUnion_Check(obj))
293 {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700294 return 1;
295 }
Miss Islington (bot)7e6cad72021-06-23 02:38:49 -0700296 CHECK_RES(is_typevar(obj));
297 CHECK_RES(is_new_type(obj));
298 CHECK_RES(is_special_form(obj));
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700299 return 0;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700300}
301
kj4eb41d02020-11-09 12:00:13 +0800302PyObject *
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700303_Py_union_type_or(PyObject* self, PyObject* other)
Maggie Moss1b4552c2020-09-09 13:23:24 -0700304{
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700305 PyObject *tuple = PyTuple_Pack(2, self, other);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700306 if (tuple == NULL) {
307 return NULL;
308 }
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700309 PyObject *new_union = make_union(tuple);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700310 Py_DECREF(tuple);
311 return new_union;
312}
313
314static int
315union_repr_item(_PyUnicodeWriter *writer, PyObject *p)
316{
317 _Py_IDENTIFIER(__module__);
318 _Py_IDENTIFIER(__qualname__);
319 _Py_IDENTIFIER(__origin__);
320 _Py_IDENTIFIER(__args__);
321 PyObject *qualname = NULL;
322 PyObject *module = NULL;
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300323 PyObject *tmp;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700324 PyObject *r = NULL;
325 int err;
326
Serhiy Storchaka6dec5252021-07-15 10:15:14 +0300327 if (p == (PyObject *)&_PyNone_Type) {
328 return _PyUnicodeWriter_WriteASCIIString(writer, "None", 4);
329 }
330
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300331 if (_PyObject_LookupAttrId(p, &PyId___origin__, &tmp) < 0) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700332 goto exit;
333 }
334
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300335 if (tmp) {
336 Py_DECREF(tmp);
337 if (_PyObject_LookupAttrId(p, &PyId___args__, &tmp) < 0) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700338 goto exit;
339 }
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300340 if (tmp) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700341 // It looks like a GenericAlias
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300342 Py_DECREF(tmp);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700343 goto use_repr;
344 }
345 }
346
347 if (_PyObject_LookupAttrId(p, &PyId___qualname__, &qualname) < 0) {
348 goto exit;
349 }
350 if (qualname == NULL) {
351 goto use_repr;
352 }
353 if (_PyObject_LookupAttrId(p, &PyId___module__, &module) < 0) {
354 goto exit;
355 }
356 if (module == NULL || module == Py_None) {
357 goto use_repr;
358 }
359
360 // Looks like a class
361 if (PyUnicode_Check(module) &&
362 _PyUnicode_EqualToASCIIString(module, "builtins"))
363 {
364 // builtins don't need a module name
365 r = PyObject_Str(qualname);
366 goto exit;
367 }
368 else {
369 r = PyUnicode_FromFormat("%S.%S", module, qualname);
370 goto exit;
371 }
372
373use_repr:
374 r = PyObject_Repr(p);
375exit:
376 Py_XDECREF(qualname);
377 Py_XDECREF(module);
378 if (r == NULL) {
379 return -1;
380 }
381 err = _PyUnicodeWriter_WriteStr(writer, r);
382 Py_DECREF(r);
383 return err;
384}
385
386static PyObject *
387union_repr(PyObject *self)
388{
389 unionobject *alias = (unionobject *)self;
390 Py_ssize_t len = PyTuple_GET_SIZE(alias->args);
391
392 _PyUnicodeWriter writer;
393 _PyUnicodeWriter_Init(&writer);
394 for (Py_ssize_t i = 0; i < len; i++) {
395 if (i > 0 && _PyUnicodeWriter_WriteASCIIString(&writer, " | ", 3) < 0) {
396 goto error;
397 }
398 PyObject *p = PyTuple_GET_ITEM(alias->args, i);
399 if (union_repr_item(&writer, p) < 0) {
400 goto error;
401 }
402 }
403 return _PyUnicodeWriter_Finish(&writer);
404error:
405 _PyUnicodeWriter_Dealloc(&writer);
406 return NULL;
407}
408
409static PyMemberDef union_members[] = {
410 {"__args__", T_OBJECT, offsetof(unionobject, args), READONLY},
411 {0}
412};
413
414static PyMethodDef union_methods[] = {
415 {"__instancecheck__", union_instancecheck, METH_O},
416 {"__subclasscheck__", union_subclasscheck, METH_O},
417 {0}};
418
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300419
420static PyObject *
421union_getitem(PyObject *self, PyObject *item)
422{
423 unionobject *alias = (unionobject *)self;
424 // Populate __parameters__ if needed.
425 if (alias->parameters == NULL) {
426 alias->parameters = _Py_make_parameters(alias->args);
427 if (alias->parameters == NULL) {
428 return NULL;
429 }
430 }
431
432 PyObject *newargs = _Py_subs_parameters(self, alias->args, alias->parameters, item);
433 if (newargs == NULL) {
434 return NULL;
435 }
436
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700437 PyObject *res = make_union(newargs);
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300438
439 Py_DECREF(newargs);
440 return res;
441}
442
443static PyMappingMethods union_as_mapping = {
444 .mp_subscript = union_getitem,
445};
446
447static PyObject *
448union_parameters(PyObject *self, void *Py_UNUSED(unused))
449{
450 unionobject *alias = (unionobject *)self;
451 if (alias->parameters == NULL) {
452 alias->parameters = _Py_make_parameters(alias->args);
453 if (alias->parameters == NULL) {
454 return NULL;
455 }
456 }
457 Py_INCREF(alias->parameters);
458 return alias->parameters;
459}
460
461static PyGetSetDef union_properties[] = {
462 {"__parameters__", union_parameters, (setter)NULL, "Type variables in the types.Union.", NULL},
463 {0}
464};
465
Maggie Moss1b4552c2020-09-09 13:23:24 -0700466static PyNumberMethods union_as_number = {
kj4eb41d02020-11-09 12:00:13 +0800467 .nb_or = _Py_union_type_or, // Add __or__ function
Maggie Moss1b4552c2020-09-09 13:23:24 -0700468};
469
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700470PyTypeObject _PyUnion_Type = {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700471 PyVarObject_HEAD_INIT(&PyType_Type, 0)
472 .tp_name = "types.Union",
473 .tp_doc = "Represent a PEP 604 union type\n"
474 "\n"
475 "E.g. for int | str",
476 .tp_basicsize = sizeof(unionobject),
477 .tp_dealloc = unionobject_dealloc,
478 .tp_alloc = PyType_GenericAlloc,
Miss Islington (bot)08561342021-07-03 06:33:16 -0700479 .tp_free = PyObject_GC_Del,
480 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
481 .tp_traverse = union_traverse,
Maggie Moss1b4552c2020-09-09 13:23:24 -0700482 .tp_hash = union_hash,
483 .tp_getattro = PyObject_GenericGetAttr,
484 .tp_members = union_members,
485 .tp_methods = union_methods,
486 .tp_richcompare = union_richcompare,
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300487 .tp_as_mapping = &union_as_mapping,
Maggie Moss1b4552c2020-09-09 13:23:24 -0700488 .tp_as_number = &union_as_number,
489 .tp_repr = union_repr,
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300490 .tp_getset = union_properties,
Maggie Moss1b4552c2020-09-09 13:23:24 -0700491};
492
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700493static PyObject *
494make_union(PyObject *args)
Maggie Moss1b4552c2020-09-09 13:23:24 -0700495{
496 assert(PyTuple_CheckExact(args));
497
498 unionobject* result = NULL;
499
500 // Check arguments are unionable.
Victor Stinnerd67de0a2020-09-23 23:25:54 +0200501 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700502 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
503 PyObject *arg = PyTuple_GET_ITEM(args, iarg);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700504 int is_arg_unionable = is_unionable(arg);
505 if (is_arg_unionable < 0) {
506 return NULL;
507 }
508 if (!is_arg_unionable) {
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700509 Py_RETURN_NOTIMPLEMENTED;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700510 }
511 }
512
Serhiy Storchakac3007ab2021-07-16 14:48:20 +0300513 args = dedup_and_flatten_args(args);
514 if (args == NULL) {
515 return NULL;
516 }
517 if (PyTuple_GET_SIZE(args) == 1) {
518 PyObject *result1 = PyTuple_GET_ITEM(args, 0);
519 Py_INCREF(result1);
520 Py_DECREF(args);
521 return result1;
522 }
523
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700524 result = PyObject_GC_New(unionobject, &_PyUnion_Type);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700525 if (result == NULL) {
Serhiy Storchakac3007ab2021-07-16 14:48:20 +0300526 Py_DECREF(args);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700527 return NULL;
528 }
529
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300530 result->parameters = NULL;
Serhiy Storchakac3007ab2021-07-16 14:48:20 +0300531 result->args = args;
Miss Islington (bot)000b9e82021-07-03 13:51:10 -0700532 _PyObject_GC_TRACK(result);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700533 return (PyObject*)result;
534}