Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 1 | // types.Union -- used to represent e.g. Union[int, str], int | str |
| 2 | #include "Python.h" |
| 3 | #include "pycore_unionobject.h" |
| 4 | #include "structmember.h" |
| 5 | |
| 6 | |
| 7 | typedef struct { |
| 8 | PyObject_HEAD |
| 9 | PyObject *args; |
| 10 | } unionobject; |
| 11 | |
| 12 | static void |
| 13 | unionobject_dealloc(PyObject *self) |
| 14 | { |
| 15 | unionobject *alias = (unionobject *)self; |
| 16 | |
| 17 | Py_XDECREF(alias->args); |
Neil Schemenauer | 0564aaf | 2020-10-27 11:55:52 -0700 | [diff] [blame] | 18 | Py_TYPE(self)->tp_free(self); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | static Py_hash_t |
| 22 | union_hash(PyObject *self) |
| 23 | { |
| 24 | unionobject *alias = (unionobject *)self; |
| 25 | Py_hash_t h1 = PyObject_Hash(alias->args); |
| 26 | if (h1 == -1) { |
| 27 | return -1; |
| 28 | } |
| 29 | return h1; |
| 30 | } |
| 31 | |
| 32 | static int |
| 33 | is_generic_alias_in_args(PyObject *args) { |
| 34 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 35 | for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) { |
| 36 | PyObject *arg = PyTuple_GET_ITEM(args, iarg); |
Ken Jin | 49cd68f | 2021-01-03 00:19:15 +0800 | [diff] [blame] | 37 | if (PyObject_TypeCheck(arg, &Py_GenericAliasType)) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 38 | return 0; |
| 39 | } |
| 40 | } |
| 41 | return 1; |
| 42 | } |
| 43 | |
| 44 | static PyObject * |
| 45 | union_instancecheck(PyObject *self, PyObject *instance) |
| 46 | { |
| 47 | unionobject *alias = (unionobject *) self; |
| 48 | Py_ssize_t nargs = PyTuple_GET_SIZE(alias->args); |
| 49 | if (!is_generic_alias_in_args(alias->args)) { |
| 50 | PyErr_SetString(PyExc_TypeError, |
| 51 | "isinstance() argument 2 cannot contain a parameterized generic"); |
| 52 | return NULL; |
| 53 | } |
| 54 | for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) { |
| 55 | PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg); |
| 56 | if (arg == Py_None) { |
| 57 | arg = (PyObject *)&_PyNone_Type; |
| 58 | } |
| 59 | if (PyType_Check(arg) && PyObject_IsInstance(instance, arg) != 0) { |
| 60 | Py_RETURN_TRUE; |
| 61 | } |
| 62 | } |
| 63 | Py_RETURN_FALSE; |
| 64 | } |
| 65 | |
| 66 | static PyObject * |
| 67 | union_subclasscheck(PyObject *self, PyObject *instance) |
| 68 | { |
| 69 | if (!PyType_Check(instance)) { |
| 70 | PyErr_SetString(PyExc_TypeError, "issubclass() arg 1 must be a class"); |
| 71 | return NULL; |
| 72 | } |
| 73 | unionobject *alias = (unionobject *)self; |
| 74 | if (!is_generic_alias_in_args(alias->args)) { |
| 75 | PyErr_SetString(PyExc_TypeError, |
| 76 | "issubclass() argument 2 cannot contain a parameterized generic"); |
| 77 | return NULL; |
| 78 | } |
| 79 | Py_ssize_t nargs = PyTuple_GET_SIZE(alias->args); |
| 80 | for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) { |
| 81 | PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg); |
| 82 | if (PyType_Check(arg) && (PyType_IsSubtype((PyTypeObject *)instance, (PyTypeObject *)arg) != 0)) { |
| 83 | Py_RETURN_TRUE; |
| 84 | } |
| 85 | } |
| 86 | Py_RETURN_FALSE; |
| 87 | } |
| 88 | |
| 89 | static int |
| 90 | is_typing_module(PyObject *obj) { |
| 91 | PyObject *module = PyObject_GetAttrString(obj, "__module__"); |
| 92 | if (module == NULL) { |
| 93 | return -1; |
| 94 | } |
| 95 | int is_typing = PyUnicode_Check(module) && _PyUnicode_EqualToASCIIString(module, "typing"); |
| 96 | Py_DECREF(module); |
| 97 | return is_typing; |
| 98 | } |
| 99 | |
| 100 | static int |
| 101 | is_typing_name(PyObject *obj, char *name) |
| 102 | { |
| 103 | PyTypeObject *type = Py_TYPE(obj); |
| 104 | if (strcmp(type->tp_name, name) != 0) { |
| 105 | return 0; |
| 106 | } |
| 107 | return is_typing_module(obj); |
| 108 | } |
| 109 | |
| 110 | static PyObject * |
| 111 | union_richcompare(PyObject *a, PyObject *b, int op) |
| 112 | { |
| 113 | PyObject *result = NULL; |
| 114 | if (op != Py_EQ && op != Py_NE) { |
| 115 | result = Py_NotImplemented; |
| 116 | Py_INCREF(result); |
| 117 | return result; |
| 118 | } |
| 119 | |
| 120 | PyTypeObject *type = Py_TYPE(b); |
| 121 | |
| 122 | PyObject* a_set = PySet_New(((unionobject*)a)->args); |
| 123 | if (a_set == NULL) { |
| 124 | return NULL; |
| 125 | } |
| 126 | PyObject* b_set = PySet_New(NULL); |
| 127 | if (b_set == NULL) { |
| 128 | goto exit; |
| 129 | } |
| 130 | |
| 131 | // Populate b_set with the data from the right object |
| 132 | int is_typing_union = is_typing_name(b, "_UnionGenericAlias"); |
| 133 | if (is_typing_union < 0) { |
| 134 | goto exit; |
| 135 | } |
| 136 | if (is_typing_union) { |
| 137 | PyObject *b_args = PyObject_GetAttrString(b, "__args__"); |
| 138 | if (b_args == NULL) { |
| 139 | goto exit; |
| 140 | } |
| 141 | if (!PyTuple_CheckExact(b_args)) { |
| 142 | Py_DECREF(b_args); |
| 143 | PyErr_SetString(PyExc_TypeError, "__args__ argument of typing.Union object is not a tuple"); |
| 144 | goto exit; |
| 145 | } |
| 146 | Py_ssize_t b_arg_length = PyTuple_GET_SIZE(b_args); |
| 147 | for (Py_ssize_t i = 0; i < b_arg_length; i++) { |
| 148 | PyObject* arg = PyTuple_GET_ITEM(b_args, i); |
| 149 | if (arg == (PyObject *)&_PyNone_Type) { |
| 150 | arg = Py_None; |
| 151 | } |
| 152 | if (PySet_Add(b_set, arg) == -1) { |
| 153 | Py_DECREF(b_args); |
| 154 | goto exit; |
| 155 | } |
| 156 | } |
| 157 | Py_DECREF(b_args); |
| 158 | } else if (type == &_Py_UnionType) { |
| 159 | PyObject* args = ((unionobject*) b)->args; |
| 160 | Py_ssize_t arg_length = PyTuple_GET_SIZE(args); |
| 161 | for (Py_ssize_t i = 0; i < arg_length; i++) { |
| 162 | PyObject* arg = PyTuple_GET_ITEM(args, i); |
| 163 | if (PySet_Add(b_set, arg) == -1) { |
| 164 | goto exit; |
| 165 | } |
| 166 | } |
| 167 | } else { |
| 168 | if (PySet_Add(b_set, b) == -1) { |
| 169 | goto exit; |
| 170 | } |
| 171 | } |
| 172 | result = PyObject_RichCompare(a_set, b_set, op); |
| 173 | exit: |
| 174 | Py_XDECREF(a_set); |
| 175 | Py_XDECREF(b_set); |
| 176 | return result; |
| 177 | } |
| 178 | |
| 179 | static PyObject* |
| 180 | flatten_args(PyObject* args) |
| 181 | { |
Victor Stinner | d67de0a | 2020-09-23 23:25:54 +0200 | [diff] [blame] | 182 | Py_ssize_t arg_length = PyTuple_GET_SIZE(args); |
| 183 | Py_ssize_t total_args = 0; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 184 | // Get number of total args once it's flattened. |
| 185 | for (Py_ssize_t i = 0; i < arg_length; i++) { |
| 186 | PyObject *arg = PyTuple_GET_ITEM(args, i); |
| 187 | PyTypeObject* arg_type = Py_TYPE(arg); |
| 188 | if (arg_type == &_Py_UnionType) { |
| 189 | total_args += PyTuple_GET_SIZE(((unionobject*) arg)->args); |
| 190 | } else { |
| 191 | total_args++; |
| 192 | } |
| 193 | } |
| 194 | // Create new tuple of flattened args. |
| 195 | PyObject *flattened_args = PyTuple_New(total_args); |
| 196 | if (flattened_args == NULL) { |
| 197 | return NULL; |
| 198 | } |
| 199 | Py_ssize_t pos = 0; |
| 200 | for (Py_ssize_t i = 0; i < arg_length; i++) { |
| 201 | PyObject *arg = PyTuple_GET_ITEM(args, i); |
| 202 | PyTypeObject* arg_type = Py_TYPE(arg); |
| 203 | if (arg_type == &_Py_UnionType) { |
| 204 | PyObject* nested_args = ((unionobject*)arg)->args; |
Victor Stinner | d73cf7c | 2020-09-26 12:48:41 +0200 | [diff] [blame] | 205 | Py_ssize_t nested_arg_length = PyTuple_GET_SIZE(nested_args); |
| 206 | for (Py_ssize_t j = 0; j < nested_arg_length; j++) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 207 | PyObject* nested_arg = PyTuple_GET_ITEM(nested_args, j); |
| 208 | Py_INCREF(nested_arg); |
| 209 | PyTuple_SET_ITEM(flattened_args, pos, nested_arg); |
| 210 | pos++; |
| 211 | } |
| 212 | } else { |
| 213 | Py_INCREF(arg); |
| 214 | PyTuple_SET_ITEM(flattened_args, pos, arg); |
| 215 | pos++; |
| 216 | } |
| 217 | } |
| 218 | return flattened_args; |
| 219 | } |
| 220 | |
| 221 | static PyObject* |
| 222 | dedup_and_flatten_args(PyObject* args) |
| 223 | { |
| 224 | args = flatten_args(args); |
| 225 | if (args == NULL) { |
| 226 | return NULL; |
| 227 | } |
| 228 | Py_ssize_t arg_length = PyTuple_GET_SIZE(args); |
| 229 | PyObject *new_args = PyTuple_New(arg_length); |
| 230 | if (new_args == NULL) { |
| 231 | return NULL; |
| 232 | } |
| 233 | // Add unique elements to an array. |
Victor Stinner | d73cf7c | 2020-09-26 12:48:41 +0200 | [diff] [blame] | 234 | Py_ssize_t added_items = 0; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 235 | for (Py_ssize_t i = 0; i < arg_length; i++) { |
| 236 | int is_duplicate = 0; |
| 237 | PyObject* i_element = PyTuple_GET_ITEM(args, i); |
| 238 | for (Py_ssize_t j = i + 1; j < arg_length; j++) { |
| 239 | PyObject* j_element = PyTuple_GET_ITEM(args, j); |
kj | 463c7d3 | 2020-12-14 02:38:24 +0800 | [diff] [blame] | 240 | int is_ga = PyObject_TypeCheck(i_element, &Py_GenericAliasType) && |
| 241 | PyObject_TypeCheck(j_element, &Py_GenericAliasType); |
kj | 4eb41d0 | 2020-11-09 12:00:13 +0800 | [diff] [blame] | 242 | // RichCompare to also deduplicate GenericAlias types (slower) |
| 243 | is_duplicate = is_ga ? PyObject_RichCompareBool(i_element, j_element, Py_EQ) |
| 244 | : i_element == j_element; |
| 245 | // Should only happen if RichCompare fails |
| 246 | if (is_duplicate < 0) { |
| 247 | Py_DECREF(args); |
| 248 | Py_DECREF(new_args); |
| 249 | return NULL; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 250 | } |
kj | 4eb41d0 | 2020-11-09 12:00:13 +0800 | [diff] [blame] | 251 | if (is_duplicate) |
| 252 | break; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 253 | } |
| 254 | if (!is_duplicate) { |
| 255 | Py_INCREF(i_element); |
| 256 | PyTuple_SET_ITEM(new_args, added_items, i_element); |
| 257 | added_items++; |
| 258 | } |
| 259 | } |
| 260 | Py_DECREF(args); |
| 261 | _PyTuple_Resize(&new_args, added_items); |
| 262 | return new_args; |
| 263 | } |
| 264 | |
| 265 | static int |
| 266 | is_typevar(PyObject *obj) |
| 267 | { |
| 268 | return is_typing_name(obj, "TypeVar"); |
| 269 | } |
| 270 | |
| 271 | static int |
| 272 | is_special_form(PyObject *obj) |
| 273 | { |
| 274 | return is_typing_name(obj, "_SpecialForm"); |
| 275 | } |
| 276 | |
| 277 | static int |
| 278 | is_new_type(PyObject *obj) |
| 279 | { |
| 280 | PyTypeObject *type = Py_TYPE(obj); |
| 281 | if (type != &PyFunction_Type) { |
| 282 | return 0; |
| 283 | } |
| 284 | return is_typing_module(obj); |
| 285 | } |
| 286 | |
| 287 | static int |
| 288 | is_unionable(PyObject *obj) |
| 289 | { |
| 290 | if (obj == Py_None) { |
| 291 | return 1; |
| 292 | } |
| 293 | PyTypeObject *type = Py_TYPE(obj); |
| 294 | return ( |
| 295 | is_typevar(obj) || |
| 296 | is_new_type(obj) || |
| 297 | is_special_form(obj) || |
| 298 | PyType_Check(obj) || |
kj | 463c7d3 | 2020-12-14 02:38:24 +0800 | [diff] [blame] | 299 | PyObject_TypeCheck(obj, &Py_GenericAliasType) || |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 300 | type == &_Py_UnionType); |
| 301 | } |
| 302 | |
kj | 4eb41d0 | 2020-11-09 12:00:13 +0800 | [diff] [blame] | 303 | PyObject * |
| 304 | _Py_union_type_or(PyObject* self, PyObject* param) |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 305 | { |
| 306 | PyObject *tuple = PyTuple_Pack(2, self, param); |
| 307 | if (tuple == NULL) { |
| 308 | return NULL; |
| 309 | } |
| 310 | PyObject *new_union = _Py_Union(tuple); |
| 311 | Py_DECREF(tuple); |
| 312 | return new_union; |
| 313 | } |
| 314 | |
| 315 | static int |
| 316 | union_repr_item(_PyUnicodeWriter *writer, PyObject *p) |
| 317 | { |
| 318 | _Py_IDENTIFIER(__module__); |
| 319 | _Py_IDENTIFIER(__qualname__); |
| 320 | _Py_IDENTIFIER(__origin__); |
| 321 | _Py_IDENTIFIER(__args__); |
| 322 | PyObject *qualname = NULL; |
| 323 | PyObject *module = NULL; |
Serhiy Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 324 | PyObject *tmp; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 325 | PyObject *r = NULL; |
| 326 | int err; |
| 327 | |
Serhiy Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 328 | if (_PyObject_LookupAttrId(p, &PyId___origin__, &tmp) < 0) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 329 | goto exit; |
| 330 | } |
| 331 | |
Serhiy Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 332 | if (tmp) { |
| 333 | Py_DECREF(tmp); |
| 334 | if (_PyObject_LookupAttrId(p, &PyId___args__, &tmp) < 0) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 335 | goto exit; |
| 336 | } |
Serhiy Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 337 | if (tmp) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 338 | // It looks like a GenericAlias |
Serhiy Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 339 | Py_DECREF(tmp); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 340 | goto use_repr; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | if (_PyObject_LookupAttrId(p, &PyId___qualname__, &qualname) < 0) { |
| 345 | goto exit; |
| 346 | } |
| 347 | if (qualname == NULL) { |
| 348 | goto use_repr; |
| 349 | } |
| 350 | if (_PyObject_LookupAttrId(p, &PyId___module__, &module) < 0) { |
| 351 | goto exit; |
| 352 | } |
| 353 | if (module == NULL || module == Py_None) { |
| 354 | goto use_repr; |
| 355 | } |
| 356 | |
| 357 | // Looks like a class |
| 358 | if (PyUnicode_Check(module) && |
| 359 | _PyUnicode_EqualToASCIIString(module, "builtins")) |
| 360 | { |
| 361 | // builtins don't need a module name |
| 362 | r = PyObject_Str(qualname); |
| 363 | goto exit; |
| 364 | } |
| 365 | else { |
| 366 | r = PyUnicode_FromFormat("%S.%S", module, qualname); |
| 367 | goto exit; |
| 368 | } |
| 369 | |
| 370 | use_repr: |
| 371 | r = PyObject_Repr(p); |
| 372 | exit: |
| 373 | Py_XDECREF(qualname); |
| 374 | Py_XDECREF(module); |
| 375 | if (r == NULL) { |
| 376 | return -1; |
| 377 | } |
| 378 | err = _PyUnicodeWriter_WriteStr(writer, r); |
| 379 | Py_DECREF(r); |
| 380 | return err; |
| 381 | } |
| 382 | |
| 383 | static PyObject * |
| 384 | union_repr(PyObject *self) |
| 385 | { |
| 386 | unionobject *alias = (unionobject *)self; |
| 387 | Py_ssize_t len = PyTuple_GET_SIZE(alias->args); |
| 388 | |
| 389 | _PyUnicodeWriter writer; |
| 390 | _PyUnicodeWriter_Init(&writer); |
| 391 | for (Py_ssize_t i = 0; i < len; i++) { |
| 392 | if (i > 0 && _PyUnicodeWriter_WriteASCIIString(&writer, " | ", 3) < 0) { |
| 393 | goto error; |
| 394 | } |
| 395 | PyObject *p = PyTuple_GET_ITEM(alias->args, i); |
| 396 | if (union_repr_item(&writer, p) < 0) { |
| 397 | goto error; |
| 398 | } |
| 399 | } |
| 400 | return _PyUnicodeWriter_Finish(&writer); |
| 401 | error: |
| 402 | _PyUnicodeWriter_Dealloc(&writer); |
| 403 | return NULL; |
| 404 | } |
| 405 | |
| 406 | static PyMemberDef union_members[] = { |
| 407 | {"__args__", T_OBJECT, offsetof(unionobject, args), READONLY}, |
| 408 | {0} |
| 409 | }; |
| 410 | |
| 411 | static PyMethodDef union_methods[] = { |
| 412 | {"__instancecheck__", union_instancecheck, METH_O}, |
| 413 | {"__subclasscheck__", union_subclasscheck, METH_O}, |
| 414 | {0}}; |
| 415 | |
| 416 | static PyNumberMethods union_as_number = { |
kj | 4eb41d0 | 2020-11-09 12:00:13 +0800 | [diff] [blame] | 417 | .nb_or = _Py_union_type_or, // Add __or__ function |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 418 | }; |
| 419 | |
| 420 | PyTypeObject _Py_UnionType = { |
| 421 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 422 | .tp_name = "types.Union", |
| 423 | .tp_doc = "Represent a PEP 604 union type\n" |
| 424 | "\n" |
| 425 | "E.g. for int | str", |
| 426 | .tp_basicsize = sizeof(unionobject), |
| 427 | .tp_dealloc = unionobject_dealloc, |
| 428 | .tp_alloc = PyType_GenericAlloc, |
| 429 | .tp_free = PyObject_Del, |
| 430 | .tp_flags = Py_TPFLAGS_DEFAULT, |
| 431 | .tp_hash = union_hash, |
| 432 | .tp_getattro = PyObject_GenericGetAttr, |
| 433 | .tp_members = union_members, |
| 434 | .tp_methods = union_methods, |
| 435 | .tp_richcompare = union_richcompare, |
| 436 | .tp_as_number = &union_as_number, |
| 437 | .tp_repr = union_repr, |
| 438 | }; |
| 439 | |
| 440 | PyObject * |
| 441 | _Py_Union(PyObject *args) |
| 442 | { |
| 443 | assert(PyTuple_CheckExact(args)); |
| 444 | |
| 445 | unionobject* result = NULL; |
| 446 | |
| 447 | // Check arguments are unionable. |
Victor Stinner | d67de0a | 2020-09-23 23:25:54 +0200 | [diff] [blame] | 448 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 449 | for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) { |
| 450 | PyObject *arg = PyTuple_GET_ITEM(args, iarg); |
| 451 | if (arg == NULL) { |
| 452 | return NULL; |
| 453 | } |
| 454 | int is_arg_unionable = is_unionable(arg); |
| 455 | if (is_arg_unionable < 0) { |
| 456 | return NULL; |
| 457 | } |
| 458 | if (!is_arg_unionable) { |
| 459 | Py_INCREF(Py_NotImplemented); |
| 460 | return Py_NotImplemented; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | result = PyObject_New(unionobject, &_Py_UnionType); |
| 465 | if (result == NULL) { |
| 466 | return NULL; |
| 467 | } |
| 468 | |
| 469 | result->args = dedup_and_flatten_args(args); |
| 470 | if (result->args == NULL) { |
| 471 | Py_DECREF(result); |
| 472 | return NULL; |
| 473 | } |
| 474 | return (PyObject*)result; |
| 475 | } |