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 | |
Pablo Galindo Salgado | 9356d1e | 2021-07-24 15:08:53 +0100 | [diff] [blame^] | 302 | static int |
| 303 | is_args_unionable(PyObject *args) |
| 304 | { |
| 305 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 306 | for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) { |
| 307 | PyObject *arg = PyTuple_GET_ITEM(args, iarg); |
| 308 | int is_arg_unionable = is_unionable(arg); |
| 309 | if (is_arg_unionable <= 0) { |
| 310 | if (is_arg_unionable == 0) { |
| 311 | PyErr_Format(PyExc_TypeError, |
| 312 | "Each union argument must be a type, got %.100R", arg); |
| 313 | } |
| 314 | return 0; |
| 315 | } |
| 316 | } |
| 317 | return 1; |
| 318 | } |
| 319 | |
kj | 4eb41d0 | 2020-11-09 12:00:13 +0800 | [diff] [blame] | 320 | PyObject * |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 321 | _Py_union_type_or(PyObject* self, PyObject* other) |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 322 | { |
Miss Islington (bot) | 85b5829 | 2021-07-18 04:59:25 -0700 | [diff] [blame] | 323 | int r = is_unionable(self); |
| 324 | if (r > 0) { |
| 325 | r = is_unionable(other); |
| 326 | } |
| 327 | if (r < 0) { |
| 328 | return NULL; |
| 329 | } |
| 330 | if (!r) { |
| 331 | Py_RETURN_NOTIMPLEMENTED; |
| 332 | } |
| 333 | |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 334 | PyObject *tuple = PyTuple_Pack(2, self, other); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 335 | if (tuple == NULL) { |
| 336 | return NULL; |
| 337 | } |
Miss Islington (bot) | 85b5829 | 2021-07-18 04:59:25 -0700 | [diff] [blame] | 338 | |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 339 | PyObject *new_union = make_union(tuple); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 340 | Py_DECREF(tuple); |
| 341 | return new_union; |
| 342 | } |
| 343 | |
| 344 | static int |
| 345 | union_repr_item(_PyUnicodeWriter *writer, PyObject *p) |
| 346 | { |
| 347 | _Py_IDENTIFIER(__module__); |
| 348 | _Py_IDENTIFIER(__qualname__); |
| 349 | _Py_IDENTIFIER(__origin__); |
| 350 | _Py_IDENTIFIER(__args__); |
| 351 | PyObject *qualname = NULL; |
| 352 | PyObject *module = NULL; |
Serhiy Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 353 | PyObject *tmp; |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 354 | PyObject *r = NULL; |
| 355 | int err; |
| 356 | |
Serhiy Storchaka | 6dec525 | 2021-07-15 10:15:14 +0300 | [diff] [blame] | 357 | if (p == (PyObject *)&_PyNone_Type) { |
| 358 | return _PyUnicodeWriter_WriteASCIIString(writer, "None", 4); |
| 359 | } |
| 360 | |
Serhiy Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 361 | if (_PyObject_LookupAttrId(p, &PyId___origin__, &tmp) < 0) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 362 | goto exit; |
| 363 | } |
| 364 | |
Serhiy Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 365 | if (tmp) { |
| 366 | Py_DECREF(tmp); |
| 367 | if (_PyObject_LookupAttrId(p, &PyId___args__, &tmp) < 0) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 368 | goto exit; |
| 369 | } |
Serhiy Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 370 | if (tmp) { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 371 | // It looks like a GenericAlias |
Serhiy Storchaka | 98c4433 | 2020-10-10 22:23:42 +0300 | [diff] [blame] | 372 | Py_DECREF(tmp); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 373 | goto use_repr; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | if (_PyObject_LookupAttrId(p, &PyId___qualname__, &qualname) < 0) { |
| 378 | goto exit; |
| 379 | } |
| 380 | if (qualname == NULL) { |
| 381 | goto use_repr; |
| 382 | } |
| 383 | if (_PyObject_LookupAttrId(p, &PyId___module__, &module) < 0) { |
| 384 | goto exit; |
| 385 | } |
| 386 | if (module == NULL || module == Py_None) { |
| 387 | goto use_repr; |
| 388 | } |
| 389 | |
| 390 | // Looks like a class |
| 391 | if (PyUnicode_Check(module) && |
| 392 | _PyUnicode_EqualToASCIIString(module, "builtins")) |
| 393 | { |
| 394 | // builtins don't need a module name |
| 395 | r = PyObject_Str(qualname); |
| 396 | goto exit; |
| 397 | } |
| 398 | else { |
| 399 | r = PyUnicode_FromFormat("%S.%S", module, qualname); |
| 400 | goto exit; |
| 401 | } |
| 402 | |
| 403 | use_repr: |
| 404 | r = PyObject_Repr(p); |
| 405 | exit: |
| 406 | Py_XDECREF(qualname); |
| 407 | Py_XDECREF(module); |
| 408 | if (r == NULL) { |
| 409 | return -1; |
| 410 | } |
| 411 | err = _PyUnicodeWriter_WriteStr(writer, r); |
| 412 | Py_DECREF(r); |
| 413 | return err; |
| 414 | } |
| 415 | |
| 416 | static PyObject * |
| 417 | union_repr(PyObject *self) |
| 418 | { |
| 419 | unionobject *alias = (unionobject *)self; |
| 420 | Py_ssize_t len = PyTuple_GET_SIZE(alias->args); |
| 421 | |
| 422 | _PyUnicodeWriter writer; |
| 423 | _PyUnicodeWriter_Init(&writer); |
| 424 | for (Py_ssize_t i = 0; i < len; i++) { |
| 425 | if (i > 0 && _PyUnicodeWriter_WriteASCIIString(&writer, " | ", 3) < 0) { |
| 426 | goto error; |
| 427 | } |
| 428 | PyObject *p = PyTuple_GET_ITEM(alias->args, i); |
| 429 | if (union_repr_item(&writer, p) < 0) { |
| 430 | goto error; |
| 431 | } |
| 432 | } |
| 433 | return _PyUnicodeWriter_Finish(&writer); |
| 434 | error: |
| 435 | _PyUnicodeWriter_Dealloc(&writer); |
| 436 | return NULL; |
| 437 | } |
| 438 | |
Pablo Galindo Salgado | 9356d1e | 2021-07-24 15:08:53 +0100 | [diff] [blame^] | 439 | static PyObject * |
| 440 | union_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 441 | { |
| 442 | unionobject *alias = (unionobject *)self; |
| 443 | PyObject* from_args = PyObject_GetAttrString(self, "_from_args"); |
| 444 | if (from_args == NULL) { |
| 445 | return NULL; |
| 446 | } |
| 447 | |
| 448 | return Py_BuildValue("N(O)", from_args, alias->args); |
| 449 | } |
| 450 | |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 451 | static PyMemberDef union_members[] = { |
| 452 | {"__args__", T_OBJECT, offsetof(unionobject, args), READONLY}, |
| 453 | {0} |
| 454 | }; |
| 455 | |
Pablo Galindo Salgado | 9356d1e | 2021-07-24 15:08:53 +0100 | [diff] [blame^] | 456 | static PyObject * |
| 457 | union_from_args(PyObject *cls, PyObject *args) |
| 458 | { |
| 459 | if (!PyTuple_CheckExact(args)) { |
| 460 | _PyArg_BadArgument("Union._from_args", "argument 'args'", "tuple", args); |
| 461 | return NULL; |
| 462 | } |
| 463 | if (!PyTuple_GET_SIZE(args)) { |
| 464 | PyErr_SetString(PyExc_ValueError, "args must be not empty"); |
| 465 | return NULL; |
| 466 | } |
| 467 | |
| 468 | if (is_args_unionable(args) <= 0) { |
| 469 | return NULL; |
| 470 | } |
| 471 | |
| 472 | return make_union(args); |
| 473 | } |
| 474 | |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 475 | static PyMethodDef union_methods[] = { |
Pablo Galindo Salgado | 9356d1e | 2021-07-24 15:08:53 +0100 | [diff] [blame^] | 476 | {"_from_args", union_from_args, METH_O | METH_CLASS}, |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 477 | {"__instancecheck__", union_instancecheck, METH_O}, |
| 478 | {"__subclasscheck__", union_subclasscheck, METH_O}, |
Pablo Galindo Salgado | 9356d1e | 2021-07-24 15:08:53 +0100 | [diff] [blame^] | 479 | {"__reduce__", union_reduce, METH_NOARGS}, |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 480 | {0}}; |
| 481 | |
Serhiy Storchaka | 2d055ce | 2021-07-17 22:14:57 +0300 | [diff] [blame] | 482 | |
| 483 | static PyObject * |
| 484 | union_getitem(PyObject *self, PyObject *item) |
| 485 | { |
| 486 | unionobject *alias = (unionobject *)self; |
| 487 | // Populate __parameters__ if needed. |
| 488 | if (alias->parameters == NULL) { |
| 489 | alias->parameters = _Py_make_parameters(alias->args); |
| 490 | if (alias->parameters == NULL) { |
| 491 | return NULL; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | PyObject *newargs = _Py_subs_parameters(self, alias->args, alias->parameters, item); |
| 496 | if (newargs == NULL) { |
| 497 | return NULL; |
| 498 | } |
| 499 | |
Miss Islington (bot) | 21db59f | 2021-07-22 15:18:49 -0700 | [diff] [blame] | 500 | PyObject *res; |
Miss Islington (bot) | 85b5829 | 2021-07-18 04:59:25 -0700 | [diff] [blame] | 501 | Py_ssize_t nargs = PyTuple_GET_SIZE(newargs); |
Miss Islington (bot) | 21db59f | 2021-07-22 15:18:49 -0700 | [diff] [blame] | 502 | if (nargs == 0) { |
| 503 | res = make_union(newargs); |
| 504 | } |
| 505 | else { |
| 506 | res = PyTuple_GET_ITEM(newargs, 0); |
| 507 | Py_INCREF(res); |
| 508 | for (Py_ssize_t iarg = 1; iarg < nargs; iarg++) { |
| 509 | PyObject *arg = PyTuple_GET_ITEM(newargs, iarg); |
| 510 | Py_SETREF(res, PyNumber_Or(res, arg)); |
| 511 | if (res == NULL) { |
| 512 | break; |
Miss Islington (bot) | 85b5829 | 2021-07-18 04:59:25 -0700 | [diff] [blame] | 513 | } |
Miss Islington (bot) | 85b5829 | 2021-07-18 04:59:25 -0700 | [diff] [blame] | 514 | } |
| 515 | } |
Serhiy Storchaka | 2d055ce | 2021-07-17 22:14:57 +0300 | [diff] [blame] | 516 | Py_DECREF(newargs); |
| 517 | return res; |
| 518 | } |
| 519 | |
| 520 | static PyMappingMethods union_as_mapping = { |
| 521 | .mp_subscript = union_getitem, |
| 522 | }; |
| 523 | |
| 524 | static PyObject * |
| 525 | union_parameters(PyObject *self, void *Py_UNUSED(unused)) |
| 526 | { |
| 527 | unionobject *alias = (unionobject *)self; |
| 528 | if (alias->parameters == NULL) { |
| 529 | alias->parameters = _Py_make_parameters(alias->args); |
| 530 | if (alias->parameters == NULL) { |
| 531 | return NULL; |
| 532 | } |
| 533 | } |
| 534 | Py_INCREF(alias->parameters); |
| 535 | return alias->parameters; |
| 536 | } |
| 537 | |
| 538 | static PyGetSetDef union_properties[] = { |
| 539 | {"__parameters__", union_parameters, (setter)NULL, "Type variables in the types.Union.", NULL}, |
| 540 | {0} |
| 541 | }; |
| 542 | |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 543 | static PyNumberMethods union_as_number = { |
kj | 4eb41d0 | 2020-11-09 12:00:13 +0800 | [diff] [blame] | 544 | .nb_or = _Py_union_type_or, // Add __or__ function |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 545 | }; |
| 546 | |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 547 | PyTypeObject _PyUnion_Type = { |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 548 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 549 | .tp_name = "types.Union", |
| 550 | .tp_doc = "Represent a PEP 604 union type\n" |
| 551 | "\n" |
| 552 | "E.g. for int | str", |
| 553 | .tp_basicsize = sizeof(unionobject), |
| 554 | .tp_dealloc = unionobject_dealloc, |
| 555 | .tp_alloc = PyType_GenericAlloc, |
Miss Islington (bot) | 0856134 | 2021-07-03 06:33:16 -0700 | [diff] [blame] | 556 | .tp_free = PyObject_GC_Del, |
| 557 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 558 | .tp_traverse = union_traverse, |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 559 | .tp_hash = union_hash, |
| 560 | .tp_getattro = PyObject_GenericGetAttr, |
| 561 | .tp_members = union_members, |
| 562 | .tp_methods = union_methods, |
| 563 | .tp_richcompare = union_richcompare, |
Serhiy Storchaka | 2d055ce | 2021-07-17 22:14:57 +0300 | [diff] [blame] | 564 | .tp_as_mapping = &union_as_mapping, |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 565 | .tp_as_number = &union_as_number, |
| 566 | .tp_repr = union_repr, |
Serhiy Storchaka | 2d055ce | 2021-07-17 22:14:57 +0300 | [diff] [blame] | 567 | .tp_getset = union_properties, |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 568 | }; |
| 569 | |
Miss Islington (bot) | 03aad30 | 2021-07-17 14:10:21 -0700 | [diff] [blame] | 570 | static PyObject * |
| 571 | make_union(PyObject *args) |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 572 | { |
| 573 | assert(PyTuple_CheckExact(args)); |
| 574 | |
Serhiy Storchaka | c3007ab | 2021-07-16 14:48:20 +0300 | [diff] [blame] | 575 | args = dedup_and_flatten_args(args); |
| 576 | if (args == NULL) { |
| 577 | return NULL; |
| 578 | } |
| 579 | if (PyTuple_GET_SIZE(args) == 1) { |
| 580 | PyObject *result1 = PyTuple_GET_ITEM(args, 0); |
| 581 | Py_INCREF(result1); |
| 582 | Py_DECREF(args); |
| 583 | return result1; |
| 584 | } |
| 585 | |
Miss Islington (bot) | 85b5829 | 2021-07-18 04:59:25 -0700 | [diff] [blame] | 586 | unionobject *result = PyObject_GC_New(unionobject, &_PyUnion_Type); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 587 | if (result == NULL) { |
Serhiy Storchaka | c3007ab | 2021-07-16 14:48:20 +0300 | [diff] [blame] | 588 | Py_DECREF(args); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 589 | return NULL; |
| 590 | } |
| 591 | |
Serhiy Storchaka | 2d055ce | 2021-07-17 22:14:57 +0300 | [diff] [blame] | 592 | result->parameters = NULL; |
Serhiy Storchaka | c3007ab | 2021-07-16 14:48:20 +0300 | [diff] [blame] | 593 | result->args = args; |
Miss Islington (bot) | 000b9e8 | 2021-07-03 13:51:10 -0700 | [diff] [blame] | 594 | _PyObject_GC_TRACK(result); |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 595 | return (PyObject*)result; |
| 596 | } |