blob: bcba0f679bd09cb6b72404476fa2f01d8471d4bd [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);
67 if (arg == Py_None) {
68 arg = (PyObject *)&_PyNone_Type;
69 }
Miss Islington (bot)b42eee72021-07-13 21:55:45 -070070 if (PyType_Check(arg)) {
71 int res = PyObject_IsInstance(instance, arg);
72 if (res < 0) {
73 return NULL;
74 }
75 if (res) {
76 Py_RETURN_TRUE;
77 }
Maggie Moss1b4552c2020-09-09 13:23:24 -070078 }
79 }
80 Py_RETURN_FALSE;
81}
82
83static PyObject *
84union_subclasscheck(PyObject *self, PyObject *instance)
85{
86 if (!PyType_Check(instance)) {
87 PyErr_SetString(PyExc_TypeError, "issubclass() arg 1 must be a class");
88 return NULL;
89 }
90 unionobject *alias = (unionobject *)self;
91 if (!is_generic_alias_in_args(alias->args)) {
92 PyErr_SetString(PyExc_TypeError,
93 "issubclass() argument 2 cannot contain a parameterized generic");
94 return NULL;
95 }
96 Py_ssize_t nargs = PyTuple_GET_SIZE(alias->args);
97 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
98 PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg);
Miss Islington (bot)b42eee72021-07-13 21:55:45 -070099 if (arg == Py_None) {
100 arg = (PyObject *)&_PyNone_Type;
101 }
102 if (PyType_Check(arg)) {
103 int res = PyObject_IsSubclass(instance, arg);
104 if (res < 0) {
105 return NULL;
106 }
107 if (res) {
108 Py_RETURN_TRUE;
109 }
Maggie Moss1b4552c2020-09-09 13:23:24 -0700110 }
111 }
112 Py_RETURN_FALSE;
113}
114
115static int
116is_typing_module(PyObject *obj) {
117 PyObject *module = PyObject_GetAttrString(obj, "__module__");
118 if (module == NULL) {
119 return -1;
120 }
121 int is_typing = PyUnicode_Check(module) && _PyUnicode_EqualToASCIIString(module, "typing");
122 Py_DECREF(module);
123 return is_typing;
124}
125
126static int
127is_typing_name(PyObject *obj, char *name)
128{
129 PyTypeObject *type = Py_TYPE(obj);
130 if (strcmp(type->tp_name, name) != 0) {
131 return 0;
132 }
133 return is_typing_module(obj);
134}
135
136static PyObject *
137union_richcompare(PyObject *a, PyObject *b, int op)
138{
139 PyObject *result = NULL;
140 if (op != Py_EQ && op != Py_NE) {
141 result = Py_NotImplemented;
142 Py_INCREF(result);
143 return result;
144 }
145
146 PyTypeObject *type = Py_TYPE(b);
147
148 PyObject* a_set = PySet_New(((unionobject*)a)->args);
149 if (a_set == NULL) {
150 return NULL;
151 }
152 PyObject* b_set = PySet_New(NULL);
153 if (b_set == NULL) {
154 goto exit;
155 }
156
157 // Populate b_set with the data from the right object
158 int is_typing_union = is_typing_name(b, "_UnionGenericAlias");
159 if (is_typing_union < 0) {
160 goto exit;
161 }
162 if (is_typing_union) {
163 PyObject *b_args = PyObject_GetAttrString(b, "__args__");
164 if (b_args == NULL) {
165 goto exit;
166 }
167 if (!PyTuple_CheckExact(b_args)) {
168 Py_DECREF(b_args);
169 PyErr_SetString(PyExc_TypeError, "__args__ argument of typing.Union object is not a tuple");
170 goto exit;
171 }
172 Py_ssize_t b_arg_length = PyTuple_GET_SIZE(b_args);
173 for (Py_ssize_t i = 0; i < b_arg_length; i++) {
174 PyObject* arg = PyTuple_GET_ITEM(b_args, i);
175 if (arg == (PyObject *)&_PyNone_Type) {
176 arg = Py_None;
177 }
178 if (PySet_Add(b_set, arg) == -1) {
179 Py_DECREF(b_args);
180 goto exit;
181 }
182 }
183 Py_DECREF(b_args);
184 } else if (type == &_Py_UnionType) {
185 PyObject* args = ((unionobject*) b)->args;
186 Py_ssize_t arg_length = PyTuple_GET_SIZE(args);
187 for (Py_ssize_t i = 0; i < arg_length; i++) {
188 PyObject* arg = PyTuple_GET_ITEM(args, i);
189 if (PySet_Add(b_set, arg) == -1) {
190 goto exit;
191 }
192 }
193 } else {
194 if (PySet_Add(b_set, b) == -1) {
195 goto exit;
196 }
197 }
198 result = PyObject_RichCompare(a_set, b_set, op);
199exit:
200 Py_XDECREF(a_set);
201 Py_XDECREF(b_set);
202 return result;
203}
204
205static PyObject*
206flatten_args(PyObject* args)
207{
Victor Stinnerd67de0a2020-09-23 23:25:54 +0200208 Py_ssize_t arg_length = PyTuple_GET_SIZE(args);
209 Py_ssize_t total_args = 0;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700210 // Get number of total args once it's flattened.
211 for (Py_ssize_t i = 0; i < arg_length; i++) {
212 PyObject *arg = PyTuple_GET_ITEM(args, i);
213 PyTypeObject* arg_type = Py_TYPE(arg);
214 if (arg_type == &_Py_UnionType) {
215 total_args += PyTuple_GET_SIZE(((unionobject*) arg)->args);
216 } else {
217 total_args++;
218 }
219 }
220 // Create new tuple of flattened args.
221 PyObject *flattened_args = PyTuple_New(total_args);
222 if (flattened_args == NULL) {
223 return NULL;
224 }
225 Py_ssize_t pos = 0;
226 for (Py_ssize_t i = 0; i < arg_length; i++) {
227 PyObject *arg = PyTuple_GET_ITEM(args, i);
228 PyTypeObject* arg_type = Py_TYPE(arg);
229 if (arg_type == &_Py_UnionType) {
230 PyObject* nested_args = ((unionobject*)arg)->args;
Victor Stinnerd73cf7c2020-09-26 12:48:41 +0200231 Py_ssize_t nested_arg_length = PyTuple_GET_SIZE(nested_args);
232 for (Py_ssize_t j = 0; j < nested_arg_length; j++) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700233 PyObject* nested_arg = PyTuple_GET_ITEM(nested_args, j);
234 Py_INCREF(nested_arg);
235 PyTuple_SET_ITEM(flattened_args, pos, nested_arg);
236 pos++;
237 }
238 } else {
239 Py_INCREF(arg);
240 PyTuple_SET_ITEM(flattened_args, pos, arg);
241 pos++;
242 }
243 }
244 return flattened_args;
245}
246
247static PyObject*
248dedup_and_flatten_args(PyObject* args)
249{
250 args = flatten_args(args);
251 if (args == NULL) {
252 return NULL;
253 }
254 Py_ssize_t arg_length = PyTuple_GET_SIZE(args);
255 PyObject *new_args = PyTuple_New(arg_length);
256 if (new_args == NULL) {
257 return NULL;
258 }
259 // Add unique elements to an array.
Victor Stinnerd73cf7c2020-09-26 12:48:41 +0200260 Py_ssize_t added_items = 0;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700261 for (Py_ssize_t i = 0; i < arg_length; i++) {
262 int is_duplicate = 0;
263 PyObject* i_element = PyTuple_GET_ITEM(args, i);
264 for (Py_ssize_t j = i + 1; j < arg_length; j++) {
265 PyObject* j_element = PyTuple_GET_ITEM(args, j);
kj463c7d32020-12-14 02:38:24 +0800266 int is_ga = PyObject_TypeCheck(i_element, &Py_GenericAliasType) &&
267 PyObject_TypeCheck(j_element, &Py_GenericAliasType);
kj4eb41d02020-11-09 12:00:13 +0800268 // RichCompare to also deduplicate GenericAlias types (slower)
269 is_duplicate = is_ga ? PyObject_RichCompareBool(i_element, j_element, Py_EQ)
270 : i_element == j_element;
271 // Should only happen if RichCompare fails
272 if (is_duplicate < 0) {
273 Py_DECREF(args);
274 Py_DECREF(new_args);
275 return NULL;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700276 }
kj4eb41d02020-11-09 12:00:13 +0800277 if (is_duplicate)
278 break;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700279 }
280 if (!is_duplicate) {
281 Py_INCREF(i_element);
282 PyTuple_SET_ITEM(new_args, added_items, i_element);
283 added_items++;
284 }
285 }
286 Py_DECREF(args);
287 _PyTuple_Resize(&new_args, added_items);
288 return new_args;
289}
290
291static int
292is_typevar(PyObject *obj)
293{
294 return is_typing_name(obj, "TypeVar");
295}
296
297static int
298is_special_form(PyObject *obj)
299{
300 return is_typing_name(obj, "_SpecialForm");
301}
302
303static int
304is_new_type(PyObject *obj)
305{
306 PyTypeObject *type = Py_TYPE(obj);
307 if (type != &PyFunction_Type) {
308 return 0;
309 }
310 return is_typing_module(obj);
311}
312
Miss Islington (bot)7e6cad72021-06-23 02:38:49 -0700313// Emulates short-circuiting behavior of the ``||`` operator
314// while also checking negative values.
315#define CHECK_RES(res) { \
316 int result = res; \
317 if (result) { \
318 return result; \
319 } \
320}
321
322// Returns 1 on true, 0 on false, and -1 on error.
Maggie Moss1b4552c2020-09-09 13:23:24 -0700323static int
324is_unionable(PyObject *obj)
325{
326 if (obj == Py_None) {
327 return 1;
328 }
329 PyTypeObject *type = Py_TYPE(obj);
Miss Islington (bot)7e6cad72021-06-23 02:38:49 -0700330 CHECK_RES(is_typevar(obj));
331 CHECK_RES(is_new_type(obj));
332 CHECK_RES(is_special_form(obj));
Maggie Moss1b4552c2020-09-09 13:23:24 -0700333 return (
Miss Islington (bot)7e6cad72021-06-23 02:38:49 -0700334 // The following checks never fail.
Maggie Moss1b4552c2020-09-09 13:23:24 -0700335 PyType_Check(obj) ||
kj463c7d32020-12-14 02:38:24 +0800336 PyObject_TypeCheck(obj, &Py_GenericAliasType) ||
Maggie Moss1b4552c2020-09-09 13:23:24 -0700337 type == &_Py_UnionType);
338}
339
kj4eb41d02020-11-09 12:00:13 +0800340PyObject *
341_Py_union_type_or(PyObject* self, PyObject* param)
Maggie Moss1b4552c2020-09-09 13:23:24 -0700342{
343 PyObject *tuple = PyTuple_Pack(2, self, param);
344 if (tuple == NULL) {
345 return NULL;
346 }
347 PyObject *new_union = _Py_Union(tuple);
348 Py_DECREF(tuple);
349 return new_union;
350}
351
352static int
353union_repr_item(_PyUnicodeWriter *writer, PyObject *p)
354{
355 _Py_IDENTIFIER(__module__);
356 _Py_IDENTIFIER(__qualname__);
357 _Py_IDENTIFIER(__origin__);
358 _Py_IDENTIFIER(__args__);
359 PyObject *qualname = NULL;
360 PyObject *module = NULL;
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300361 PyObject *tmp;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700362 PyObject *r = NULL;
363 int err;
364
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300365 if (_PyObject_LookupAttrId(p, &PyId___origin__, &tmp) < 0) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700366 goto exit;
367 }
368
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300369 if (tmp) {
370 Py_DECREF(tmp);
371 if (_PyObject_LookupAttrId(p, &PyId___args__, &tmp) < 0) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700372 goto exit;
373 }
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300374 if (tmp) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700375 // It looks like a GenericAlias
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300376 Py_DECREF(tmp);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700377 goto use_repr;
378 }
379 }
380
381 if (_PyObject_LookupAttrId(p, &PyId___qualname__, &qualname) < 0) {
382 goto exit;
383 }
384 if (qualname == NULL) {
385 goto use_repr;
386 }
387 if (_PyObject_LookupAttrId(p, &PyId___module__, &module) < 0) {
388 goto exit;
389 }
390 if (module == NULL || module == Py_None) {
391 goto use_repr;
392 }
393
394 // Looks like a class
395 if (PyUnicode_Check(module) &&
396 _PyUnicode_EqualToASCIIString(module, "builtins"))
397 {
398 // builtins don't need a module name
399 r = PyObject_Str(qualname);
400 goto exit;
401 }
402 else {
403 r = PyUnicode_FromFormat("%S.%S", module, qualname);
404 goto exit;
405 }
406
407use_repr:
408 r = PyObject_Repr(p);
409exit:
410 Py_XDECREF(qualname);
411 Py_XDECREF(module);
412 if (r == NULL) {
413 return -1;
414 }
415 err = _PyUnicodeWriter_WriteStr(writer, r);
416 Py_DECREF(r);
417 return err;
418}
419
420static PyObject *
421union_repr(PyObject *self)
422{
423 unionobject *alias = (unionobject *)self;
424 Py_ssize_t len = PyTuple_GET_SIZE(alias->args);
425
426 _PyUnicodeWriter writer;
427 _PyUnicodeWriter_Init(&writer);
428 for (Py_ssize_t i = 0; i < len; i++) {
429 if (i > 0 && _PyUnicodeWriter_WriteASCIIString(&writer, " | ", 3) < 0) {
430 goto error;
431 }
432 PyObject *p = PyTuple_GET_ITEM(alias->args, i);
433 if (union_repr_item(&writer, p) < 0) {
434 goto error;
435 }
436 }
437 return _PyUnicodeWriter_Finish(&writer);
438error:
439 _PyUnicodeWriter_Dealloc(&writer);
440 return NULL;
441}
442
443static PyMemberDef union_members[] = {
444 {"__args__", T_OBJECT, offsetof(unionobject, args), READONLY},
445 {0}
446};
447
448static PyMethodDef union_methods[] = {
449 {"__instancecheck__", union_instancecheck, METH_O},
450 {"__subclasscheck__", union_subclasscheck, METH_O},
451 {0}};
452
453static PyNumberMethods union_as_number = {
kj4eb41d02020-11-09 12:00:13 +0800454 .nb_or = _Py_union_type_or, // Add __or__ function
Maggie Moss1b4552c2020-09-09 13:23:24 -0700455};
456
457PyTypeObject _Py_UnionType = {
458 PyVarObject_HEAD_INIT(&PyType_Type, 0)
459 .tp_name = "types.Union",
460 .tp_doc = "Represent a PEP 604 union type\n"
461 "\n"
462 "E.g. for int | str",
463 .tp_basicsize = sizeof(unionobject),
464 .tp_dealloc = unionobject_dealloc,
465 .tp_alloc = PyType_GenericAlloc,
Miss Islington (bot)08561342021-07-03 06:33:16 -0700466 .tp_free = PyObject_GC_Del,
467 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
468 .tp_traverse = union_traverse,
Maggie Moss1b4552c2020-09-09 13:23:24 -0700469 .tp_hash = union_hash,
470 .tp_getattro = PyObject_GenericGetAttr,
471 .tp_members = union_members,
472 .tp_methods = union_methods,
473 .tp_richcompare = union_richcompare,
474 .tp_as_number = &union_as_number,
475 .tp_repr = union_repr,
476};
477
478PyObject *
479_Py_Union(PyObject *args)
480{
481 assert(PyTuple_CheckExact(args));
482
483 unionobject* result = NULL;
484
485 // Check arguments are unionable.
Victor Stinnerd67de0a2020-09-23 23:25:54 +0200486 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700487 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
488 PyObject *arg = PyTuple_GET_ITEM(args, iarg);
489 if (arg == NULL) {
490 return NULL;
491 }
492 int is_arg_unionable = is_unionable(arg);
493 if (is_arg_unionable < 0) {
494 return NULL;
495 }
496 if (!is_arg_unionable) {
497 Py_INCREF(Py_NotImplemented);
498 return Py_NotImplemented;
499 }
500 }
501
Miss Islington (bot)08561342021-07-03 06:33:16 -0700502 result = PyObject_GC_New(unionobject, &_Py_UnionType);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700503 if (result == NULL) {
504 return NULL;
505 }
506
507 result->args = dedup_and_flatten_args(args);
Miss Islington (bot)000b9e82021-07-03 13:51:10 -0700508 _PyObject_GC_TRACK(result);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700509 if (result->args == NULL) {
Miss Islington (bot)000b9e82021-07-03 13:51:10 -0700510 Py_DECREF(result);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700511 return NULL;
512 }
Maggie Moss1b4552c2020-09-09 13:23:24 -0700513 return (PyObject*)result;
514}