blob: 9804d87c66ccd2e46676122d90e73fe3c0cd47e5 [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
Maggie Moss1b4552c2020-09-09 13:23:24 -0700118static PyObject *
119union_richcompare(PyObject *a, PyObject *b, int op)
120{
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700121 if (!_PyUnion_Check(b) || (op != Py_EQ && op != Py_NE)) {
122 Py_RETURN_NOTIMPLEMENTED;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700123 }
124
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700125 PyObject *a_set = PySet_New(((unionobject*)a)->args);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700126 if (a_set == NULL) {
127 return NULL;
128 }
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700129 PyObject *b_set = PySet_New(((unionobject*)b)->args);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700130 if (b_set == NULL) {
Serhiy Storchakac3007ab2021-07-16 14:48:20 +0300131 Py_DECREF(a_set);
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700132 return NULL;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700133 }
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700134 PyObject *result = PyObject_RichCompare(a_set, b_set, op);
135 Py_DECREF(b_set);
136 Py_DECREF(a_set);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700137 return result;
138}
139
140static PyObject*
141flatten_args(PyObject* args)
142{
Victor Stinnerd67de0a2020-09-23 23:25:54 +0200143 Py_ssize_t arg_length = PyTuple_GET_SIZE(args);
144 Py_ssize_t total_args = 0;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700145 // Get number of total args once it's flattened.
146 for (Py_ssize_t i = 0; i < arg_length; i++) {
147 PyObject *arg = PyTuple_GET_ITEM(args, i);
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700148 if (_PyUnion_Check(arg)) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700149 total_args += PyTuple_GET_SIZE(((unionobject*) arg)->args);
150 } else {
151 total_args++;
152 }
153 }
154 // Create new tuple of flattened args.
155 PyObject *flattened_args = PyTuple_New(total_args);
156 if (flattened_args == NULL) {
157 return NULL;
158 }
159 Py_ssize_t pos = 0;
160 for (Py_ssize_t i = 0; i < arg_length; i++) {
161 PyObject *arg = PyTuple_GET_ITEM(args, i);
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700162 if (_PyUnion_Check(arg)) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700163 PyObject* nested_args = ((unionobject*)arg)->args;
Victor Stinnerd73cf7c2020-09-26 12:48:41 +0200164 Py_ssize_t nested_arg_length = PyTuple_GET_SIZE(nested_args);
165 for (Py_ssize_t j = 0; j < nested_arg_length; j++) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700166 PyObject* nested_arg = PyTuple_GET_ITEM(nested_args, j);
167 Py_INCREF(nested_arg);
168 PyTuple_SET_ITEM(flattened_args, pos, nested_arg);
169 pos++;
170 }
171 } else {
Serhiy Storchaka6dec5252021-07-15 10:15:14 +0300172 if (arg == Py_None) {
173 arg = (PyObject *)&_PyNone_Type;
174 }
Maggie Moss1b4552c2020-09-09 13:23:24 -0700175 Py_INCREF(arg);
176 PyTuple_SET_ITEM(flattened_args, pos, arg);
177 pos++;
178 }
179 }
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700180 assert(pos == total_args);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700181 return flattened_args;
182}
183
184static PyObject*
185dedup_and_flatten_args(PyObject* args)
186{
187 args = flatten_args(args);
188 if (args == NULL) {
189 return NULL;
190 }
191 Py_ssize_t arg_length = PyTuple_GET_SIZE(args);
192 PyObject *new_args = PyTuple_New(arg_length);
193 if (new_args == NULL) {
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700194 Py_DECREF(args);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700195 return NULL;
196 }
197 // Add unique elements to an array.
Victor Stinnerd73cf7c2020-09-26 12:48:41 +0200198 Py_ssize_t added_items = 0;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700199 for (Py_ssize_t i = 0; i < arg_length; i++) {
200 int is_duplicate = 0;
201 PyObject* i_element = PyTuple_GET_ITEM(args, i);
Serhiy Storchaka80844d12021-07-16 16:42:04 +0300202 for (Py_ssize_t j = 0; j < added_items; j++) {
203 PyObject* j_element = PyTuple_GET_ITEM(new_args, j);
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700204 int is_ga = _PyGenericAlias_Check(i_element) &&
205 _PyGenericAlias_Check(j_element);
kj4eb41d02020-11-09 12:00:13 +0800206 // RichCompare to also deduplicate GenericAlias types (slower)
207 is_duplicate = is_ga ? PyObject_RichCompareBool(i_element, j_element, Py_EQ)
208 : i_element == j_element;
209 // Should only happen if RichCompare fails
210 if (is_duplicate < 0) {
211 Py_DECREF(args);
212 Py_DECREF(new_args);
213 return NULL;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700214 }
kj4eb41d02020-11-09 12:00:13 +0800215 if (is_duplicate)
216 break;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700217 }
218 if (!is_duplicate) {
219 Py_INCREF(i_element);
220 PyTuple_SET_ITEM(new_args, added_items, i_element);
221 added_items++;
222 }
223 }
224 Py_DECREF(args);
225 _PyTuple_Resize(&new_args, added_items);
226 return new_args;
227}
228
229static int
Maggie Moss1b4552c2020-09-09 13:23:24 -0700230is_unionable(PyObject *obj)
231{
Ken Jinca5a4cf2021-07-24 22:49:25 +0800232 return (obj == Py_None ||
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700233 PyType_Check(obj) ||
234 _PyGenericAlias_Check(obj) ||
Ken Jinca5a4cf2021-07-24 22:49:25 +0800235 _PyUnion_Check(obj));
Maggie Moss1b4552c2020-09-09 13:23:24 -0700236}
237
Pablo Galindo Salgado9356d1e2021-07-24 15:08:53 +0100238static int
239is_args_unionable(PyObject *args)
240{
241 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
242 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
243 PyObject *arg = PyTuple_GET_ITEM(args, iarg);
Ken Jinca5a4cf2021-07-24 22:49:25 +0800244 if (!is_unionable(arg)) {
245 PyErr_Format(PyExc_TypeError,
246 "Each union argument must be a type, got %.100R", arg);
Pablo Galindo Salgado9356d1e2021-07-24 15:08:53 +0100247 return 0;
248 }
249 }
250 return 1;
251}
252
kj4eb41d02020-11-09 12:00:13 +0800253PyObject *
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700254_Py_union_type_or(PyObject* self, PyObject* other)
Maggie Moss1b4552c2020-09-09 13:23:24 -0700255{
Ken Jinca5a4cf2021-07-24 22:49:25 +0800256 if (!is_unionable(self) || !is_unionable(other)) {
Miss Islington (bot)85b58292021-07-18 04:59:25 -0700257 Py_RETURN_NOTIMPLEMENTED;
258 }
259
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700260 PyObject *tuple = PyTuple_Pack(2, self, other);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700261 if (tuple == NULL) {
262 return NULL;
263 }
Miss Islington (bot)85b58292021-07-18 04:59:25 -0700264
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700265 PyObject *new_union = make_union(tuple);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700266 Py_DECREF(tuple);
267 return new_union;
268}
269
270static int
271union_repr_item(_PyUnicodeWriter *writer, PyObject *p)
272{
273 _Py_IDENTIFIER(__module__);
274 _Py_IDENTIFIER(__qualname__);
275 _Py_IDENTIFIER(__origin__);
276 _Py_IDENTIFIER(__args__);
277 PyObject *qualname = NULL;
278 PyObject *module = NULL;
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300279 PyObject *tmp;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700280 PyObject *r = NULL;
281 int err;
282
Serhiy Storchaka6dec5252021-07-15 10:15:14 +0300283 if (p == (PyObject *)&_PyNone_Type) {
284 return _PyUnicodeWriter_WriteASCIIString(writer, "None", 4);
285 }
286
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300287 if (_PyObject_LookupAttrId(p, &PyId___origin__, &tmp) < 0) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700288 goto exit;
289 }
290
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300291 if (tmp) {
292 Py_DECREF(tmp);
293 if (_PyObject_LookupAttrId(p, &PyId___args__, &tmp) < 0) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700294 goto exit;
295 }
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300296 if (tmp) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700297 // It looks like a GenericAlias
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300298 Py_DECREF(tmp);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700299 goto use_repr;
300 }
301 }
302
303 if (_PyObject_LookupAttrId(p, &PyId___qualname__, &qualname) < 0) {
304 goto exit;
305 }
306 if (qualname == NULL) {
307 goto use_repr;
308 }
309 if (_PyObject_LookupAttrId(p, &PyId___module__, &module) < 0) {
310 goto exit;
311 }
312 if (module == NULL || module == Py_None) {
313 goto use_repr;
314 }
315
316 // Looks like a class
317 if (PyUnicode_Check(module) &&
318 _PyUnicode_EqualToASCIIString(module, "builtins"))
319 {
320 // builtins don't need a module name
321 r = PyObject_Str(qualname);
322 goto exit;
323 }
324 else {
325 r = PyUnicode_FromFormat("%S.%S", module, qualname);
326 goto exit;
327 }
328
329use_repr:
330 r = PyObject_Repr(p);
331exit:
332 Py_XDECREF(qualname);
333 Py_XDECREF(module);
334 if (r == NULL) {
335 return -1;
336 }
337 err = _PyUnicodeWriter_WriteStr(writer, r);
338 Py_DECREF(r);
339 return err;
340}
341
342static PyObject *
343union_repr(PyObject *self)
344{
345 unionobject *alias = (unionobject *)self;
346 Py_ssize_t len = PyTuple_GET_SIZE(alias->args);
347
348 _PyUnicodeWriter writer;
349 _PyUnicodeWriter_Init(&writer);
350 for (Py_ssize_t i = 0; i < len; i++) {
351 if (i > 0 && _PyUnicodeWriter_WriteASCIIString(&writer, " | ", 3) < 0) {
352 goto error;
353 }
354 PyObject *p = PyTuple_GET_ITEM(alias->args, i);
355 if (union_repr_item(&writer, p) < 0) {
356 goto error;
357 }
358 }
359 return _PyUnicodeWriter_Finish(&writer);
360error:
361 _PyUnicodeWriter_Dealloc(&writer);
362 return NULL;
363}
364
Pablo Galindo Salgado9356d1e2021-07-24 15:08:53 +0100365static PyObject *
366union_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
367{
368 unionobject *alias = (unionobject *)self;
369 PyObject* from_args = PyObject_GetAttrString(self, "_from_args");
370 if (from_args == NULL) {
371 return NULL;
372 }
373
374 return Py_BuildValue("N(O)", from_args, alias->args);
375}
376
Maggie Moss1b4552c2020-09-09 13:23:24 -0700377static PyMemberDef union_members[] = {
378 {"__args__", T_OBJECT, offsetof(unionobject, args), READONLY},
379 {0}
380};
381
Pablo Galindo Salgado9356d1e2021-07-24 15:08:53 +0100382static PyObject *
383union_from_args(PyObject *cls, PyObject *args)
384{
385 if (!PyTuple_CheckExact(args)) {
386 _PyArg_BadArgument("Union._from_args", "argument 'args'", "tuple", args);
387 return NULL;
388 }
389 if (!PyTuple_GET_SIZE(args)) {
390 PyErr_SetString(PyExc_ValueError, "args must be not empty");
391 return NULL;
392 }
393
Ken Jinca5a4cf2021-07-24 22:49:25 +0800394 if (!is_args_unionable(args)) {
Pablo Galindo Salgado9356d1e2021-07-24 15:08:53 +0100395 return NULL;
396 }
397
398 return make_union(args);
399}
400
Maggie Moss1b4552c2020-09-09 13:23:24 -0700401static PyMethodDef union_methods[] = {
Pablo Galindo Salgado9356d1e2021-07-24 15:08:53 +0100402 {"_from_args", union_from_args, METH_O | METH_CLASS},
Maggie Moss1b4552c2020-09-09 13:23:24 -0700403 {"__instancecheck__", union_instancecheck, METH_O},
404 {"__subclasscheck__", union_subclasscheck, METH_O},
Pablo Galindo Salgado9356d1e2021-07-24 15:08:53 +0100405 {"__reduce__", union_reduce, METH_NOARGS},
Maggie Moss1b4552c2020-09-09 13:23:24 -0700406 {0}};
407
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300408
409static PyObject *
410union_getitem(PyObject *self, PyObject *item)
411{
412 unionobject *alias = (unionobject *)self;
413 // Populate __parameters__ if needed.
414 if (alias->parameters == NULL) {
415 alias->parameters = _Py_make_parameters(alias->args);
416 if (alias->parameters == NULL) {
417 return NULL;
418 }
419 }
420
421 PyObject *newargs = _Py_subs_parameters(self, alias->args, alias->parameters, item);
422 if (newargs == NULL) {
423 return NULL;
424 }
425
Miss Islington (bot)21db59f2021-07-22 15:18:49 -0700426 PyObject *res;
Miss Islington (bot)85b58292021-07-18 04:59:25 -0700427 Py_ssize_t nargs = PyTuple_GET_SIZE(newargs);
Miss Islington (bot)21db59f2021-07-22 15:18:49 -0700428 if (nargs == 0) {
429 res = make_union(newargs);
430 }
431 else {
432 res = PyTuple_GET_ITEM(newargs, 0);
433 Py_INCREF(res);
434 for (Py_ssize_t iarg = 1; iarg < nargs; iarg++) {
435 PyObject *arg = PyTuple_GET_ITEM(newargs, iarg);
436 Py_SETREF(res, PyNumber_Or(res, arg));
437 if (res == NULL) {
438 break;
Miss Islington (bot)85b58292021-07-18 04:59:25 -0700439 }
Miss Islington (bot)85b58292021-07-18 04:59:25 -0700440 }
441 }
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300442 Py_DECREF(newargs);
443 return res;
444}
445
446static PyMappingMethods union_as_mapping = {
447 .mp_subscript = union_getitem,
448};
449
450static PyObject *
451union_parameters(PyObject *self, void *Py_UNUSED(unused))
452{
453 unionobject *alias = (unionobject *)self;
454 if (alias->parameters == NULL) {
455 alias->parameters = _Py_make_parameters(alias->args);
456 if (alias->parameters == NULL) {
457 return NULL;
458 }
459 }
460 Py_INCREF(alias->parameters);
461 return alias->parameters;
462}
463
464static PyGetSetDef union_properties[] = {
465 {"__parameters__", union_parameters, (setter)NULL, "Type variables in the types.Union.", NULL},
466 {0}
467};
468
Maggie Moss1b4552c2020-09-09 13:23:24 -0700469static PyNumberMethods union_as_number = {
kj4eb41d02020-11-09 12:00:13 +0800470 .nb_or = _Py_union_type_or, // Add __or__ function
Maggie Moss1b4552c2020-09-09 13:23:24 -0700471};
472
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700473PyTypeObject _PyUnion_Type = {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700474 PyVarObject_HEAD_INIT(&PyType_Type, 0)
475 .tp_name = "types.Union",
476 .tp_doc = "Represent a PEP 604 union type\n"
477 "\n"
478 "E.g. for int | str",
479 .tp_basicsize = sizeof(unionobject),
480 .tp_dealloc = unionobject_dealloc,
481 .tp_alloc = PyType_GenericAlloc,
Miss Islington (bot)08561342021-07-03 06:33:16 -0700482 .tp_free = PyObject_GC_Del,
483 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
484 .tp_traverse = union_traverse,
Maggie Moss1b4552c2020-09-09 13:23:24 -0700485 .tp_hash = union_hash,
486 .tp_getattro = PyObject_GenericGetAttr,
487 .tp_members = union_members,
488 .tp_methods = union_methods,
489 .tp_richcompare = union_richcompare,
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300490 .tp_as_mapping = &union_as_mapping,
Maggie Moss1b4552c2020-09-09 13:23:24 -0700491 .tp_as_number = &union_as_number,
492 .tp_repr = union_repr,
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300493 .tp_getset = union_properties,
Maggie Moss1b4552c2020-09-09 13:23:24 -0700494};
495
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700496static PyObject *
497make_union(PyObject *args)
Maggie Moss1b4552c2020-09-09 13:23:24 -0700498{
499 assert(PyTuple_CheckExact(args));
500
Serhiy Storchakac3007ab2021-07-16 14:48:20 +0300501 args = dedup_and_flatten_args(args);
502 if (args == NULL) {
503 return NULL;
504 }
505 if (PyTuple_GET_SIZE(args) == 1) {
506 PyObject *result1 = PyTuple_GET_ITEM(args, 0);
507 Py_INCREF(result1);
508 Py_DECREF(args);
509 return result1;
510 }
511
Miss Islington (bot)85b58292021-07-18 04:59:25 -0700512 unionobject *result = PyObject_GC_New(unionobject, &_PyUnion_Type);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700513 if (result == NULL) {
Serhiy Storchakac3007ab2021-07-16 14:48:20 +0300514 Py_DECREF(args);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700515 return NULL;
516 }
517
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300518 result->parameters = NULL;
Serhiy Storchakac3007ab2021-07-16 14:48:20 +0300519 result->args = args;
Miss Islington (bot)000b9e82021-07-03 13:51:10 -0700520 _PyObject_GC_TRACK(result);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700521 return (PyObject*)result;
522}