blob: 762df5d9780a388ef6b1ce0367ca88932de18b37 [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;
Miss Islington (bot)70598802021-07-16 02:02:59 -070036 PyObject *args = PyFrozenSet_New(alias->args);
37 if (args == NULL) {
38 return (Py_hash_t)-1;
Maggie Moss1b4552c2020-09-09 13:23:24 -070039 }
Miss Islington (bot)70598802021-07-16 02:02:59 -070040 Py_hash_t hash = PyObject_Hash(args);
41 Py_DECREF(args);
42 return hash;
Maggie Moss1b4552c2020-09-09 13:23:24 -070043}
44
45static int
46is_generic_alias_in_args(PyObject *args) {
47 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
48 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
49 PyObject *arg = PyTuple_GET_ITEM(args, iarg);
Ken Jin49cd68f2021-01-03 00:19:15 +080050 if (PyObject_TypeCheck(arg, &Py_GenericAliasType)) {
Maggie Moss1b4552c2020-09-09 13:23:24 -070051 return 0;
52 }
53 }
54 return 1;
55}
56
57static PyObject *
58union_instancecheck(PyObject *self, PyObject *instance)
59{
60 unionobject *alias = (unionobject *) self;
61 Py_ssize_t nargs = PyTuple_GET_SIZE(alias->args);
62 if (!is_generic_alias_in_args(alias->args)) {
63 PyErr_SetString(PyExc_TypeError,
64 "isinstance() argument 2 cannot contain a parameterized generic");
65 return NULL;
66 }
67 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
68 PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg);
Miss Islington (bot)b42eee72021-07-13 21:55:45 -070069 if (PyType_Check(arg)) {
70 int res = PyObject_IsInstance(instance, arg);
71 if (res < 0) {
72 return NULL;
73 }
74 if (res) {
75 Py_RETURN_TRUE;
76 }
Maggie Moss1b4552c2020-09-09 13:23:24 -070077 }
78 }
79 Py_RETURN_FALSE;
80}
81
82static PyObject *
83union_subclasscheck(PyObject *self, PyObject *instance)
84{
85 if (!PyType_Check(instance)) {
86 PyErr_SetString(PyExc_TypeError, "issubclass() arg 1 must be a class");
87 return NULL;
88 }
89 unionobject *alias = (unionobject *)self;
90 if (!is_generic_alias_in_args(alias->args)) {
91 PyErr_SetString(PyExc_TypeError,
92 "issubclass() argument 2 cannot contain a parameterized generic");
93 return NULL;
94 }
95 Py_ssize_t nargs = PyTuple_GET_SIZE(alias->args);
96 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
97 PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg);
Miss Islington (bot)b42eee72021-07-13 21:55:45 -070098 if (PyType_Check(arg)) {
99 int res = PyObject_IsSubclass(instance, arg);
100 if (res < 0) {
101 return NULL;
102 }
103 if (res) {
104 Py_RETURN_TRUE;
105 }
Maggie Moss1b4552c2020-09-09 13:23:24 -0700106 }
107 }
108 Py_RETURN_FALSE;
109}
110
111static int
112is_typing_module(PyObject *obj) {
113 PyObject *module = PyObject_GetAttrString(obj, "__module__");
114 if (module == NULL) {
115 return -1;
116 }
117 int is_typing = PyUnicode_Check(module) && _PyUnicode_EqualToASCIIString(module, "typing");
118 Py_DECREF(module);
119 return is_typing;
120}
121
122static int
123is_typing_name(PyObject *obj, char *name)
124{
125 PyTypeObject *type = Py_TYPE(obj);
126 if (strcmp(type->tp_name, name) != 0) {
127 return 0;
128 }
Miss Islington (bot)cc1a47c2021-07-15 00:25:22 -0700129 return is_typing_module((PyObject *)type);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700130}
131
132static PyObject *
133union_richcompare(PyObject *a, PyObject *b, int op)
134{
135 PyObject *result = NULL;
136 if (op != Py_EQ && op != Py_NE) {
137 result = Py_NotImplemented;
138 Py_INCREF(result);
139 return result;
140 }
141
142 PyTypeObject *type = Py_TYPE(b);
143
144 PyObject* a_set = PySet_New(((unionobject*)a)->args);
145 if (a_set == NULL) {
146 return NULL;
147 }
148 PyObject* b_set = PySet_New(NULL);
149 if (b_set == NULL) {
150 goto exit;
151 }
152
153 // Populate b_set with the data from the right object
154 int is_typing_union = is_typing_name(b, "_UnionGenericAlias");
155 if (is_typing_union < 0) {
156 goto exit;
157 }
158 if (is_typing_union) {
159 PyObject *b_args = PyObject_GetAttrString(b, "__args__");
160 if (b_args == NULL) {
161 goto exit;
162 }
163 if (!PyTuple_CheckExact(b_args)) {
164 Py_DECREF(b_args);
165 PyErr_SetString(PyExc_TypeError, "__args__ argument of typing.Union object is not a tuple");
166 goto exit;
167 }
168 Py_ssize_t b_arg_length = PyTuple_GET_SIZE(b_args);
169 for (Py_ssize_t i = 0; i < b_arg_length; i++) {
170 PyObject* arg = PyTuple_GET_ITEM(b_args, i);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700171 if (PySet_Add(b_set, arg) == -1) {
172 Py_DECREF(b_args);
173 goto exit;
174 }
175 }
176 Py_DECREF(b_args);
177 } else if (type == &_Py_UnionType) {
178 PyObject* args = ((unionobject*) b)->args;
179 Py_ssize_t arg_length = PyTuple_GET_SIZE(args);
180 for (Py_ssize_t i = 0; i < arg_length; i++) {
181 PyObject* arg = PyTuple_GET_ITEM(args, i);
182 if (PySet_Add(b_set, arg) == -1) {
183 goto exit;
184 }
185 }
186 } else {
187 if (PySet_Add(b_set, b) == -1) {
188 goto exit;
189 }
190 }
191 result = PyObject_RichCompare(a_set, b_set, op);
192exit:
193 Py_XDECREF(a_set);
194 Py_XDECREF(b_set);
195 return result;
196}
197
198static PyObject*
199flatten_args(PyObject* args)
200{
Victor Stinnerd67de0a2020-09-23 23:25:54 +0200201 Py_ssize_t arg_length = PyTuple_GET_SIZE(args);
202 Py_ssize_t total_args = 0;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700203 // Get number of total args once it's flattened.
204 for (Py_ssize_t i = 0; i < arg_length; i++) {
205 PyObject *arg = PyTuple_GET_ITEM(args, i);
206 PyTypeObject* arg_type = Py_TYPE(arg);
207 if (arg_type == &_Py_UnionType) {
208 total_args += PyTuple_GET_SIZE(((unionobject*) arg)->args);
209 } else {
210 total_args++;
211 }
212 }
213 // Create new tuple of flattened args.
214 PyObject *flattened_args = PyTuple_New(total_args);
215 if (flattened_args == NULL) {
216 return NULL;
217 }
218 Py_ssize_t pos = 0;
219 for (Py_ssize_t i = 0; i < arg_length; i++) {
220 PyObject *arg = PyTuple_GET_ITEM(args, i);
221 PyTypeObject* arg_type = Py_TYPE(arg);
222 if (arg_type == &_Py_UnionType) {
223 PyObject* nested_args = ((unionobject*)arg)->args;
Victor Stinnerd73cf7c2020-09-26 12:48:41 +0200224 Py_ssize_t nested_arg_length = PyTuple_GET_SIZE(nested_args);
225 for (Py_ssize_t j = 0; j < nested_arg_length; j++) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700226 PyObject* nested_arg = PyTuple_GET_ITEM(nested_args, j);
227 Py_INCREF(nested_arg);
228 PyTuple_SET_ITEM(flattened_args, pos, nested_arg);
229 pos++;
230 }
231 } else {
Serhiy Storchaka6dec5252021-07-15 10:15:14 +0300232 if (arg == Py_None) {
233 arg = (PyObject *)&_PyNone_Type;
234 }
Maggie Moss1b4552c2020-09-09 13:23:24 -0700235 Py_INCREF(arg);
236 PyTuple_SET_ITEM(flattened_args, pos, arg);
237 pos++;
238 }
239 }
240 return flattened_args;
241}
242
243static PyObject*
244dedup_and_flatten_args(PyObject* args)
245{
246 args = flatten_args(args);
247 if (args == NULL) {
248 return NULL;
249 }
250 Py_ssize_t arg_length = PyTuple_GET_SIZE(args);
251 PyObject *new_args = PyTuple_New(arg_length);
252 if (new_args == NULL) {
253 return NULL;
254 }
255 // Add unique elements to an array.
Victor Stinnerd73cf7c2020-09-26 12:48:41 +0200256 Py_ssize_t added_items = 0;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700257 for (Py_ssize_t i = 0; i < arg_length; i++) {
258 int is_duplicate = 0;
259 PyObject* i_element = PyTuple_GET_ITEM(args, i);
260 for (Py_ssize_t j = i + 1; j < arg_length; j++) {
261 PyObject* j_element = PyTuple_GET_ITEM(args, j);
kj463c7d32020-12-14 02:38:24 +0800262 int is_ga = PyObject_TypeCheck(i_element, &Py_GenericAliasType) &&
263 PyObject_TypeCheck(j_element, &Py_GenericAliasType);
kj4eb41d02020-11-09 12:00:13 +0800264 // RichCompare to also deduplicate GenericAlias types (slower)
265 is_duplicate = is_ga ? PyObject_RichCompareBool(i_element, j_element, Py_EQ)
266 : i_element == j_element;
267 // Should only happen if RichCompare fails
268 if (is_duplicate < 0) {
269 Py_DECREF(args);
270 Py_DECREF(new_args);
271 return NULL;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700272 }
kj4eb41d02020-11-09 12:00:13 +0800273 if (is_duplicate)
274 break;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700275 }
276 if (!is_duplicate) {
277 Py_INCREF(i_element);
278 PyTuple_SET_ITEM(new_args, added_items, i_element);
279 added_items++;
280 }
281 }
282 Py_DECREF(args);
283 _PyTuple_Resize(&new_args, added_items);
284 return new_args;
285}
286
287static int
288is_typevar(PyObject *obj)
289{
290 return is_typing_name(obj, "TypeVar");
291}
292
293static int
294is_special_form(PyObject *obj)
295{
296 return is_typing_name(obj, "_SpecialForm");
297}
298
299static int
300is_new_type(PyObject *obj)
301{
302 PyTypeObject *type = Py_TYPE(obj);
303 if (type != &PyFunction_Type) {
304 return 0;
305 }
306 return is_typing_module(obj);
307}
308
Miss Islington (bot)7e6cad72021-06-23 02:38:49 -0700309// Emulates short-circuiting behavior of the ``||`` operator
310// while also checking negative values.
311#define CHECK_RES(res) { \
312 int result = res; \
313 if (result) { \
314 return result; \
315 } \
316}
317
318// Returns 1 on true, 0 on false, and -1 on error.
Maggie Moss1b4552c2020-09-09 13:23:24 -0700319static int
320is_unionable(PyObject *obj)
321{
322 if (obj == Py_None) {
323 return 1;
324 }
325 PyTypeObject *type = Py_TYPE(obj);
Miss Islington (bot)7e6cad72021-06-23 02:38:49 -0700326 CHECK_RES(is_typevar(obj));
327 CHECK_RES(is_new_type(obj));
328 CHECK_RES(is_special_form(obj));
Maggie Moss1b4552c2020-09-09 13:23:24 -0700329 return (
Miss Islington (bot)7e6cad72021-06-23 02:38:49 -0700330 // The following checks never fail.
Maggie Moss1b4552c2020-09-09 13:23:24 -0700331 PyType_Check(obj) ||
kj463c7d32020-12-14 02:38:24 +0800332 PyObject_TypeCheck(obj, &Py_GenericAliasType) ||
Maggie Moss1b4552c2020-09-09 13:23:24 -0700333 type == &_Py_UnionType);
334}
335
kj4eb41d02020-11-09 12:00:13 +0800336PyObject *
337_Py_union_type_or(PyObject* self, PyObject* param)
Maggie Moss1b4552c2020-09-09 13:23:24 -0700338{
339 PyObject *tuple = PyTuple_Pack(2, self, param);
340 if (tuple == NULL) {
341 return NULL;
342 }
343 PyObject *new_union = _Py_Union(tuple);
344 Py_DECREF(tuple);
345 return new_union;
346}
347
348static int
349union_repr_item(_PyUnicodeWriter *writer, PyObject *p)
350{
351 _Py_IDENTIFIER(__module__);
352 _Py_IDENTIFIER(__qualname__);
353 _Py_IDENTIFIER(__origin__);
354 _Py_IDENTIFIER(__args__);
355 PyObject *qualname = NULL;
356 PyObject *module = NULL;
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300357 PyObject *tmp;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700358 PyObject *r = NULL;
359 int err;
360
Serhiy Storchaka6dec5252021-07-15 10:15:14 +0300361 if (p == (PyObject *)&_PyNone_Type) {
362 return _PyUnicodeWriter_WriteASCIIString(writer, "None", 4);
363 }
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}