blob: 229b518ea757569e740ae320af76d98f979252ca [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
8typedef struct {
9 PyObject_HEAD
10 PyObject *args;
11} unionobject;
12
13static void
14unionobject_dealloc(PyObject *self)
15{
16 unionobject *alias = (unionobject *)self;
17
Miss Islington (bot)08561342021-07-03 06:33:16 -070018 _PyObject_GC_UNTRACK(self);
19
Maggie Moss1b4552c2020-09-09 13:23:24 -070020 Py_XDECREF(alias->args);
Neil Schemenauer0564aaf2020-10-27 11:55:52 -070021 Py_TYPE(self)->tp_free(self);
Maggie Moss1b4552c2020-09-09 13:23:24 -070022}
23
Miss Islington (bot)08561342021-07-03 06:33:16 -070024static int
25union_traverse(PyObject *self, visitproc visit, void *arg)
26{
27 unionobject *alias = (unionobject *)self;
28 Py_VISIT(alias->args);
29 return 0;
30}
31
Maggie Moss1b4552c2020-09-09 13:23:24 -070032static Py_hash_t
33union_hash(PyObject *self)
34{
35 unionobject *alias = (unionobject *)self;
36 Py_hash_t h1 = PyObject_Hash(alias->args);
37 if (h1 == -1) {
38 return -1;
39 }
40 return h1;
41}
42
43static int
44is_generic_alias_in_args(PyObject *args) {
45 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
46 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
47 PyObject *arg = PyTuple_GET_ITEM(args, iarg);
Ken Jin49cd68f2021-01-03 00:19:15 +080048 if (PyObject_TypeCheck(arg, &Py_GenericAliasType)) {
Maggie Moss1b4552c2020-09-09 13:23:24 -070049 return 0;
50 }
51 }
52 return 1;
53}
54
55static PyObject *
56union_instancecheck(PyObject *self, PyObject *instance)
57{
58 unionobject *alias = (unionobject *) self;
59 Py_ssize_t nargs = PyTuple_GET_SIZE(alias->args);
60 if (!is_generic_alias_in_args(alias->args)) {
61 PyErr_SetString(PyExc_TypeError,
62 "isinstance() argument 2 cannot contain a parameterized generic");
63 return NULL;
64 }
65 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
66 PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg);
Miss Islington (bot)b42eee72021-07-13 21:55:45 -070067 if (PyType_Check(arg)) {
68 int res = PyObject_IsInstance(instance, arg);
69 if (res < 0) {
70 return NULL;
71 }
72 if (res) {
73 Py_RETURN_TRUE;
74 }
Maggie Moss1b4552c2020-09-09 13:23:24 -070075 }
76 }
77 Py_RETURN_FALSE;
78}
79
80static PyObject *
81union_subclasscheck(PyObject *self, PyObject *instance)
82{
83 if (!PyType_Check(instance)) {
84 PyErr_SetString(PyExc_TypeError, "issubclass() arg 1 must be a class");
85 return NULL;
86 }
87 unionobject *alias = (unionobject *)self;
88 if (!is_generic_alias_in_args(alias->args)) {
89 PyErr_SetString(PyExc_TypeError,
90 "issubclass() argument 2 cannot contain a parameterized generic");
91 return NULL;
92 }
93 Py_ssize_t nargs = PyTuple_GET_SIZE(alias->args);
94 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
95 PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg);
Miss Islington (bot)b42eee72021-07-13 21:55:45 -070096 if (PyType_Check(arg)) {
97 int res = PyObject_IsSubclass(instance, arg);
98 if (res < 0) {
99 return NULL;
100 }
101 if (res) {
102 Py_RETURN_TRUE;
103 }
Maggie Moss1b4552c2020-09-09 13:23:24 -0700104 }
105 }
106 Py_RETURN_FALSE;
107}
108
109static int
110is_typing_module(PyObject *obj) {
111 PyObject *module = PyObject_GetAttrString(obj, "__module__");
112 if (module == NULL) {
113 return -1;
114 }
115 int is_typing = PyUnicode_Check(module) && _PyUnicode_EqualToASCIIString(module, "typing");
116 Py_DECREF(module);
117 return is_typing;
118}
119
120static int
121is_typing_name(PyObject *obj, char *name)
122{
123 PyTypeObject *type = Py_TYPE(obj);
124 if (strcmp(type->tp_name, name) != 0) {
125 return 0;
126 }
Miss Islington (bot)cc1a47c2021-07-15 00:25:22 -0700127 return is_typing_module((PyObject *)type);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700128}
129
130static PyObject *
131union_richcompare(PyObject *a, PyObject *b, int op)
132{
133 PyObject *result = NULL;
134 if (op != Py_EQ && op != Py_NE) {
135 result = Py_NotImplemented;
136 Py_INCREF(result);
137 return result;
138 }
139
140 PyTypeObject *type = Py_TYPE(b);
141
142 PyObject* a_set = PySet_New(((unionobject*)a)->args);
143 if (a_set == NULL) {
144 return NULL;
145 }
146 PyObject* b_set = PySet_New(NULL);
147 if (b_set == NULL) {
148 goto exit;
149 }
150
151 // Populate b_set with the data from the right object
152 int is_typing_union = is_typing_name(b, "_UnionGenericAlias");
153 if (is_typing_union < 0) {
154 goto exit;
155 }
156 if (is_typing_union) {
157 PyObject *b_args = PyObject_GetAttrString(b, "__args__");
158 if (b_args == NULL) {
159 goto exit;
160 }
161 if (!PyTuple_CheckExact(b_args)) {
162 Py_DECREF(b_args);
163 PyErr_SetString(PyExc_TypeError, "__args__ argument of typing.Union object is not a tuple");
164 goto exit;
165 }
166 Py_ssize_t b_arg_length = PyTuple_GET_SIZE(b_args);
167 for (Py_ssize_t i = 0; i < b_arg_length; i++) {
168 PyObject* arg = PyTuple_GET_ITEM(b_args, i);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700169 if (PySet_Add(b_set, arg) == -1) {
170 Py_DECREF(b_args);
171 goto exit;
172 }
173 }
174 Py_DECREF(b_args);
175 } else if (type == &_Py_UnionType) {
176 PyObject* args = ((unionobject*) b)->args;
177 Py_ssize_t arg_length = PyTuple_GET_SIZE(args);
178 for (Py_ssize_t i = 0; i < arg_length; i++) {
179 PyObject* arg = PyTuple_GET_ITEM(args, i);
180 if (PySet_Add(b_set, arg) == -1) {
181 goto exit;
182 }
183 }
184 } else {
185 if (PySet_Add(b_set, b) == -1) {
186 goto exit;
187 }
188 }
189 result = PyObject_RichCompare(a_set, b_set, op);
190exit:
191 Py_XDECREF(a_set);
192 Py_XDECREF(b_set);
193 return result;
194}
195
196static PyObject*
197flatten_args(PyObject* args)
198{
Victor Stinnerd67de0a2020-09-23 23:25:54 +0200199 Py_ssize_t arg_length = PyTuple_GET_SIZE(args);
200 Py_ssize_t total_args = 0;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700201 // Get number of total args once it's flattened.
202 for (Py_ssize_t i = 0; i < arg_length; i++) {
203 PyObject *arg = PyTuple_GET_ITEM(args, i);
204 PyTypeObject* arg_type = Py_TYPE(arg);
205 if (arg_type == &_Py_UnionType) {
206 total_args += PyTuple_GET_SIZE(((unionobject*) arg)->args);
207 } else {
208 total_args++;
209 }
210 }
211 // Create new tuple of flattened args.
212 PyObject *flattened_args = PyTuple_New(total_args);
213 if (flattened_args == NULL) {
214 return NULL;
215 }
216 Py_ssize_t pos = 0;
217 for (Py_ssize_t i = 0; i < arg_length; i++) {
218 PyObject *arg = PyTuple_GET_ITEM(args, i);
219 PyTypeObject* arg_type = Py_TYPE(arg);
220 if (arg_type == &_Py_UnionType) {
221 PyObject* nested_args = ((unionobject*)arg)->args;
Victor Stinnerd73cf7c2020-09-26 12:48:41 +0200222 Py_ssize_t nested_arg_length = PyTuple_GET_SIZE(nested_args);
223 for (Py_ssize_t j = 0; j < nested_arg_length; j++) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700224 PyObject* nested_arg = PyTuple_GET_ITEM(nested_args, j);
225 Py_INCREF(nested_arg);
226 PyTuple_SET_ITEM(flattened_args, pos, nested_arg);
227 pos++;
228 }
229 } else {
Serhiy Storchaka6dec5252021-07-15 10:15:14 +0300230 if (arg == Py_None) {
231 arg = (PyObject *)&_PyNone_Type;
232 }
Maggie Moss1b4552c2020-09-09 13:23:24 -0700233 Py_INCREF(arg);
234 PyTuple_SET_ITEM(flattened_args, pos, arg);
235 pos++;
236 }
237 }
238 return flattened_args;
239}
240
241static PyObject*
242dedup_and_flatten_args(PyObject* args)
243{
244 args = flatten_args(args);
245 if (args == NULL) {
246 return NULL;
247 }
248 Py_ssize_t arg_length = PyTuple_GET_SIZE(args);
249 PyObject *new_args = PyTuple_New(arg_length);
250 if (new_args == NULL) {
251 return NULL;
252 }
253 // Add unique elements to an array.
Victor Stinnerd73cf7c2020-09-26 12:48:41 +0200254 Py_ssize_t added_items = 0;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700255 for (Py_ssize_t i = 0; i < arg_length; i++) {
256 int is_duplicate = 0;
257 PyObject* i_element = PyTuple_GET_ITEM(args, i);
258 for (Py_ssize_t j = i + 1; j < arg_length; j++) {
259 PyObject* j_element = PyTuple_GET_ITEM(args, j);
kj463c7d32020-12-14 02:38:24 +0800260 int is_ga = PyObject_TypeCheck(i_element, &Py_GenericAliasType) &&
261 PyObject_TypeCheck(j_element, &Py_GenericAliasType);
kj4eb41d02020-11-09 12:00:13 +0800262 // RichCompare to also deduplicate GenericAlias types (slower)
263 is_duplicate = is_ga ? PyObject_RichCompareBool(i_element, j_element, Py_EQ)
264 : i_element == j_element;
265 // Should only happen if RichCompare fails
266 if (is_duplicate < 0) {
267 Py_DECREF(args);
268 Py_DECREF(new_args);
269 return NULL;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700270 }
kj4eb41d02020-11-09 12:00:13 +0800271 if (is_duplicate)
272 break;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700273 }
274 if (!is_duplicate) {
275 Py_INCREF(i_element);
276 PyTuple_SET_ITEM(new_args, added_items, i_element);
277 added_items++;
278 }
279 }
280 Py_DECREF(args);
281 _PyTuple_Resize(&new_args, added_items);
282 return new_args;
283}
284
285static int
286is_typevar(PyObject *obj)
287{
288 return is_typing_name(obj, "TypeVar");
289}
290
291static int
292is_special_form(PyObject *obj)
293{
294 return is_typing_name(obj, "_SpecialForm");
295}
296
297static int
298is_new_type(PyObject *obj)
299{
300 PyTypeObject *type = Py_TYPE(obj);
301 if (type != &PyFunction_Type) {
302 return 0;
303 }
304 return is_typing_module(obj);
305}
306
Miss Islington (bot)7e6cad72021-06-23 02:38:49 -0700307// Emulates short-circuiting behavior of the ``||`` operator
308// while also checking negative values.
309#define CHECK_RES(res) { \
310 int result = res; \
311 if (result) { \
312 return result; \
313 } \
314}
315
316// Returns 1 on true, 0 on false, and -1 on error.
Maggie Moss1b4552c2020-09-09 13:23:24 -0700317static int
318is_unionable(PyObject *obj)
319{
320 if (obj == Py_None) {
321 return 1;
322 }
323 PyTypeObject *type = Py_TYPE(obj);
Miss Islington (bot)7e6cad72021-06-23 02:38:49 -0700324 CHECK_RES(is_typevar(obj));
325 CHECK_RES(is_new_type(obj));
326 CHECK_RES(is_special_form(obj));
Maggie Moss1b4552c2020-09-09 13:23:24 -0700327 return (
Miss Islington (bot)7e6cad72021-06-23 02:38:49 -0700328 // The following checks never fail.
Maggie Moss1b4552c2020-09-09 13:23:24 -0700329 PyType_Check(obj) ||
kj463c7d32020-12-14 02:38:24 +0800330 PyObject_TypeCheck(obj, &Py_GenericAliasType) ||
Maggie Moss1b4552c2020-09-09 13:23:24 -0700331 type == &_Py_UnionType);
332}
333
kj4eb41d02020-11-09 12:00:13 +0800334PyObject *
335_Py_union_type_or(PyObject* self, PyObject* param)
Maggie Moss1b4552c2020-09-09 13:23:24 -0700336{
337 PyObject *tuple = PyTuple_Pack(2, self, param);
338 if (tuple == NULL) {
339 return NULL;
340 }
341 PyObject *new_union = _Py_Union(tuple);
342 Py_DECREF(tuple);
343 return new_union;
344}
345
346static int
347union_repr_item(_PyUnicodeWriter *writer, PyObject *p)
348{
349 _Py_IDENTIFIER(__module__);
350 _Py_IDENTIFIER(__qualname__);
351 _Py_IDENTIFIER(__origin__);
352 _Py_IDENTIFIER(__args__);
353 PyObject *qualname = NULL;
354 PyObject *module = NULL;
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300355 PyObject *tmp;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700356 PyObject *r = NULL;
357 int err;
358
Serhiy Storchaka6dec5252021-07-15 10:15:14 +0300359 if (p == (PyObject *)&_PyNone_Type) {
360 return _PyUnicodeWriter_WriteASCIIString(writer, "None", 4);
361 }
362
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300363 if (_PyObject_LookupAttrId(p, &PyId___origin__, &tmp) < 0) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700364 goto exit;
365 }
366
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300367 if (tmp) {
368 Py_DECREF(tmp);
369 if (_PyObject_LookupAttrId(p, &PyId___args__, &tmp) < 0) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700370 goto exit;
371 }
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300372 if (tmp) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700373 // It looks like a GenericAlias
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300374 Py_DECREF(tmp);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700375 goto use_repr;
376 }
377 }
378
379 if (_PyObject_LookupAttrId(p, &PyId___qualname__, &qualname) < 0) {
380 goto exit;
381 }
382 if (qualname == NULL) {
383 goto use_repr;
384 }
385 if (_PyObject_LookupAttrId(p, &PyId___module__, &module) < 0) {
386 goto exit;
387 }
388 if (module == NULL || module == Py_None) {
389 goto use_repr;
390 }
391
392 // Looks like a class
393 if (PyUnicode_Check(module) &&
394 _PyUnicode_EqualToASCIIString(module, "builtins"))
395 {
396 // builtins don't need a module name
397 r = PyObject_Str(qualname);
398 goto exit;
399 }
400 else {
401 r = PyUnicode_FromFormat("%S.%S", module, qualname);
402 goto exit;
403 }
404
405use_repr:
406 r = PyObject_Repr(p);
407exit:
408 Py_XDECREF(qualname);
409 Py_XDECREF(module);
410 if (r == NULL) {
411 return -1;
412 }
413 err = _PyUnicodeWriter_WriteStr(writer, r);
414 Py_DECREF(r);
415 return err;
416}
417
418static PyObject *
419union_repr(PyObject *self)
420{
421 unionobject *alias = (unionobject *)self;
422 Py_ssize_t len = PyTuple_GET_SIZE(alias->args);
423
424 _PyUnicodeWriter writer;
425 _PyUnicodeWriter_Init(&writer);
426 for (Py_ssize_t i = 0; i < len; i++) {
427 if (i > 0 && _PyUnicodeWriter_WriteASCIIString(&writer, " | ", 3) < 0) {
428 goto error;
429 }
430 PyObject *p = PyTuple_GET_ITEM(alias->args, i);
431 if (union_repr_item(&writer, p) < 0) {
432 goto error;
433 }
434 }
435 return _PyUnicodeWriter_Finish(&writer);
436error:
437 _PyUnicodeWriter_Dealloc(&writer);
438 return NULL;
439}
440
441static PyMemberDef union_members[] = {
442 {"__args__", T_OBJECT, offsetof(unionobject, args), READONLY},
443 {0}
444};
445
446static PyMethodDef union_methods[] = {
447 {"__instancecheck__", union_instancecheck, METH_O},
448 {"__subclasscheck__", union_subclasscheck, METH_O},
449 {0}};
450
451static PyNumberMethods union_as_number = {
kj4eb41d02020-11-09 12:00:13 +0800452 .nb_or = _Py_union_type_or, // Add __or__ function
Maggie Moss1b4552c2020-09-09 13:23:24 -0700453};
454
455PyTypeObject _Py_UnionType = {
456 PyVarObject_HEAD_INIT(&PyType_Type, 0)
457 .tp_name = "types.Union",
458 .tp_doc = "Represent a PEP 604 union type\n"
459 "\n"
460 "E.g. for int | str",
461 .tp_basicsize = sizeof(unionobject),
462 .tp_dealloc = unionobject_dealloc,
463 .tp_alloc = PyType_GenericAlloc,
Miss Islington (bot)08561342021-07-03 06:33:16 -0700464 .tp_free = PyObject_GC_Del,
465 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
466 .tp_traverse = union_traverse,
Maggie Moss1b4552c2020-09-09 13:23:24 -0700467 .tp_hash = union_hash,
468 .tp_getattro = PyObject_GenericGetAttr,
469 .tp_members = union_members,
470 .tp_methods = union_methods,
471 .tp_richcompare = union_richcompare,
472 .tp_as_number = &union_as_number,
473 .tp_repr = union_repr,
474};
475
476PyObject *
477_Py_Union(PyObject *args)
478{
479 assert(PyTuple_CheckExact(args));
480
481 unionobject* result = NULL;
482
483 // Check arguments are unionable.
Victor Stinnerd67de0a2020-09-23 23:25:54 +0200484 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700485 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
486 PyObject *arg = PyTuple_GET_ITEM(args, iarg);
487 if (arg == NULL) {
488 return NULL;
489 }
490 int is_arg_unionable = is_unionable(arg);
491 if (is_arg_unionable < 0) {
492 return NULL;
493 }
494 if (!is_arg_unionable) {
495 Py_INCREF(Py_NotImplemented);
496 return Py_NotImplemented;
497 }
498 }
499
Miss Islington (bot)08561342021-07-03 06:33:16 -0700500 result = PyObject_GC_New(unionobject, &_Py_UnionType);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700501 if (result == NULL) {
502 return NULL;
503 }
504
505 result->args = dedup_and_flatten_args(args);
Miss Islington (bot)000b9e82021-07-03 13:51:10 -0700506 _PyObject_GC_TRACK(result);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700507 if (result->args == NULL) {
Miss Islington (bot)000b9e82021-07-03 13:51:10 -0700508 Py_DECREF(result);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700509 return NULL;
510 }
Maggie Moss1b4552c2020-09-09 13:23:24 -0700511 return (PyObject*)result;
512}