blob: 659346aac8211b0598e32d973525093dfe260341 [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)85b58292021-07-18 04:59:25 -0700305 int r = is_unionable(self);
306 if (r > 0) {
307 r = is_unionable(other);
308 }
309 if (r < 0) {
310 return NULL;
311 }
312 if (!r) {
313 Py_RETURN_NOTIMPLEMENTED;
314 }
315
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700316 PyObject *tuple = PyTuple_Pack(2, self, other);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700317 if (tuple == NULL) {
318 return NULL;
319 }
Miss Islington (bot)85b58292021-07-18 04:59:25 -0700320
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700321 PyObject *new_union = make_union(tuple);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700322 Py_DECREF(tuple);
323 return new_union;
324}
325
326static int
327union_repr_item(_PyUnicodeWriter *writer, PyObject *p)
328{
329 _Py_IDENTIFIER(__module__);
330 _Py_IDENTIFIER(__qualname__);
331 _Py_IDENTIFIER(__origin__);
332 _Py_IDENTIFIER(__args__);
333 PyObject *qualname = NULL;
334 PyObject *module = NULL;
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300335 PyObject *tmp;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700336 PyObject *r = NULL;
337 int err;
338
Serhiy Storchaka6dec5252021-07-15 10:15:14 +0300339 if (p == (PyObject *)&_PyNone_Type) {
340 return _PyUnicodeWriter_WriteASCIIString(writer, "None", 4);
341 }
342
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300343 if (_PyObject_LookupAttrId(p, &PyId___origin__, &tmp) < 0) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700344 goto exit;
345 }
346
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300347 if (tmp) {
348 Py_DECREF(tmp);
349 if (_PyObject_LookupAttrId(p, &PyId___args__, &tmp) < 0) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700350 goto exit;
351 }
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300352 if (tmp) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700353 // It looks like a GenericAlias
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300354 Py_DECREF(tmp);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700355 goto use_repr;
356 }
357 }
358
359 if (_PyObject_LookupAttrId(p, &PyId___qualname__, &qualname) < 0) {
360 goto exit;
361 }
362 if (qualname == NULL) {
363 goto use_repr;
364 }
365 if (_PyObject_LookupAttrId(p, &PyId___module__, &module) < 0) {
366 goto exit;
367 }
368 if (module == NULL || module == Py_None) {
369 goto use_repr;
370 }
371
372 // Looks like a class
373 if (PyUnicode_Check(module) &&
374 _PyUnicode_EqualToASCIIString(module, "builtins"))
375 {
376 // builtins don't need a module name
377 r = PyObject_Str(qualname);
378 goto exit;
379 }
380 else {
381 r = PyUnicode_FromFormat("%S.%S", module, qualname);
382 goto exit;
383 }
384
385use_repr:
386 r = PyObject_Repr(p);
387exit:
388 Py_XDECREF(qualname);
389 Py_XDECREF(module);
390 if (r == NULL) {
391 return -1;
392 }
393 err = _PyUnicodeWriter_WriteStr(writer, r);
394 Py_DECREF(r);
395 return err;
396}
397
398static PyObject *
399union_repr(PyObject *self)
400{
401 unionobject *alias = (unionobject *)self;
402 Py_ssize_t len = PyTuple_GET_SIZE(alias->args);
403
404 _PyUnicodeWriter writer;
405 _PyUnicodeWriter_Init(&writer);
406 for (Py_ssize_t i = 0; i < len; i++) {
407 if (i > 0 && _PyUnicodeWriter_WriteASCIIString(&writer, " | ", 3) < 0) {
408 goto error;
409 }
410 PyObject *p = PyTuple_GET_ITEM(alias->args, i);
411 if (union_repr_item(&writer, p) < 0) {
412 goto error;
413 }
414 }
415 return _PyUnicodeWriter_Finish(&writer);
416error:
417 _PyUnicodeWriter_Dealloc(&writer);
418 return NULL;
419}
420
421static PyMemberDef union_members[] = {
422 {"__args__", T_OBJECT, offsetof(unionobject, args), READONLY},
423 {0}
424};
425
426static PyMethodDef union_methods[] = {
427 {"__instancecheck__", union_instancecheck, METH_O},
428 {"__subclasscheck__", union_subclasscheck, METH_O},
429 {0}};
430
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300431
432static PyObject *
433union_getitem(PyObject *self, PyObject *item)
434{
435 unionobject *alias = (unionobject *)self;
436 // Populate __parameters__ if needed.
437 if (alias->parameters == NULL) {
438 alias->parameters = _Py_make_parameters(alias->args);
439 if (alias->parameters == NULL) {
440 return NULL;
441 }
442 }
443
444 PyObject *newargs = _Py_subs_parameters(self, alias->args, alias->parameters, item);
445 if (newargs == NULL) {
446 return NULL;
447 }
448
Miss Islington (bot)21db59f2021-07-22 15:18:49 -0700449 PyObject *res;
Miss Islington (bot)85b58292021-07-18 04:59:25 -0700450 Py_ssize_t nargs = PyTuple_GET_SIZE(newargs);
Miss Islington (bot)21db59f2021-07-22 15:18:49 -0700451 if (nargs == 0) {
452 res = make_union(newargs);
453 }
454 else {
455 res = PyTuple_GET_ITEM(newargs, 0);
456 Py_INCREF(res);
457 for (Py_ssize_t iarg = 1; iarg < nargs; iarg++) {
458 PyObject *arg = PyTuple_GET_ITEM(newargs, iarg);
459 Py_SETREF(res, PyNumber_Or(res, arg));
460 if (res == NULL) {
461 break;
Miss Islington (bot)85b58292021-07-18 04:59:25 -0700462 }
Miss Islington (bot)85b58292021-07-18 04:59:25 -0700463 }
464 }
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300465 Py_DECREF(newargs);
466 return res;
467}
468
469static PyMappingMethods union_as_mapping = {
470 .mp_subscript = union_getitem,
471};
472
473static PyObject *
474union_parameters(PyObject *self, void *Py_UNUSED(unused))
475{
476 unionobject *alias = (unionobject *)self;
477 if (alias->parameters == NULL) {
478 alias->parameters = _Py_make_parameters(alias->args);
479 if (alias->parameters == NULL) {
480 return NULL;
481 }
482 }
483 Py_INCREF(alias->parameters);
484 return alias->parameters;
485}
486
487static PyGetSetDef union_properties[] = {
488 {"__parameters__", union_parameters, (setter)NULL, "Type variables in the types.Union.", NULL},
489 {0}
490};
491
Maggie Moss1b4552c2020-09-09 13:23:24 -0700492static PyNumberMethods union_as_number = {
kj4eb41d02020-11-09 12:00:13 +0800493 .nb_or = _Py_union_type_or, // Add __or__ function
Maggie Moss1b4552c2020-09-09 13:23:24 -0700494};
495
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700496PyTypeObject _PyUnion_Type = {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700497 PyVarObject_HEAD_INIT(&PyType_Type, 0)
498 .tp_name = "types.Union",
499 .tp_doc = "Represent a PEP 604 union type\n"
500 "\n"
501 "E.g. for int | str",
502 .tp_basicsize = sizeof(unionobject),
503 .tp_dealloc = unionobject_dealloc,
504 .tp_alloc = PyType_GenericAlloc,
Miss Islington (bot)08561342021-07-03 06:33:16 -0700505 .tp_free = PyObject_GC_Del,
506 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
507 .tp_traverse = union_traverse,
Maggie Moss1b4552c2020-09-09 13:23:24 -0700508 .tp_hash = union_hash,
509 .tp_getattro = PyObject_GenericGetAttr,
510 .tp_members = union_members,
511 .tp_methods = union_methods,
512 .tp_richcompare = union_richcompare,
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300513 .tp_as_mapping = &union_as_mapping,
Maggie Moss1b4552c2020-09-09 13:23:24 -0700514 .tp_as_number = &union_as_number,
515 .tp_repr = union_repr,
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300516 .tp_getset = union_properties,
Maggie Moss1b4552c2020-09-09 13:23:24 -0700517};
518
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700519static PyObject *
520make_union(PyObject *args)
Maggie Moss1b4552c2020-09-09 13:23:24 -0700521{
522 assert(PyTuple_CheckExact(args));
523
Serhiy Storchakac3007ab2021-07-16 14:48:20 +0300524 args = dedup_and_flatten_args(args);
525 if (args == NULL) {
526 return NULL;
527 }
528 if (PyTuple_GET_SIZE(args) == 1) {
529 PyObject *result1 = PyTuple_GET_ITEM(args, 0);
530 Py_INCREF(result1);
531 Py_DECREF(args);
532 return result1;
533 }
534
Miss Islington (bot)85b58292021-07-18 04:59:25 -0700535 unionobject *result = PyObject_GC_New(unionobject, &_PyUnion_Type);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700536 if (result == NULL) {
Serhiy Storchakac3007ab2021-07-16 14:48:20 +0300537 Py_DECREF(args);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700538 return NULL;
539 }
540
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300541 result->parameters = NULL;
Serhiy Storchakac3007ab2021-07-16 14:48:20 +0300542 result->args = args;
Miss Islington (bot)000b9e82021-07-03 13:51:10 -0700543 _PyObject_GC_TRACK(result);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700544 return (PyObject*)result;
545}