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" |
Miss Islington (bot) | 0856134 | 2021-07-03 06:33:16 -0700 | [diff] [blame] | 3 | #include "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 4 | #include "pycore_unionobject.h" |
| 5 | #include "structmember.h" |
| 6 | |
| 7 | |
| 8 | typedef struct { |
| 9 | PyObject_HEAD |
| 10 | PyObject *args; |
| 11 | } unionobject; |
| 12 | |
| 13 | static void |
| 14 | unionobject_dealloc(PyObject *self) |
| 15 | { |
| 16 | unionobject *alias = (unionobject *)self; |
| 17 | |
Miss Islington (bot) | 0856134 | 2021-07-03 06:33:16 -0700 | [diff] [blame] | 18 | _PyObject_GC_UNTRACK(self); |
| 19 | |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 20 | Py_XDECREF(alias->args); |
Neil Schemenauer | 0564aaf | 2020-10-27 11:55:52 -0700 | [diff] [blame] | 21 | Py_TYPE(self)->tp_free(self); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 22 | } |
| 23 | |
Miss Islington (bot) | 0856134 | 2021-07-03 06:33:16 -0700 | [diff] [blame] | 24 | static int |
| 25 | union_traverse(PyObject *self, visitproc visit, void *arg) |
| 26 | { |
| 27 | unionobject *alias = (unionobject *)self; |
| 28 | Py_VISIT(alias->args); |
| 29 | return 0; |
| 30 | } |
| 31 | |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 32 | static Py_hash_t |
| 33 | union_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 | |
| 43 | static int |
| 44 | is_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 Jin | 49cd68f | 2021-01-03 00:19:15 +0800 | [diff] [blame] | 48 | if (PyObject_TypeCheck(arg, &Py_GenericAliasType)) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 49 | return 0; |
| 50 | } |
| 51 | } |
| 52 | return 1; |
| 53 | } |
| 54 | |
| 55 | static PyObject * |
| 56 | union_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) | b42eee7 | 2021-07-13 21:55:45 -0700 | [diff] [blame] | 67 | 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 Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | Py_RETURN_FALSE; |
| 78 | } |
| 79 | |
| 80 | static PyObject * |
| 81 | union_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) | b42eee7 | 2021-07-13 21:55:45 -0700 | [diff] [blame] | 96 | 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 Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | Py_RETURN_FALSE; |
| 107 | } |
| 108 | |
| 109 | static int |
| 110 | is_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 | |
| 120 | static int |
| 121 | is_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) | cc1a47c | 2021-07-15 00:25:22 -0700 | [diff] [blame] | 127 | return is_typing_module((PyObject *)type); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static PyObject * |
| 131 | union_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 Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 169 | 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); |
| 190 | exit: |
| 191 | Py_XDECREF(a_set); |
| 192 | Py_XDECREF(b_set); |
| 193 | return result; |
| 194 | } |
| 195 | |
| 196 | static PyObject* |
| 197 | flatten_args(PyObject* args) |
| 198 | { |
Victor Stinner | d67de0a | 2020-09-23 23:25:54 +0200 | [diff] [blame] | 199 | Py_ssize_t arg_length = PyTuple_GET_SIZE(args); |
| 200 | Py_ssize_t total_args = 0; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 201 | // 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 Stinner | d73cf7c | 2020-09-26 12:48:41 +0200 | [diff] [blame] | 222 | Py_ssize_t nested_arg_length = PyTuple_GET_SIZE(nested_args); |
| 223 | for (Py_ssize_t j = 0; j < nested_arg_length; j++) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 224 | 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 Storchaka | 6dec525 | 2021-07-15 10:15:14 +0300 | [diff] [blame] | 230 | if (arg == Py_None) { |
| 231 | arg = (PyObject *)&_PyNone_Type; |
| 232 | } |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 233 | Py_INCREF(arg); |
| 234 | PyTuple_SET_ITEM(flattened_args, pos, arg); |
| 235 | pos++; |
| 236 | } |
| 237 | } |
| 238 | return flattened_args; |
| 239 | } |
| 240 | |
| 241 | static PyObject* |
| 242 | dedup_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 Stinner | d73cf7c | 2020-09-26 12:48:41 +0200 | [diff] [blame] | 254 | Py_ssize_t added_items = 0; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 255 | 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); |
kj | 463c7d3 | 2020-12-14 02:38:24 +0800 | [diff] [blame] | 260 | int is_ga = PyObject_TypeCheck(i_element, &Py_GenericAliasType) && |
| 261 | PyObject_TypeCheck(j_element, &Py_GenericAliasType); |
kj | 4eb41d0 | 2020-11-09 12:00:13 +0800 | [diff] [blame] | 262 | // 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 Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 270 | } |
kj | 4eb41d0 | 2020-11-09 12:00:13 +0800 | [diff] [blame] | 271 | if (is_duplicate) |
| 272 | break; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 273 | } |
| 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 | |
| 285 | static int |
| 286 | is_typevar(PyObject *obj) |
| 287 | { |
| 288 | return is_typing_name(obj, "TypeVar"); |
| 289 | } |
| 290 | |
| 291 | static int |
| 292 | is_special_form(PyObject *obj) |
| 293 | { |
| 294 | return is_typing_name(obj, "_SpecialForm"); |
| 295 | } |
| 296 | |
| 297 | static int |
| 298 | is_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) | 7e6cad7 | 2021-06-23 02:38:49 -0700 | [diff] [blame] | 307 | // 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 Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 317 | static int |
| 318 | is_unionable(PyObject *obj) |
| 319 | { |
| 320 | if (obj == Py_None) { |
| 321 | return 1; |
| 322 | } |
| 323 | PyTypeObject *type = Py_TYPE(obj); |
Miss Islington (bot) | 7e6cad7 | 2021-06-23 02:38:49 -0700 | [diff] [blame] | 324 | CHECK_RES(is_typevar(obj)); |
| 325 | CHECK_RES(is_new_type(obj)); |
| 326 | CHECK_RES(is_special_form(obj)); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 327 | return ( |
Miss Islington (bot) | 7e6cad7 | 2021-06-23 02:38:49 -0700 | [diff] [blame] | 328 | // The following checks never fail. |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 329 | PyType_Check(obj) || |
kj | 463c7d3 | 2020-12-14 02:38:24 +0800 | [diff] [blame] | 330 | PyObject_TypeCheck(obj, &Py_GenericAliasType) || |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 331 | type == &_Py_UnionType); |
| 332 | } |
| 333 | |
kj | 4eb41d0 | 2020-11-09 12:00:13 +0800 | [diff] [blame] | 334 | PyObject * |
| 335 | _Py_union_type_or(PyObject* self, PyObject* param) |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 336 | { |
| 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 | |
| 346 | static int |
| 347 | union_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 Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 355 | PyObject *tmp; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 356 | PyObject *r = NULL; |
| 357 | int err; |
| 358 | |
Serhiy Storchaka | 6dec525 | 2021-07-15 10:15:14 +0300 | [diff] [blame] | 359 | if (p == (PyObject *)&_PyNone_Type) { |
| 360 | return _PyUnicodeWriter_WriteASCIIString(writer, "None", 4); |
| 361 | } |
| 362 | |
Serhiy Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 363 | if (_PyObject_LookupAttrId(p, &PyId___origin__, &tmp) < 0) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 364 | goto exit; |
| 365 | } |
| 366 | |
Serhiy Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 367 | if (tmp) { |
| 368 | Py_DECREF(tmp); |
| 369 | if (_PyObject_LookupAttrId(p, &PyId___args__, &tmp) < 0) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 370 | goto exit; |
| 371 | } |
Serhiy Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 372 | if (tmp) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 373 | // It looks like a GenericAlias |
Serhiy Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 374 | Py_DECREF(tmp); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 375 | 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 | |
| 405 | use_repr: |
| 406 | r = PyObject_Repr(p); |
| 407 | exit: |
| 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 | |
| 418 | static PyObject * |
| 419 | union_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); |
| 436 | error: |
| 437 | _PyUnicodeWriter_Dealloc(&writer); |
| 438 | return NULL; |
| 439 | } |
| 440 | |
| 441 | static PyMemberDef union_members[] = { |
| 442 | {"__args__", T_OBJECT, offsetof(unionobject, args), READONLY}, |
| 443 | {0} |
| 444 | }; |
| 445 | |
| 446 | static PyMethodDef union_methods[] = { |
| 447 | {"__instancecheck__", union_instancecheck, METH_O}, |
| 448 | {"__subclasscheck__", union_subclasscheck, METH_O}, |
| 449 | {0}}; |
| 450 | |
| 451 | static PyNumberMethods union_as_number = { |
kj | 4eb41d0 | 2020-11-09 12:00:13 +0800 | [diff] [blame] | 452 | .nb_or = _Py_union_type_or, // Add __or__ function |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 453 | }; |
| 454 | |
| 455 | PyTypeObject _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) | 0856134 | 2021-07-03 06:33:16 -0700 | [diff] [blame] | 464 | .tp_free = PyObject_GC_Del, |
| 465 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 466 | .tp_traverse = union_traverse, |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 467 | .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 | |
| 476 | PyObject * |
| 477 | _Py_Union(PyObject *args) |
| 478 | { |
| 479 | assert(PyTuple_CheckExact(args)); |
| 480 | |
| 481 | unionobject* result = NULL; |
| 482 | |
| 483 | // Check arguments are unionable. |
Victor Stinner | d67de0a | 2020-09-23 23:25:54 +0200 | [diff] [blame] | 484 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 485 | 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) | 0856134 | 2021-07-03 06:33:16 -0700 | [diff] [blame] | 500 | result = PyObject_GC_New(unionobject, &_Py_UnionType); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 501 | if (result == NULL) { |
| 502 | return NULL; |
| 503 | } |
| 504 | |
| 505 | result->args = dedup_and_flatten_args(args); |
Miss Islington (bot) | 000b9e8 | 2021-07-03 13:51:10 -0700 | [diff] [blame] | 506 | _PyObject_GC_TRACK(result); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 507 | if (result->args == NULL) { |
Miss Islington (bot) | 000b9e8 | 2021-07-03 13:51:10 -0700 | [diff] [blame] | 508 | Py_DECREF(result); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 509 | return NULL; |
| 510 | } |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 511 | return (PyObject*)result; |
| 512 | } |