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; |
Miss Islington (bot) | 7059880 | 2021-07-16 02:02:59 -0700 | [diff] [blame] | 36 | PyObject *args = PyFrozenSet_New(alias->args); |
| 37 | if (args == NULL) { |
| 38 | return (Py_hash_t)-1; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 39 | } |
Miss Islington (bot) | 7059880 | 2021-07-16 02:02:59 -0700 | [diff] [blame] | 40 | Py_hash_t hash = PyObject_Hash(args); |
| 41 | Py_DECREF(args); |
| 42 | return hash; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static int |
| 46 | is_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 Jin | 49cd68f | 2021-01-03 00:19:15 +0800 | [diff] [blame] | 50 | if (PyObject_TypeCheck(arg, &Py_GenericAliasType)) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 51 | return 0; |
| 52 | } |
| 53 | } |
| 54 | return 1; |
| 55 | } |
| 56 | |
| 57 | static PyObject * |
| 58 | union_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) | b42eee7 | 2021-07-13 21:55:45 -0700 | [diff] [blame] | 69 | 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 Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | Py_RETURN_FALSE; |
| 80 | } |
| 81 | |
| 82 | static PyObject * |
| 83 | union_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) | b42eee7 | 2021-07-13 21:55:45 -0700 | [diff] [blame] | 98 | 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 Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | Py_RETURN_FALSE; |
| 109 | } |
| 110 | |
| 111 | static int |
| 112 | is_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 | |
| 122 | static int |
| 123 | is_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) | cc1a47c | 2021-07-15 00:25:22 -0700 | [diff] [blame] | 129 | return is_typing_module((PyObject *)type); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static PyObject * |
| 133 | union_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 Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 171 | 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); |
| 192 | exit: |
| 193 | Py_XDECREF(a_set); |
| 194 | Py_XDECREF(b_set); |
| 195 | return result; |
| 196 | } |
| 197 | |
| 198 | static PyObject* |
| 199 | flatten_args(PyObject* args) |
| 200 | { |
Victor Stinner | d67de0a | 2020-09-23 23:25:54 +0200 | [diff] [blame] | 201 | Py_ssize_t arg_length = PyTuple_GET_SIZE(args); |
| 202 | Py_ssize_t total_args = 0; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 203 | // 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 Stinner | d73cf7c | 2020-09-26 12:48:41 +0200 | [diff] [blame] | 224 | Py_ssize_t nested_arg_length = PyTuple_GET_SIZE(nested_args); |
| 225 | for (Py_ssize_t j = 0; j < nested_arg_length; j++) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 226 | 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 Storchaka | 6dec525 | 2021-07-15 10:15:14 +0300 | [diff] [blame] | 232 | if (arg == Py_None) { |
| 233 | arg = (PyObject *)&_PyNone_Type; |
| 234 | } |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 235 | Py_INCREF(arg); |
| 236 | PyTuple_SET_ITEM(flattened_args, pos, arg); |
| 237 | pos++; |
| 238 | } |
| 239 | } |
| 240 | return flattened_args; |
| 241 | } |
| 242 | |
| 243 | static PyObject* |
| 244 | dedup_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 Stinner | d73cf7c | 2020-09-26 12:48:41 +0200 | [diff] [blame] | 256 | Py_ssize_t added_items = 0; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 257 | 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); |
kj | 463c7d3 | 2020-12-14 02:38:24 +0800 | [diff] [blame] | 262 | int is_ga = PyObject_TypeCheck(i_element, &Py_GenericAliasType) && |
| 263 | PyObject_TypeCheck(j_element, &Py_GenericAliasType); |
kj | 4eb41d0 | 2020-11-09 12:00:13 +0800 | [diff] [blame] | 264 | // 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 Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 272 | } |
kj | 4eb41d0 | 2020-11-09 12:00:13 +0800 | [diff] [blame] | 273 | if (is_duplicate) |
| 274 | break; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 275 | } |
| 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 | |
| 287 | static int |
| 288 | is_typevar(PyObject *obj) |
| 289 | { |
| 290 | return is_typing_name(obj, "TypeVar"); |
| 291 | } |
| 292 | |
| 293 | static int |
| 294 | is_special_form(PyObject *obj) |
| 295 | { |
| 296 | return is_typing_name(obj, "_SpecialForm"); |
| 297 | } |
| 298 | |
| 299 | static int |
| 300 | is_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) | 7e6cad7 | 2021-06-23 02:38:49 -0700 | [diff] [blame] | 309 | // 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 Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 319 | static int |
| 320 | is_unionable(PyObject *obj) |
| 321 | { |
| 322 | if (obj == Py_None) { |
| 323 | return 1; |
| 324 | } |
| 325 | PyTypeObject *type = Py_TYPE(obj); |
Miss Islington (bot) | 7e6cad7 | 2021-06-23 02:38:49 -0700 | [diff] [blame] | 326 | CHECK_RES(is_typevar(obj)); |
| 327 | CHECK_RES(is_new_type(obj)); |
| 328 | CHECK_RES(is_special_form(obj)); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 329 | return ( |
Miss Islington (bot) | 7e6cad7 | 2021-06-23 02:38:49 -0700 | [diff] [blame] | 330 | // The following checks never fail. |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 331 | PyType_Check(obj) || |
kj | 463c7d3 | 2020-12-14 02:38:24 +0800 | [diff] [blame] | 332 | PyObject_TypeCheck(obj, &Py_GenericAliasType) || |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 333 | type == &_Py_UnionType); |
| 334 | } |
| 335 | |
kj | 4eb41d0 | 2020-11-09 12:00:13 +0800 | [diff] [blame] | 336 | PyObject * |
| 337 | _Py_union_type_or(PyObject* self, PyObject* param) |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 338 | { |
| 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 | |
| 348 | static int |
| 349 | union_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 Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 357 | PyObject *tmp; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 358 | PyObject *r = NULL; |
| 359 | int err; |
| 360 | |
Serhiy Storchaka | 6dec525 | 2021-07-15 10:15:14 +0300 | [diff] [blame] | 361 | if (p == (PyObject *)&_PyNone_Type) { |
| 362 | return _PyUnicodeWriter_WriteASCIIString(writer, "None", 4); |
| 363 | } |
| 364 | |
Serhiy Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 365 | if (_PyObject_LookupAttrId(p, &PyId___origin__, &tmp) < 0) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 366 | goto exit; |
| 367 | } |
| 368 | |
Serhiy Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 369 | if (tmp) { |
| 370 | Py_DECREF(tmp); |
| 371 | if (_PyObject_LookupAttrId(p, &PyId___args__, &tmp) < 0) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 372 | goto exit; |
| 373 | } |
Serhiy Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 374 | if (tmp) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 375 | // It looks like a GenericAlias |
Serhiy Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 376 | Py_DECREF(tmp); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 377 | 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 | |
| 407 | use_repr: |
| 408 | r = PyObject_Repr(p); |
| 409 | exit: |
| 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 | |
| 420 | static PyObject * |
| 421 | union_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); |
| 438 | error: |
| 439 | _PyUnicodeWriter_Dealloc(&writer); |
| 440 | return NULL; |
| 441 | } |
| 442 | |
| 443 | static PyMemberDef union_members[] = { |
| 444 | {"__args__", T_OBJECT, offsetof(unionobject, args), READONLY}, |
| 445 | {0} |
| 446 | }; |
| 447 | |
| 448 | static PyMethodDef union_methods[] = { |
| 449 | {"__instancecheck__", union_instancecheck, METH_O}, |
| 450 | {"__subclasscheck__", union_subclasscheck, METH_O}, |
| 451 | {0}}; |
| 452 | |
| 453 | static PyNumberMethods union_as_number = { |
kj | 4eb41d0 | 2020-11-09 12:00:13 +0800 | [diff] [blame] | 454 | .nb_or = _Py_union_type_or, // Add __or__ function |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 455 | }; |
| 456 | |
| 457 | PyTypeObject _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) | 0856134 | 2021-07-03 06:33:16 -0700 | [diff] [blame] | 466 | .tp_free = PyObject_GC_Del, |
| 467 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 468 | .tp_traverse = union_traverse, |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 469 | .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 | |
| 478 | PyObject * |
| 479 | _Py_Union(PyObject *args) |
| 480 | { |
| 481 | assert(PyTuple_CheckExact(args)); |
| 482 | |
| 483 | unionobject* result = NULL; |
| 484 | |
| 485 | // Check arguments are unionable. |
Victor Stinner | d67de0a | 2020-09-23 23:25:54 +0200 | [diff] [blame] | 486 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 487 | 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) | 0856134 | 2021-07-03 06:33:16 -0700 | [diff] [blame] | 502 | result = PyObject_GC_New(unionobject, &_Py_UnionType); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 503 | if (result == NULL) { |
| 504 | return NULL; |
| 505 | } |
| 506 | |
| 507 | result->args = dedup_and_flatten_args(args); |
Miss Islington (bot) | 000b9e8 | 2021-07-03 13:51:10 -0700 | [diff] [blame] | 508 | _PyObject_GC_TRACK(result); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 509 | if (result->args == NULL) { |
Miss Islington (bot) | 000b9e8 | 2021-07-03 13:51:10 -0700 | [diff] [blame] | 510 | Py_DECREF(result); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 511 | return NULL; |
| 512 | } |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 513 | return (PyObject*)result; |
| 514 | } |