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 | |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 8 | static PyObject *make_union(PyObject *); |
| 9 | |
| 10 | |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 11 | typedef struct { |
| 12 | PyObject_HEAD |
| 13 | PyObject *args; |
Serhiy Storchaka | 2d055ce | 2021-07-17 22:14:57 +0300 | [diff] [blame] | 14 | PyObject *parameters; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 15 | } unionobject; |
| 16 | |
| 17 | static void |
| 18 | unionobject_dealloc(PyObject *self) |
| 19 | { |
| 20 | unionobject *alias = (unionobject *)self; |
| 21 | |
Miss Islington (bot) | 0856134 | 2021-07-03 06:33:16 -0700 | [diff] [blame] | 22 | _PyObject_GC_UNTRACK(self); |
| 23 | |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 24 | Py_XDECREF(alias->args); |
Serhiy Storchaka | 2d055ce | 2021-07-17 22:14:57 +0300 | [diff] [blame] | 25 | Py_XDECREF(alias->parameters); |
Neil Schemenauer | 0564aaf | 2020-10-27 11:55:52 -0700 | [diff] [blame] | 26 | Py_TYPE(self)->tp_free(self); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Miss Islington (bot) | 0856134 | 2021-07-03 06:33:16 -0700 | [diff] [blame] | 29 | static int |
| 30 | union_traverse(PyObject *self, visitproc visit, void *arg) |
| 31 | { |
| 32 | unionobject *alias = (unionobject *)self; |
| 33 | Py_VISIT(alias->args); |
Serhiy Storchaka | 2d055ce | 2021-07-17 22:14:57 +0300 | [diff] [blame] | 34 | Py_VISIT(alias->parameters); |
Miss Islington (bot) | 0856134 | 2021-07-03 06:33:16 -0700 | [diff] [blame] | 35 | return 0; |
| 36 | } |
| 37 | |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 38 | static Py_hash_t |
| 39 | union_hash(PyObject *self) |
| 40 | { |
| 41 | unionobject *alias = (unionobject *)self; |
Miss Islington (bot) | 7059880 | 2021-07-16 02:02:59 -0700 | [diff] [blame] | 42 | PyObject *args = PyFrozenSet_New(alias->args); |
| 43 | if (args == NULL) { |
| 44 | return (Py_hash_t)-1; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 45 | } |
Miss Islington (bot) | 7059880 | 2021-07-16 02:02:59 -0700 | [diff] [blame] | 46 | Py_hash_t hash = PyObject_Hash(args); |
| 47 | Py_DECREF(args); |
| 48 | return hash; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static int |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 52 | is_generic_alias_in_args(PyObject *args) |
| 53 | { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 54 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 55 | for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) { |
| 56 | PyObject *arg = PyTuple_GET_ITEM(args, iarg); |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 57 | if (_PyGenericAlias_Check(arg)) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 58 | return 0; |
| 59 | } |
| 60 | } |
| 61 | return 1; |
| 62 | } |
| 63 | |
| 64 | static PyObject * |
| 65 | union_instancecheck(PyObject *self, PyObject *instance) |
| 66 | { |
| 67 | unionobject *alias = (unionobject *) self; |
| 68 | Py_ssize_t nargs = PyTuple_GET_SIZE(alias->args); |
| 69 | if (!is_generic_alias_in_args(alias->args)) { |
| 70 | PyErr_SetString(PyExc_TypeError, |
| 71 | "isinstance() argument 2 cannot contain a parameterized generic"); |
| 72 | return NULL; |
| 73 | } |
| 74 | for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) { |
| 75 | PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg); |
Miss Islington (bot) | b42eee7 | 2021-07-13 21:55:45 -0700 | [diff] [blame] | 76 | if (PyType_Check(arg)) { |
| 77 | int res = PyObject_IsInstance(instance, arg); |
| 78 | if (res < 0) { |
| 79 | return NULL; |
| 80 | } |
| 81 | if (res) { |
| 82 | Py_RETURN_TRUE; |
| 83 | } |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | Py_RETURN_FALSE; |
| 87 | } |
| 88 | |
| 89 | static PyObject * |
| 90 | union_subclasscheck(PyObject *self, PyObject *instance) |
| 91 | { |
| 92 | if (!PyType_Check(instance)) { |
| 93 | PyErr_SetString(PyExc_TypeError, "issubclass() arg 1 must be a class"); |
| 94 | return NULL; |
| 95 | } |
| 96 | unionobject *alias = (unionobject *)self; |
| 97 | if (!is_generic_alias_in_args(alias->args)) { |
| 98 | PyErr_SetString(PyExc_TypeError, |
| 99 | "issubclass() argument 2 cannot contain a parameterized generic"); |
| 100 | return NULL; |
| 101 | } |
| 102 | Py_ssize_t nargs = PyTuple_GET_SIZE(alias->args); |
| 103 | for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) { |
| 104 | PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg); |
Miss Islington (bot) | b42eee7 | 2021-07-13 21:55:45 -0700 | [diff] [blame] | 105 | if (PyType_Check(arg)) { |
| 106 | int res = PyObject_IsSubclass(instance, arg); |
| 107 | if (res < 0) { |
| 108 | return NULL; |
| 109 | } |
| 110 | if (res) { |
| 111 | Py_RETURN_TRUE; |
| 112 | } |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 115 | Py_RETURN_FALSE; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static int |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 119 | is_typing_module(PyObject *obj) |
| 120 | { |
| 121 | _Py_IDENTIFIER(__module__); |
| 122 | PyObject *module; |
| 123 | if (_PyObject_LookupAttrId(obj, &PyId___module__, &module) < 0) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 124 | return -1; |
| 125 | } |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 126 | int is_typing = (module != NULL && |
| 127 | PyUnicode_Check(module) && |
| 128 | _PyUnicode_EqualToASCIIString(module, "typing")); |
| 129 | Py_XDECREF(module); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 130 | return is_typing; |
| 131 | } |
| 132 | |
| 133 | static int |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 134 | is_typing_name(PyObject *obj, const char *name) |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 135 | { |
| 136 | PyTypeObject *type = Py_TYPE(obj); |
| 137 | if (strcmp(type->tp_name, name) != 0) { |
| 138 | return 0; |
| 139 | } |
Miss Islington (bot) | cc1a47c | 2021-07-15 00:25:22 -0700 | [diff] [blame] | 140 | return is_typing_module((PyObject *)type); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static PyObject * |
| 144 | union_richcompare(PyObject *a, PyObject *b, int op) |
| 145 | { |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 146 | if (!_PyUnion_Check(b) || (op != Py_EQ && op != Py_NE)) { |
| 147 | Py_RETURN_NOTIMPLEMENTED; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 150 | PyObject *a_set = PySet_New(((unionobject*)a)->args); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 151 | if (a_set == NULL) { |
| 152 | return NULL; |
| 153 | } |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 154 | PyObject *b_set = PySet_New(((unionobject*)b)->args); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 155 | if (b_set == NULL) { |
Serhiy Storchaka | c3007ab | 2021-07-16 14:48:20 +0300 | [diff] [blame] | 156 | Py_DECREF(a_set); |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 157 | return NULL; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 158 | } |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 159 | PyObject *result = PyObject_RichCompare(a_set, b_set, op); |
| 160 | Py_DECREF(b_set); |
| 161 | Py_DECREF(a_set); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 162 | return result; |
| 163 | } |
| 164 | |
| 165 | static PyObject* |
| 166 | flatten_args(PyObject* args) |
| 167 | { |
Victor Stinner | d67de0a | 2020-09-23 23:25:54 +0200 | [diff] [blame] | 168 | Py_ssize_t arg_length = PyTuple_GET_SIZE(args); |
| 169 | Py_ssize_t total_args = 0; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 170 | // Get number of total args once it's flattened. |
| 171 | for (Py_ssize_t i = 0; i < arg_length; i++) { |
| 172 | PyObject *arg = PyTuple_GET_ITEM(args, i); |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 173 | if (_PyUnion_Check(arg)) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 174 | total_args += PyTuple_GET_SIZE(((unionobject*) arg)->args); |
| 175 | } else { |
| 176 | total_args++; |
| 177 | } |
| 178 | } |
| 179 | // Create new tuple of flattened args. |
| 180 | PyObject *flattened_args = PyTuple_New(total_args); |
| 181 | if (flattened_args == NULL) { |
| 182 | return NULL; |
| 183 | } |
| 184 | Py_ssize_t pos = 0; |
| 185 | for (Py_ssize_t i = 0; i < arg_length; i++) { |
| 186 | PyObject *arg = PyTuple_GET_ITEM(args, i); |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 187 | if (_PyUnion_Check(arg)) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 188 | PyObject* nested_args = ((unionobject*)arg)->args; |
Victor Stinner | d73cf7c | 2020-09-26 12:48:41 +0200 | [diff] [blame] | 189 | Py_ssize_t nested_arg_length = PyTuple_GET_SIZE(nested_args); |
| 190 | for (Py_ssize_t j = 0; j < nested_arg_length; j++) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 191 | PyObject* nested_arg = PyTuple_GET_ITEM(nested_args, j); |
| 192 | Py_INCREF(nested_arg); |
| 193 | PyTuple_SET_ITEM(flattened_args, pos, nested_arg); |
| 194 | pos++; |
| 195 | } |
| 196 | } else { |
Serhiy Storchaka | 6dec525 | 2021-07-15 10:15:14 +0300 | [diff] [blame] | 197 | if (arg == Py_None) { |
| 198 | arg = (PyObject *)&_PyNone_Type; |
| 199 | } |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 200 | Py_INCREF(arg); |
| 201 | PyTuple_SET_ITEM(flattened_args, pos, arg); |
| 202 | pos++; |
| 203 | } |
| 204 | } |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 205 | assert(pos == total_args); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 206 | return flattened_args; |
| 207 | } |
| 208 | |
| 209 | static PyObject* |
| 210 | dedup_and_flatten_args(PyObject* args) |
| 211 | { |
| 212 | args = flatten_args(args); |
| 213 | if (args == NULL) { |
| 214 | return NULL; |
| 215 | } |
| 216 | Py_ssize_t arg_length = PyTuple_GET_SIZE(args); |
| 217 | PyObject *new_args = PyTuple_New(arg_length); |
| 218 | if (new_args == NULL) { |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 219 | Py_DECREF(args); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 220 | return NULL; |
| 221 | } |
| 222 | // Add unique elements to an array. |
Victor Stinner | d73cf7c | 2020-09-26 12:48:41 +0200 | [diff] [blame] | 223 | Py_ssize_t added_items = 0; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 224 | for (Py_ssize_t i = 0; i < arg_length; i++) { |
| 225 | int is_duplicate = 0; |
| 226 | PyObject* i_element = PyTuple_GET_ITEM(args, i); |
Serhiy Storchaka | 80844d1 | 2021-07-16 16:42:04 +0300 | [diff] [blame] | 227 | for (Py_ssize_t j = 0; j < added_items; j++) { |
| 228 | PyObject* j_element = PyTuple_GET_ITEM(new_args, j); |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 229 | int is_ga = _PyGenericAlias_Check(i_element) && |
| 230 | _PyGenericAlias_Check(j_element); |
kj | 4eb41d0 | 2020-11-09 12:00:13 +0800 | [diff] [blame] | 231 | // RichCompare to also deduplicate GenericAlias types (slower) |
| 232 | is_duplicate = is_ga ? PyObject_RichCompareBool(i_element, j_element, Py_EQ) |
| 233 | : i_element == j_element; |
| 234 | // Should only happen if RichCompare fails |
| 235 | if (is_duplicate < 0) { |
| 236 | Py_DECREF(args); |
| 237 | Py_DECREF(new_args); |
| 238 | return NULL; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 239 | } |
kj | 4eb41d0 | 2020-11-09 12:00:13 +0800 | [diff] [blame] | 240 | if (is_duplicate) |
| 241 | break; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 242 | } |
| 243 | if (!is_duplicate) { |
| 244 | Py_INCREF(i_element); |
| 245 | PyTuple_SET_ITEM(new_args, added_items, i_element); |
| 246 | added_items++; |
| 247 | } |
| 248 | } |
| 249 | Py_DECREF(args); |
| 250 | _PyTuple_Resize(&new_args, added_items); |
| 251 | return new_args; |
| 252 | } |
| 253 | |
| 254 | static int |
| 255 | is_typevar(PyObject *obj) |
| 256 | { |
| 257 | return is_typing_name(obj, "TypeVar"); |
| 258 | } |
| 259 | |
| 260 | static int |
| 261 | is_special_form(PyObject *obj) |
| 262 | { |
| 263 | return is_typing_name(obj, "_SpecialForm"); |
| 264 | } |
| 265 | |
| 266 | static int |
| 267 | is_new_type(PyObject *obj) |
| 268 | { |
| 269 | PyTypeObject *type = Py_TYPE(obj); |
| 270 | if (type != &PyFunction_Type) { |
| 271 | return 0; |
| 272 | } |
| 273 | return is_typing_module(obj); |
| 274 | } |
| 275 | |
Miss Islington (bot) | 7e6cad7 | 2021-06-23 02:38:49 -0700 | [diff] [blame] | 276 | // Emulates short-circuiting behavior of the ``||`` operator |
| 277 | // while also checking negative values. |
| 278 | #define CHECK_RES(res) { \ |
| 279 | int result = res; \ |
| 280 | if (result) { \ |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 281 | return result; \ |
Miss Islington (bot) | 7e6cad7 | 2021-06-23 02:38:49 -0700 | [diff] [blame] | 282 | } \ |
| 283 | } |
| 284 | |
| 285 | // Returns 1 on true, 0 on false, and -1 on error. |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 286 | static int |
| 287 | is_unionable(PyObject *obj) |
| 288 | { |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 289 | if (obj == Py_None || |
| 290 | PyType_Check(obj) || |
| 291 | _PyGenericAlias_Check(obj) || |
| 292 | _PyUnion_Check(obj)) |
| 293 | { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 294 | return 1; |
| 295 | } |
Miss Islington (bot) | 7e6cad7 | 2021-06-23 02:38:49 -0700 | [diff] [blame] | 296 | CHECK_RES(is_typevar(obj)); |
| 297 | CHECK_RES(is_new_type(obj)); |
| 298 | CHECK_RES(is_special_form(obj)); |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 299 | return 0; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 300 | } |
| 301 | |
kj | 4eb41d0 | 2020-11-09 12:00:13 +0800 | [diff] [blame] | 302 | PyObject * |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 303 | _Py_union_type_or(PyObject* self, PyObject* other) |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 304 | { |
Miss Islington (bot) | 85b5829 | 2021-07-18 04:59:25 -0700 | [diff] [blame] | 305 | int r = is_unionable(self); |
| 306 | if (r > 0) { |
| 307 | r = is_unionable(other); |
| 308 | } |
| 309 | if (r < 0) { |
| 310 | return NULL; |
| 311 | } |
| 312 | if (!r) { |
| 313 | Py_RETURN_NOTIMPLEMENTED; |
| 314 | } |
| 315 | |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 316 | PyObject *tuple = PyTuple_Pack(2, self, other); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 317 | if (tuple == NULL) { |
| 318 | return NULL; |
| 319 | } |
Miss Islington (bot) | 85b5829 | 2021-07-18 04:59:25 -0700 | [diff] [blame] | 320 | |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 321 | PyObject *new_union = make_union(tuple); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 322 | Py_DECREF(tuple); |
| 323 | return new_union; |
| 324 | } |
| 325 | |
| 326 | static int |
| 327 | union_repr_item(_PyUnicodeWriter *writer, PyObject *p) |
| 328 | { |
| 329 | _Py_IDENTIFIER(__module__); |
| 330 | _Py_IDENTIFIER(__qualname__); |
| 331 | _Py_IDENTIFIER(__origin__); |
| 332 | _Py_IDENTIFIER(__args__); |
| 333 | PyObject *qualname = NULL; |
| 334 | PyObject *module = NULL; |
Serhiy Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 335 | PyObject *tmp; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 336 | PyObject *r = NULL; |
| 337 | int err; |
| 338 | |
Serhiy Storchaka | 6dec525 | 2021-07-15 10:15:14 +0300 | [diff] [blame] | 339 | if (p == (PyObject *)&_PyNone_Type) { |
| 340 | return _PyUnicodeWriter_WriteASCIIString(writer, "None", 4); |
| 341 | } |
| 342 | |
Serhiy Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 343 | if (_PyObject_LookupAttrId(p, &PyId___origin__, &tmp) < 0) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 344 | goto exit; |
| 345 | } |
| 346 | |
Serhiy Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 347 | if (tmp) { |
| 348 | Py_DECREF(tmp); |
| 349 | if (_PyObject_LookupAttrId(p, &PyId___args__, &tmp) < 0) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 350 | goto exit; |
| 351 | } |
Serhiy Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 352 | if (tmp) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 353 | // It looks like a GenericAlias |
Serhiy Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 354 | Py_DECREF(tmp); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 355 | goto use_repr; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | if (_PyObject_LookupAttrId(p, &PyId___qualname__, &qualname) < 0) { |
| 360 | goto exit; |
| 361 | } |
| 362 | if (qualname == NULL) { |
| 363 | goto use_repr; |
| 364 | } |
| 365 | if (_PyObject_LookupAttrId(p, &PyId___module__, &module) < 0) { |
| 366 | goto exit; |
| 367 | } |
| 368 | if (module == NULL || module == Py_None) { |
| 369 | goto use_repr; |
| 370 | } |
| 371 | |
| 372 | // Looks like a class |
| 373 | if (PyUnicode_Check(module) && |
| 374 | _PyUnicode_EqualToASCIIString(module, "builtins")) |
| 375 | { |
| 376 | // builtins don't need a module name |
| 377 | r = PyObject_Str(qualname); |
| 378 | goto exit; |
| 379 | } |
| 380 | else { |
| 381 | r = PyUnicode_FromFormat("%S.%S", module, qualname); |
| 382 | goto exit; |
| 383 | } |
| 384 | |
| 385 | use_repr: |
| 386 | r = PyObject_Repr(p); |
| 387 | exit: |
| 388 | Py_XDECREF(qualname); |
| 389 | Py_XDECREF(module); |
| 390 | if (r == NULL) { |
| 391 | return -1; |
| 392 | } |
| 393 | err = _PyUnicodeWriter_WriteStr(writer, r); |
| 394 | Py_DECREF(r); |
| 395 | return err; |
| 396 | } |
| 397 | |
| 398 | static PyObject * |
| 399 | union_repr(PyObject *self) |
| 400 | { |
| 401 | unionobject *alias = (unionobject *)self; |
| 402 | Py_ssize_t len = PyTuple_GET_SIZE(alias->args); |
| 403 | |
| 404 | _PyUnicodeWriter writer; |
| 405 | _PyUnicodeWriter_Init(&writer); |
| 406 | for (Py_ssize_t i = 0; i < len; i++) { |
| 407 | if (i > 0 && _PyUnicodeWriter_WriteASCIIString(&writer, " | ", 3) < 0) { |
| 408 | goto error; |
| 409 | } |
| 410 | PyObject *p = PyTuple_GET_ITEM(alias->args, i); |
| 411 | if (union_repr_item(&writer, p) < 0) { |
| 412 | goto error; |
| 413 | } |
| 414 | } |
| 415 | return _PyUnicodeWriter_Finish(&writer); |
| 416 | error: |
| 417 | _PyUnicodeWriter_Dealloc(&writer); |
| 418 | return NULL; |
| 419 | } |
| 420 | |
| 421 | static PyMemberDef union_members[] = { |
| 422 | {"__args__", T_OBJECT, offsetof(unionobject, args), READONLY}, |
| 423 | {0} |
| 424 | }; |
| 425 | |
| 426 | static PyMethodDef union_methods[] = { |
| 427 | {"__instancecheck__", union_instancecheck, METH_O}, |
| 428 | {"__subclasscheck__", union_subclasscheck, METH_O}, |
| 429 | {0}}; |
| 430 | |
Serhiy Storchaka | 2d055ce | 2021-07-17 22:14:57 +0300 | [diff] [blame] | 431 | |
| 432 | static PyObject * |
| 433 | union_getitem(PyObject *self, PyObject *item) |
| 434 | { |
| 435 | unionobject *alias = (unionobject *)self; |
| 436 | // Populate __parameters__ if needed. |
| 437 | if (alias->parameters == NULL) { |
| 438 | alias->parameters = _Py_make_parameters(alias->args); |
| 439 | if (alias->parameters == NULL) { |
| 440 | return NULL; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | PyObject *newargs = _Py_subs_parameters(self, alias->args, alias->parameters, item); |
| 445 | if (newargs == NULL) { |
| 446 | return NULL; |
| 447 | } |
| 448 | |
Miss Islington (bot) | 85b5829 | 2021-07-18 04:59:25 -0700 | [diff] [blame] | 449 | // Check arguments are unionable. |
| 450 | Py_ssize_t nargs = PyTuple_GET_SIZE(newargs); |
| 451 | for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) { |
| 452 | PyObject *arg = PyTuple_GET_ITEM(newargs, iarg); |
| 453 | int is_arg_unionable = is_unionable(arg); |
| 454 | if (is_arg_unionable <= 0) { |
| 455 | Py_DECREF(newargs); |
| 456 | if (is_arg_unionable == 0) { |
| 457 | PyErr_Format(PyExc_TypeError, |
| 458 | "Each union argument must be a type, got %.100R", arg); |
| 459 | } |
| 460 | return NULL; |
| 461 | } |
| 462 | } |
| 463 | |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 464 | PyObject *res = make_union(newargs); |
Serhiy Storchaka | 2d055ce | 2021-07-17 22:14:57 +0300 | [diff] [blame] | 465 | |
| 466 | Py_DECREF(newargs); |
| 467 | return res; |
| 468 | } |
| 469 | |
| 470 | static PyMappingMethods union_as_mapping = { |
| 471 | .mp_subscript = union_getitem, |
| 472 | }; |
| 473 | |
| 474 | static PyObject * |
| 475 | union_parameters(PyObject *self, void *Py_UNUSED(unused)) |
| 476 | { |
| 477 | unionobject *alias = (unionobject *)self; |
| 478 | if (alias->parameters == NULL) { |
| 479 | alias->parameters = _Py_make_parameters(alias->args); |
| 480 | if (alias->parameters == NULL) { |
| 481 | return NULL; |
| 482 | } |
| 483 | } |
| 484 | Py_INCREF(alias->parameters); |
| 485 | return alias->parameters; |
| 486 | } |
| 487 | |
| 488 | static PyGetSetDef union_properties[] = { |
| 489 | {"__parameters__", union_parameters, (setter)NULL, "Type variables in the types.Union.", NULL}, |
| 490 | {0} |
| 491 | }; |
| 492 | |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 493 | static PyNumberMethods union_as_number = { |
kj | 4eb41d0 | 2020-11-09 12:00:13 +0800 | [diff] [blame] | 494 | .nb_or = _Py_union_type_or, // Add __or__ function |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 495 | }; |
| 496 | |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 497 | PyTypeObject _PyUnion_Type = { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 498 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 499 | .tp_name = "types.Union", |
| 500 | .tp_doc = "Represent a PEP 604 union type\n" |
| 501 | "\n" |
| 502 | "E.g. for int | str", |
| 503 | .tp_basicsize = sizeof(unionobject), |
| 504 | .tp_dealloc = unionobject_dealloc, |
| 505 | .tp_alloc = PyType_GenericAlloc, |
Miss Islington (bot) | 0856134 | 2021-07-03 06:33:16 -0700 | [diff] [blame] | 506 | .tp_free = PyObject_GC_Del, |
| 507 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 508 | .tp_traverse = union_traverse, |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 509 | .tp_hash = union_hash, |
| 510 | .tp_getattro = PyObject_GenericGetAttr, |
| 511 | .tp_members = union_members, |
| 512 | .tp_methods = union_methods, |
| 513 | .tp_richcompare = union_richcompare, |
Serhiy Storchaka | 2d055ce | 2021-07-17 22:14:57 +0300 | [diff] [blame] | 514 | .tp_as_mapping = &union_as_mapping, |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 515 | .tp_as_number = &union_as_number, |
| 516 | .tp_repr = union_repr, |
Serhiy Storchaka | 2d055ce | 2021-07-17 22:14:57 +0300 | [diff] [blame] | 517 | .tp_getset = union_properties, |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 518 | }; |
| 519 | |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 520 | static PyObject * |
| 521 | make_union(PyObject *args) |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 522 | { |
| 523 | assert(PyTuple_CheckExact(args)); |
| 524 | |
Serhiy Storchaka | c3007ab | 2021-07-16 14:48:20 +0300 | [diff] [blame] | 525 | args = dedup_and_flatten_args(args); |
| 526 | if (args == NULL) { |
| 527 | return NULL; |
| 528 | } |
| 529 | if (PyTuple_GET_SIZE(args) == 1) { |
| 530 | PyObject *result1 = PyTuple_GET_ITEM(args, 0); |
| 531 | Py_INCREF(result1); |
| 532 | Py_DECREF(args); |
| 533 | return result1; |
| 534 | } |
| 535 | |
Miss Islington (bot) | 85b5829 | 2021-07-18 04:59:25 -0700 | [diff] [blame] | 536 | unionobject *result = PyObject_GC_New(unionobject, &_PyUnion_Type); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 537 | if (result == NULL) { |
Serhiy Storchaka | c3007ab | 2021-07-16 14:48:20 +0300 | [diff] [blame] | 538 | Py_DECREF(args); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 539 | return NULL; |
| 540 | } |
| 541 | |
Serhiy Storchaka | 2d055ce | 2021-07-17 22:14:57 +0300 | [diff] [blame] | 542 | result->parameters = NULL; |
Serhiy Storchaka | c3007ab | 2021-07-16 14:48:20 +0300 | [diff] [blame] | 543 | result->args = args; |
Miss Islington (bot) | 000b9e8 | 2021-07-03 13:51:10 -0700 | [diff] [blame] | 544 | _PyObject_GC_TRACK(result); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 545 | return (PyObject*)result; |
| 546 | } |