Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 1 | // types.GenericAlias -- used to represent e.g. list[int]. |
| 2 | |
| 3 | #include "Python.h" |
| 4 | #include "pycore_object.h" |
kj | 4eb41d0 | 2020-11-09 12:00:13 +0800 | [diff] [blame] | 5 | #include "pycore_unionobject.h" // _Py_union_as_number |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 6 | #include "structmember.h" // PyMemberDef |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 7 | |
| 8 | typedef struct { |
| 9 | PyObject_HEAD |
| 10 | PyObject *origin; |
| 11 | PyObject *args; |
| 12 | PyObject *parameters; |
kj | 384b7a4 | 2020-11-16 11:27:23 +0800 | [diff] [blame] | 13 | PyObject* weakreflist; |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 14 | } gaobject; |
| 15 | |
| 16 | static void |
| 17 | ga_dealloc(PyObject *self) |
| 18 | { |
| 19 | gaobject *alias = (gaobject *)self; |
| 20 | |
| 21 | _PyObject_GC_UNTRACK(self); |
kj | 384b7a4 | 2020-11-16 11:27:23 +0800 | [diff] [blame] | 22 | if (alias->weakreflist != NULL) { |
| 23 | PyObject_ClearWeakRefs((PyObject *)alias); |
| 24 | } |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 25 | Py_XDECREF(alias->origin); |
| 26 | Py_XDECREF(alias->args); |
| 27 | Py_XDECREF(alias->parameters); |
Victor Stinner | 8182cc2 | 2020-07-10 12:40:38 +0200 | [diff] [blame] | 28 | Py_TYPE(self)->tp_free(self); |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | static int |
| 32 | ga_traverse(PyObject *self, visitproc visit, void *arg) |
| 33 | { |
| 34 | gaobject *alias = (gaobject *)self; |
| 35 | Py_VISIT(alias->origin); |
| 36 | Py_VISIT(alias->args); |
| 37 | Py_VISIT(alias->parameters); |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | static int |
| 42 | ga_repr_item(_PyUnicodeWriter *writer, PyObject *p) |
| 43 | { |
| 44 | _Py_IDENTIFIER(__module__); |
| 45 | _Py_IDENTIFIER(__qualname__); |
| 46 | _Py_IDENTIFIER(__origin__); |
| 47 | _Py_IDENTIFIER(__args__); |
| 48 | PyObject *qualname = NULL; |
| 49 | PyObject *module = NULL; |
| 50 | PyObject *r = NULL; |
| 51 | PyObject *tmp; |
| 52 | int err; |
| 53 | |
| 54 | if (p == Py_Ellipsis) { |
| 55 | // The Ellipsis object |
| 56 | r = PyUnicode_FromString("..."); |
| 57 | goto done; |
| 58 | } |
| 59 | |
| 60 | if (_PyObject_LookupAttrId(p, &PyId___origin__, &tmp) < 0) { |
| 61 | goto done; |
| 62 | } |
| 63 | if (tmp != NULL) { |
| 64 | Py_DECREF(tmp); |
| 65 | if (_PyObject_LookupAttrId(p, &PyId___args__, &tmp) < 0) { |
| 66 | goto done; |
| 67 | } |
| 68 | if (tmp != NULL) { |
| 69 | Py_DECREF(tmp); |
| 70 | // It looks like a GenericAlias |
| 71 | goto use_repr; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (_PyObject_LookupAttrId(p, &PyId___qualname__, &qualname) < 0) { |
| 76 | goto done; |
| 77 | } |
| 78 | if (qualname == NULL) { |
| 79 | goto use_repr; |
| 80 | } |
| 81 | if (_PyObject_LookupAttrId(p, &PyId___module__, &module) < 0) { |
| 82 | goto done; |
| 83 | } |
| 84 | if (module == NULL || module == Py_None) { |
| 85 | goto use_repr; |
| 86 | } |
| 87 | |
| 88 | // Looks like a class |
| 89 | if (PyUnicode_Check(module) && |
| 90 | _PyUnicode_EqualToASCIIString(module, "builtins")) |
| 91 | { |
| 92 | // builtins don't need a module name |
| 93 | r = PyObject_Str(qualname); |
| 94 | goto done; |
| 95 | } |
| 96 | else { |
| 97 | r = PyUnicode_FromFormat("%S.%S", module, qualname); |
| 98 | goto done; |
| 99 | } |
| 100 | |
| 101 | use_repr: |
| 102 | r = PyObject_Repr(p); |
| 103 | |
| 104 | done: |
| 105 | Py_XDECREF(qualname); |
| 106 | Py_XDECREF(module); |
| 107 | if (r == NULL) { |
| 108 | // error if any of the above PyObject_Repr/PyUnicode_From* fail |
| 109 | err = -1; |
| 110 | } |
| 111 | else { |
| 112 | err = _PyUnicodeWriter_WriteStr(writer, r); |
| 113 | Py_DECREF(r); |
| 114 | } |
| 115 | return err; |
| 116 | } |
| 117 | |
| 118 | static PyObject * |
| 119 | ga_repr(PyObject *self) |
| 120 | { |
| 121 | gaobject *alias = (gaobject *)self; |
| 122 | Py_ssize_t len = PyTuple_GET_SIZE(alias->args); |
| 123 | |
| 124 | _PyUnicodeWriter writer; |
| 125 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 126 | |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 127 | if (ga_repr_item(&writer, alias->origin) < 0) { |
| 128 | goto error; |
| 129 | } |
| 130 | if (_PyUnicodeWriter_WriteASCIIString(&writer, "[", 1) < 0) { |
| 131 | goto error; |
| 132 | } |
| 133 | for (Py_ssize_t i = 0; i < len; i++) { |
| 134 | if (i > 0) { |
| 135 | if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) { |
| 136 | goto error; |
| 137 | } |
| 138 | } |
| 139 | PyObject *p = PyTuple_GET_ITEM(alias->args, i); |
| 140 | if (ga_repr_item(&writer, p) < 0) { |
| 141 | goto error; |
| 142 | } |
| 143 | } |
| 144 | if (len == 0) { |
| 145 | // for something like tuple[()] we should print a "()" |
| 146 | if (_PyUnicodeWriter_WriteASCIIString(&writer, "()", 2) < 0) { |
| 147 | goto error; |
| 148 | } |
| 149 | } |
| 150 | if (_PyUnicodeWriter_WriteASCIIString(&writer, "]", 1) < 0) { |
| 151 | goto error; |
| 152 | } |
| 153 | return _PyUnicodeWriter_Finish(&writer); |
| 154 | error: |
| 155 | _PyUnicodeWriter_Dealloc(&writer); |
| 156 | return NULL; |
| 157 | } |
| 158 | |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 159 | /* Checks if a variable number of names are from typing.py. |
| 160 | * If any one of the names are found, return 1, else 0. |
| 161 | **/ |
| 162 | static inline int |
| 163 | is_typing_name(PyObject *obj, int num, ...) |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 164 | { |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 165 | va_list names; |
| 166 | va_start(names, num); |
| 167 | |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 168 | PyTypeObject *type = Py_TYPE(obj); |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 169 | int hit = 0; |
| 170 | for (int i = 0; i < num; ++i) { |
| 171 | if (!strcmp(type->tp_name, va_arg(names, const char *))) { |
| 172 | hit = 1; |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | if (!hit) { |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | PyObject *module = PyObject_GetAttrString((PyObject *)type, "__module__"); |
| 180 | if (module == NULL) { |
| 181 | return -1; |
| 182 | } |
| 183 | int res = PyUnicode_Check(module) |
| 184 | && _PyUnicode_EqualToASCIIString(module, "typing"); |
| 185 | Py_DECREF(module); |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 186 | |
| 187 | va_end(names); |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 188 | return res; |
| 189 | } |
| 190 | |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 191 | // isinstance(obj, (TypeVar, ParamSpec)) without importing typing.py. |
| 192 | // Returns -1 for errors. |
| 193 | static inline int |
| 194 | is_typevarlike(PyObject *obj) |
| 195 | { |
| 196 | return is_typing_name(obj, 2, "TypeVar", "ParamSpec"); |
| 197 | } |
| 198 | |
| 199 | static inline int |
| 200 | is_paramspec(PyObject *obj) |
| 201 | { |
| 202 | return is_typing_name(obj, 1, "ParamSpec"); |
| 203 | } |
| 204 | |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 205 | // Index of item in self[:len], or -1 if not found (self is a tuple) |
| 206 | static Py_ssize_t |
| 207 | tuple_index(PyObject *self, Py_ssize_t len, PyObject *item) |
| 208 | { |
| 209 | for (Py_ssize_t i = 0; i < len; i++) { |
| 210 | if (PyTuple_GET_ITEM(self, i) == item) { |
| 211 | return i; |
| 212 | } |
| 213 | } |
| 214 | return -1; |
| 215 | } |
| 216 | |
Serhiy Storchaka | 41a6458 | 2020-05-04 10:56:05 +0300 | [diff] [blame] | 217 | static int |
| 218 | tuple_add(PyObject *self, Py_ssize_t len, PyObject *item) |
| 219 | { |
| 220 | if (tuple_index(self, len, item) < 0) { |
| 221 | Py_INCREF(item); |
| 222 | PyTuple_SET_ITEM(self, len, item); |
| 223 | return 1; |
| 224 | } |
| 225 | return 0; |
| 226 | } |
| 227 | |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 228 | static PyObject * |
| 229 | make_parameters(PyObject *args) |
| 230 | { |
Serhiy Storchaka | 41a6458 | 2020-05-04 10:56:05 +0300 | [diff] [blame] | 231 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 232 | Py_ssize_t len = nargs; |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 233 | PyObject *parameters = PyTuple_New(len); |
| 234 | if (parameters == NULL) |
| 235 | return NULL; |
| 236 | Py_ssize_t iparam = 0; |
Serhiy Storchaka | 41a6458 | 2020-05-04 10:56:05 +0300 | [diff] [blame] | 237 | for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) { |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 238 | PyObject *t = PyTuple_GET_ITEM(args, iarg); |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 239 | int typevar = is_typevarlike(t); |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 240 | if (typevar < 0) { |
Serhiy Storchaka | 41a6458 | 2020-05-04 10:56:05 +0300 | [diff] [blame] | 241 | Py_DECREF(parameters); |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 242 | return NULL; |
| 243 | } |
| 244 | if (typevar) { |
Serhiy Storchaka | 41a6458 | 2020-05-04 10:56:05 +0300 | [diff] [blame] | 245 | iparam += tuple_add(parameters, iparam, t); |
| 246 | } |
| 247 | else { |
| 248 | _Py_IDENTIFIER(__parameters__); |
| 249 | PyObject *subparams; |
| 250 | if (_PyObject_LookupAttrId(t, &PyId___parameters__, &subparams) < 0) { |
| 251 | Py_DECREF(parameters); |
| 252 | return NULL; |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 253 | } |
Serhiy Storchaka | 41a6458 | 2020-05-04 10:56:05 +0300 | [diff] [blame] | 254 | if (subparams && PyTuple_Check(subparams)) { |
| 255 | Py_ssize_t len2 = PyTuple_GET_SIZE(subparams); |
| 256 | Py_ssize_t needed = len2 - 1 - (iarg - iparam); |
| 257 | if (needed > 0) { |
| 258 | len += needed; |
| 259 | if (_PyTuple_Resize(¶meters, len) < 0) { |
| 260 | Py_DECREF(subparams); |
| 261 | Py_DECREF(parameters); |
| 262 | return NULL; |
| 263 | } |
| 264 | } |
| 265 | for (Py_ssize_t j = 0; j < len2; j++) { |
| 266 | PyObject *t2 = PyTuple_GET_ITEM(subparams, j); |
| 267 | iparam += tuple_add(parameters, iparam, t2); |
| 268 | } |
| 269 | } |
| 270 | Py_XDECREF(subparams); |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | if (iparam < len) { |
| 274 | if (_PyTuple_Resize(¶meters, iparam) < 0) { |
| 275 | Py_XDECREF(parameters); |
| 276 | return NULL; |
| 277 | } |
| 278 | } |
| 279 | return parameters; |
| 280 | } |
| 281 | |
Serhiy Storchaka | 41a6458 | 2020-05-04 10:56:05 +0300 | [diff] [blame] | 282 | /* If obj is a generic alias, substitute type variables params |
| 283 | with substitutions argitems. For example, if obj is list[T], |
| 284 | params is (T, S), and argitems is (str, int), return list[str]. |
| 285 | If obj doesn't have a __parameters__ attribute or that's not |
| 286 | a non-empty tuple, return a new reference to obj. */ |
| 287 | static PyObject * |
| 288 | subs_tvars(PyObject *obj, PyObject *params, PyObject **argitems) |
| 289 | { |
| 290 | _Py_IDENTIFIER(__parameters__); |
| 291 | PyObject *subparams; |
| 292 | if (_PyObject_LookupAttrId(obj, &PyId___parameters__, &subparams) < 0) { |
| 293 | return NULL; |
| 294 | } |
| 295 | if (subparams && PyTuple_Check(subparams) && PyTuple_GET_SIZE(subparams)) { |
| 296 | Py_ssize_t nparams = PyTuple_GET_SIZE(params); |
| 297 | Py_ssize_t nsubargs = PyTuple_GET_SIZE(subparams); |
| 298 | PyObject *subargs = PyTuple_New(nsubargs); |
| 299 | if (subargs == NULL) { |
| 300 | Py_DECREF(subparams); |
| 301 | return NULL; |
| 302 | } |
| 303 | for (Py_ssize_t i = 0; i < nsubargs; ++i) { |
| 304 | PyObject *arg = PyTuple_GET_ITEM(subparams, i); |
| 305 | Py_ssize_t iparam = tuple_index(params, nparams, arg); |
| 306 | if (iparam >= 0) { |
| 307 | arg = argitems[iparam]; |
| 308 | } |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 309 | // convert all the lists inside args to tuples to help |
| 310 | // with caching in other libaries |
| 311 | if (PyList_CheckExact(arg)) { |
| 312 | arg = PyList_AsTuple(arg); |
| 313 | } |
| 314 | else { |
| 315 | Py_INCREF(arg); |
| 316 | } |
Serhiy Storchaka | 41a6458 | 2020-05-04 10:56:05 +0300 | [diff] [blame] | 317 | PyTuple_SET_ITEM(subargs, i, arg); |
| 318 | } |
| 319 | |
| 320 | obj = PyObject_GetItem(obj, subargs); |
| 321 | |
| 322 | Py_DECREF(subargs); |
| 323 | } |
| 324 | else { |
| 325 | Py_INCREF(obj); |
| 326 | } |
| 327 | Py_XDECREF(subparams); |
| 328 | return obj; |
| 329 | } |
| 330 | |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 331 | static PyObject * |
| 332 | ga_getitem(PyObject *self, PyObject *item) |
| 333 | { |
| 334 | gaobject *alias = (gaobject *)self; |
| 335 | // do a lookup for __parameters__ so it gets populated (if not already) |
| 336 | if (alias->parameters == NULL) { |
| 337 | alias->parameters = make_parameters(alias->args); |
| 338 | if (alias->parameters == NULL) { |
| 339 | return NULL; |
| 340 | } |
| 341 | } |
| 342 | Py_ssize_t nparams = PyTuple_GET_SIZE(alias->parameters); |
| 343 | if (nparams == 0) { |
| 344 | return PyErr_Format(PyExc_TypeError, |
| 345 | "There are no type variables left in %R", |
| 346 | self); |
| 347 | } |
| 348 | int is_tuple = PyTuple_Check(item); |
Serhiy Storchaka | 41a6458 | 2020-05-04 10:56:05 +0300 | [diff] [blame] | 349 | Py_ssize_t nitems = is_tuple ? PyTuple_GET_SIZE(item) : 1; |
| 350 | PyObject **argitems = is_tuple ? &PyTuple_GET_ITEM(item, 0) : &item; |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 351 | // A special case in PEP 612 where if X = Callable[P, int], |
| 352 | // then X[int, str] == X[[int, str]]. |
| 353 | if (nparams == 1 && nitems > 1 && is_tuple && |
| 354 | is_paramspec(PyTuple_GET_ITEM(alias->parameters, 0))) { |
| 355 | argitems = &item; |
| 356 | } |
| 357 | else { |
| 358 | if (nitems != nparams) { |
| 359 | return PyErr_Format(PyExc_TypeError, |
| 360 | "Too %s arguments for %R", |
| 361 | nitems > nparams ? "many" : "few", |
| 362 | self); |
| 363 | } |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 364 | } |
Serhiy Storchaka | 41a6458 | 2020-05-04 10:56:05 +0300 | [diff] [blame] | 365 | /* Replace all type variables (specified by alias->parameters) |
| 366 | with corresponding values specified by argitems. |
| 367 | t = list[T]; t[int] -> newargs = [int] |
| 368 | t = dict[str, T]; t[int] -> newargs = [str, int] |
| 369 | t = dict[T, list[S]]; t[str, int] -> newargs = [str, list[int]] |
| 370 | */ |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 371 | Py_ssize_t nargs = PyTuple_GET_SIZE(alias->args); |
| 372 | PyObject *newargs = PyTuple_New(nargs); |
Serhiy Storchaka | 41a6458 | 2020-05-04 10:56:05 +0300 | [diff] [blame] | 373 | if (newargs == NULL) { |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 374 | return NULL; |
Serhiy Storchaka | 41a6458 | 2020-05-04 10:56:05 +0300 | [diff] [blame] | 375 | } |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 376 | for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) { |
| 377 | PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg); |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 378 | int typevar = is_typevarlike(arg); |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 379 | if (typevar < 0) { |
| 380 | Py_DECREF(newargs); |
| 381 | return NULL; |
| 382 | } |
| 383 | if (typevar) { |
| 384 | Py_ssize_t iparam = tuple_index(alias->parameters, nparams, arg); |
| 385 | assert(iparam >= 0); |
Serhiy Storchaka | 41a6458 | 2020-05-04 10:56:05 +0300 | [diff] [blame] | 386 | arg = argitems[iparam]; |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 387 | // convert lists to tuples to help with caching in other libaries. |
| 388 | if (PyList_CheckExact(arg)) { |
| 389 | arg = PyList_AsTuple(arg); |
| 390 | } |
| 391 | else { |
| 392 | Py_INCREF(arg); |
| 393 | } |
Serhiy Storchaka | 41a6458 | 2020-05-04 10:56:05 +0300 | [diff] [blame] | 394 | } |
| 395 | else { |
| 396 | arg = subs_tvars(arg, alias->parameters, argitems); |
| 397 | if (arg == NULL) { |
| 398 | Py_DECREF(newargs); |
| 399 | return NULL; |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 400 | } |
| 401 | } |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 402 | PyTuple_SET_ITEM(newargs, iarg, arg); |
| 403 | } |
Serhiy Storchaka | 41a6458 | 2020-05-04 10:56:05 +0300 | [diff] [blame] | 404 | |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 405 | PyObject *res = Py_GenericAlias(alias->origin, newargs); |
Serhiy Storchaka | 41a6458 | 2020-05-04 10:56:05 +0300 | [diff] [blame] | 406 | |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 407 | Py_DECREF(newargs); |
| 408 | return res; |
| 409 | } |
| 410 | |
| 411 | static PyMappingMethods ga_as_mapping = { |
| 412 | .mp_subscript = ga_getitem, |
| 413 | }; |
| 414 | |
| 415 | static Py_hash_t |
| 416 | ga_hash(PyObject *self) |
| 417 | { |
| 418 | gaobject *alias = (gaobject *)self; |
| 419 | // TODO: Hash in the hash for the origin |
| 420 | Py_hash_t h0 = PyObject_Hash(alias->origin); |
| 421 | if (h0 == -1) { |
| 422 | return -1; |
| 423 | } |
| 424 | Py_hash_t h1 = PyObject_Hash(alias->args); |
| 425 | if (h1 == -1) { |
| 426 | return -1; |
| 427 | } |
| 428 | return h0 ^ h1; |
| 429 | } |
| 430 | |
| 431 | static PyObject * |
| 432 | ga_call(PyObject *self, PyObject *args, PyObject *kwds) |
| 433 | { |
| 434 | gaobject *alias = (gaobject *)self; |
| 435 | PyObject *obj = PyObject_Call(alias->origin, args, kwds); |
| 436 | if (obj != NULL) { |
| 437 | if (PyObject_SetAttrString(obj, "__orig_class__", self) < 0) { |
| 438 | if (!PyErr_ExceptionMatches(PyExc_AttributeError) && |
| 439 | !PyErr_ExceptionMatches(PyExc_TypeError)) |
| 440 | { |
| 441 | Py_DECREF(obj); |
| 442 | return NULL; |
| 443 | } |
| 444 | PyErr_Clear(); |
| 445 | } |
| 446 | } |
| 447 | return obj; |
| 448 | } |
| 449 | |
| 450 | static const char* const attr_exceptions[] = { |
| 451 | "__origin__", |
| 452 | "__args__", |
| 453 | "__parameters__", |
| 454 | "__mro_entries__", |
| 455 | "__reduce_ex__", // needed so we don't look up object.__reduce_ex__ |
| 456 | "__reduce__", |
| 457 | NULL, |
| 458 | }; |
| 459 | |
| 460 | static PyObject * |
| 461 | ga_getattro(PyObject *self, PyObject *name) |
| 462 | { |
| 463 | gaobject *alias = (gaobject *)self; |
| 464 | if (PyUnicode_Check(name)) { |
| 465 | for (const char * const *p = attr_exceptions; ; p++) { |
| 466 | if (*p == NULL) { |
| 467 | return PyObject_GetAttr(alias->origin, name); |
| 468 | } |
| 469 | if (_PyUnicode_EqualToASCIIString(name, *p)) { |
| 470 | break; |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | return PyObject_GenericGetAttr(self, name); |
| 475 | } |
| 476 | |
| 477 | static PyObject * |
| 478 | ga_richcompare(PyObject *a, PyObject *b, int op) |
| 479 | { |
kj | 463c7d3 | 2020-12-14 02:38:24 +0800 | [diff] [blame] | 480 | if (!PyObject_TypeCheck(a, &Py_GenericAliasType) || |
| 481 | !PyObject_TypeCheck(b, &Py_GenericAliasType) || |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 482 | (op != Py_EQ && op != Py_NE)) |
| 483 | { |
| 484 | Py_RETURN_NOTIMPLEMENTED; |
| 485 | } |
| 486 | |
| 487 | if (op == Py_NE) { |
| 488 | PyObject *eq = ga_richcompare(a, b, Py_EQ); |
| 489 | if (eq == NULL) |
| 490 | return NULL; |
| 491 | Py_DECREF(eq); |
| 492 | if (eq == Py_True) { |
| 493 | Py_RETURN_FALSE; |
| 494 | } |
| 495 | else { |
| 496 | Py_RETURN_TRUE; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | gaobject *aa = (gaobject *)a; |
| 501 | gaobject *bb = (gaobject *)b; |
| 502 | int eq = PyObject_RichCompareBool(aa->origin, bb->origin, Py_EQ); |
| 503 | if (eq < 0) { |
| 504 | return NULL; |
| 505 | } |
| 506 | if (!eq) { |
| 507 | Py_RETURN_FALSE; |
| 508 | } |
| 509 | return PyObject_RichCompare(aa->args, bb->args, Py_EQ); |
| 510 | } |
| 511 | |
| 512 | static PyObject * |
| 513 | ga_mro_entries(PyObject *self, PyObject *args) |
| 514 | { |
| 515 | gaobject *alias = (gaobject *)self; |
| 516 | return PyTuple_Pack(1, alias->origin); |
| 517 | } |
| 518 | |
| 519 | static PyObject * |
| 520 | ga_instancecheck(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 521 | { |
| 522 | PyErr_SetString(PyExc_TypeError, |
| 523 | "isinstance() argument 2 cannot be a parameterized generic"); |
| 524 | return NULL; |
| 525 | } |
| 526 | |
| 527 | static PyObject * |
| 528 | ga_subclasscheck(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 529 | { |
| 530 | PyErr_SetString(PyExc_TypeError, |
| 531 | "issubclass() argument 2 cannot be a parameterized generic"); |
| 532 | return NULL; |
| 533 | } |
| 534 | |
| 535 | static PyObject * |
| 536 | ga_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 537 | { |
| 538 | gaobject *alias = (gaobject *)self; |
| 539 | return Py_BuildValue("O(OO)", Py_TYPE(alias), |
| 540 | alias->origin, alias->args); |
| 541 | } |
| 542 | |
Batuhan Taskaya | 2e87774 | 2020-09-16 00:58:32 +0300 | [diff] [blame] | 543 | static PyObject * |
| 544 | ga_dir(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 545 | { |
| 546 | gaobject *alias = (gaobject *)self; |
| 547 | PyObject *dir = PyObject_Dir(alias->origin); |
| 548 | if (dir == NULL) { |
| 549 | return NULL; |
| 550 | } |
| 551 | |
| 552 | PyObject *dir_entry = NULL; |
| 553 | for (const char * const *p = attr_exceptions; ; p++) { |
| 554 | if (*p == NULL) { |
| 555 | break; |
| 556 | } |
| 557 | else { |
| 558 | dir_entry = PyUnicode_FromString(*p); |
| 559 | if (dir_entry == NULL) { |
| 560 | goto error; |
| 561 | } |
| 562 | int contains = PySequence_Contains(dir, dir_entry); |
| 563 | if (contains < 0) { |
| 564 | goto error; |
| 565 | } |
| 566 | if (contains == 0 && PyList_Append(dir, dir_entry) < 0) { |
| 567 | goto error; |
| 568 | } |
| 569 | |
| 570 | Py_CLEAR(dir_entry); |
| 571 | } |
| 572 | } |
| 573 | return dir; |
| 574 | |
| 575 | error: |
| 576 | Py_DECREF(dir); |
| 577 | Py_XDECREF(dir_entry); |
| 578 | return NULL; |
| 579 | } |
| 580 | |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 581 | static PyMethodDef ga_methods[] = { |
| 582 | {"__mro_entries__", ga_mro_entries, METH_O}, |
| 583 | {"__instancecheck__", ga_instancecheck, METH_O}, |
| 584 | {"__subclasscheck__", ga_subclasscheck, METH_O}, |
| 585 | {"__reduce__", ga_reduce, METH_NOARGS}, |
Batuhan Taskaya | 2e87774 | 2020-09-16 00:58:32 +0300 | [diff] [blame] | 586 | {"__dir__", ga_dir, METH_NOARGS}, |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 587 | {0} |
| 588 | }; |
| 589 | |
| 590 | static PyMemberDef ga_members[] = { |
| 591 | {"__origin__", T_OBJECT, offsetof(gaobject, origin), READONLY}, |
| 592 | {"__args__", T_OBJECT, offsetof(gaobject, args), READONLY}, |
| 593 | {0} |
| 594 | }; |
| 595 | |
| 596 | static PyObject * |
| 597 | ga_parameters(PyObject *self, void *unused) |
| 598 | { |
| 599 | gaobject *alias = (gaobject *)self; |
| 600 | if (alias->parameters == NULL) { |
| 601 | alias->parameters = make_parameters(alias->args); |
| 602 | if (alias->parameters == NULL) { |
| 603 | return NULL; |
| 604 | } |
| 605 | } |
| 606 | Py_INCREF(alias->parameters); |
| 607 | return alias->parameters; |
| 608 | } |
| 609 | |
| 610 | static PyGetSetDef ga_properties[] = { |
| 611 | {"__parameters__", ga_parameters, (setter)NULL, "Type variables in the GenericAlias.", NULL}, |
| 612 | {0} |
| 613 | }; |
| 614 | |
kj | 463c7d3 | 2020-12-14 02:38:24 +0800 | [diff] [blame] | 615 | /* A helper function to create GenericAlias' args tuple and set its attributes. |
| 616 | * Returns 1 on success, 0 on failure. |
| 617 | */ |
| 618 | static inline int |
| 619 | setup_ga(gaobject *alias, PyObject *origin, PyObject *args) { |
| 620 | if (!PyTuple_Check(args)) { |
| 621 | args = PyTuple_Pack(1, args); |
| 622 | if (args == NULL) { |
| 623 | return 0; |
| 624 | } |
| 625 | } |
| 626 | else { |
| 627 | Py_INCREF(args); |
| 628 | } |
| 629 | |
| 630 | Py_INCREF(origin); |
| 631 | alias->origin = origin; |
| 632 | alias->args = args; |
| 633 | alias->parameters = NULL; |
| 634 | alias->weakreflist = NULL; |
| 635 | return 1; |
| 636 | } |
| 637 | |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 638 | static PyObject * |
| 639 | ga_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 640 | { |
kj | 804d689 | 2020-12-05 23:02:14 +0700 | [diff] [blame] | 641 | if (!_PyArg_NoKeywords("GenericAlias", kwds)) { |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 642 | return NULL; |
| 643 | } |
Dong-hee Na | 02e4484 | 2020-04-24 01:25:53 +0900 | [diff] [blame] | 644 | if (!_PyArg_CheckPositional("GenericAlias", PyTuple_GET_SIZE(args), 2, 2)) { |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 645 | return NULL; |
| 646 | } |
| 647 | PyObject *origin = PyTuple_GET_ITEM(args, 0); |
| 648 | PyObject *arguments = PyTuple_GET_ITEM(args, 1); |
kj | 463c7d3 | 2020-12-14 02:38:24 +0800 | [diff] [blame] | 649 | gaobject *self = (gaobject *)type->tp_alloc(type, 0); |
| 650 | if (self == NULL) { |
| 651 | return NULL; |
| 652 | } |
| 653 | if (!setup_ga(self, origin, arguments)) { |
| 654 | type->tp_free((PyObject *)self); |
| 655 | return NULL; |
| 656 | } |
| 657 | return (PyObject *)self; |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 658 | } |
| 659 | |
kj | 4eb41d0 | 2020-11-09 12:00:13 +0800 | [diff] [blame] | 660 | static PyNumberMethods ga_as_number = { |
| 661 | .nb_or = (binaryfunc)_Py_union_type_or, // Add __or__ function |
| 662 | }; |
| 663 | |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 664 | // TODO: |
| 665 | // - argument clinic? |
| 666 | // - __doc__? |
| 667 | // - cache? |
| 668 | PyTypeObject Py_GenericAliasType = { |
| 669 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 670 | .tp_name = "types.GenericAlias", |
| 671 | .tp_doc = "Represent a PEP 585 generic type\n" |
| 672 | "\n" |
Mikhail Golubev | 77f0a23 | 2020-10-09 00:38:36 +0300 | [diff] [blame] | 673 | "E.g. for t = list[int], t.__origin__ is list and t.__args__ is (int,).", |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 674 | .tp_basicsize = sizeof(gaobject), |
| 675 | .tp_dealloc = ga_dealloc, |
| 676 | .tp_repr = ga_repr, |
kj | 4eb41d0 | 2020-11-09 12:00:13 +0800 | [diff] [blame] | 677 | .tp_as_number = &ga_as_number, // allow X | Y of GenericAlias objs |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 678 | .tp_as_mapping = &ga_as_mapping, |
| 679 | .tp_hash = ga_hash, |
| 680 | .tp_call = ga_call, |
| 681 | .tp_getattro = ga_getattro, |
kj | 463c7d3 | 2020-12-14 02:38:24 +0800 | [diff] [blame] | 682 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 683 | .tp_traverse = ga_traverse, |
| 684 | .tp_richcompare = ga_richcompare, |
kj | 384b7a4 | 2020-11-16 11:27:23 +0800 | [diff] [blame] | 685 | .tp_weaklistoffset = offsetof(gaobject, weakreflist), |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 686 | .tp_methods = ga_methods, |
| 687 | .tp_members = ga_members, |
| 688 | .tp_alloc = PyType_GenericAlloc, |
| 689 | .tp_new = ga_new, |
| 690 | .tp_free = PyObject_GC_Del, |
| 691 | .tp_getset = ga_properties, |
| 692 | }; |
| 693 | |
| 694 | PyObject * |
| 695 | Py_GenericAlias(PyObject *origin, PyObject *args) |
| 696 | { |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 697 | gaobject *alias = PyObject_GC_New(gaobject, &Py_GenericAliasType); |
| 698 | if (alias == NULL) { |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 699 | return NULL; |
| 700 | } |
kj | 463c7d3 | 2020-12-14 02:38:24 +0800 | [diff] [blame] | 701 | if (!setup_ga(alias, origin, args)) { |
| 702 | PyObject_GC_Del((PyObject *)alias); |
| 703 | return NULL; |
| 704 | } |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 705 | _PyObject_GC_TRACK(alias); |
| 706 | return (PyObject *)alias; |
| 707 | } |