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" |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 5 | #include "structmember.h" // PyMemberDef |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 6 | |
| 7 | typedef struct { |
| 8 | PyObject_HEAD |
| 9 | PyObject *origin; |
| 10 | PyObject *args; |
| 11 | PyObject *parameters; |
| 12 | } gaobject; |
| 13 | |
| 14 | static void |
| 15 | ga_dealloc(PyObject *self) |
| 16 | { |
| 17 | gaobject *alias = (gaobject *)self; |
| 18 | |
| 19 | _PyObject_GC_UNTRACK(self); |
| 20 | Py_XDECREF(alias->origin); |
| 21 | Py_XDECREF(alias->args); |
| 22 | Py_XDECREF(alias->parameters); |
| 23 | self->ob_type->tp_free(self); |
| 24 | } |
| 25 | |
| 26 | static int |
| 27 | ga_traverse(PyObject *self, visitproc visit, void *arg) |
| 28 | { |
| 29 | gaobject *alias = (gaobject *)self; |
| 30 | Py_VISIT(alias->origin); |
| 31 | Py_VISIT(alias->args); |
| 32 | Py_VISIT(alias->parameters); |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | static int |
| 37 | ga_repr_item(_PyUnicodeWriter *writer, PyObject *p) |
| 38 | { |
| 39 | _Py_IDENTIFIER(__module__); |
| 40 | _Py_IDENTIFIER(__qualname__); |
| 41 | _Py_IDENTIFIER(__origin__); |
| 42 | _Py_IDENTIFIER(__args__); |
| 43 | PyObject *qualname = NULL; |
| 44 | PyObject *module = NULL; |
| 45 | PyObject *r = NULL; |
| 46 | PyObject *tmp; |
| 47 | int err; |
| 48 | |
| 49 | if (p == Py_Ellipsis) { |
| 50 | // The Ellipsis object |
| 51 | r = PyUnicode_FromString("..."); |
| 52 | goto done; |
| 53 | } |
| 54 | |
| 55 | if (_PyObject_LookupAttrId(p, &PyId___origin__, &tmp) < 0) { |
| 56 | goto done; |
| 57 | } |
| 58 | if (tmp != NULL) { |
| 59 | Py_DECREF(tmp); |
| 60 | if (_PyObject_LookupAttrId(p, &PyId___args__, &tmp) < 0) { |
| 61 | goto done; |
| 62 | } |
| 63 | if (tmp != NULL) { |
| 64 | Py_DECREF(tmp); |
| 65 | // It looks like a GenericAlias |
| 66 | goto use_repr; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if (_PyObject_LookupAttrId(p, &PyId___qualname__, &qualname) < 0) { |
| 71 | goto done; |
| 72 | } |
| 73 | if (qualname == NULL) { |
| 74 | goto use_repr; |
| 75 | } |
| 76 | if (_PyObject_LookupAttrId(p, &PyId___module__, &module) < 0) { |
| 77 | goto done; |
| 78 | } |
| 79 | if (module == NULL || module == Py_None) { |
| 80 | goto use_repr; |
| 81 | } |
| 82 | |
| 83 | // Looks like a class |
| 84 | if (PyUnicode_Check(module) && |
| 85 | _PyUnicode_EqualToASCIIString(module, "builtins")) |
| 86 | { |
| 87 | // builtins don't need a module name |
| 88 | r = PyObject_Str(qualname); |
| 89 | goto done; |
| 90 | } |
| 91 | else { |
| 92 | r = PyUnicode_FromFormat("%S.%S", module, qualname); |
| 93 | goto done; |
| 94 | } |
| 95 | |
| 96 | use_repr: |
| 97 | r = PyObject_Repr(p); |
| 98 | |
| 99 | done: |
| 100 | Py_XDECREF(qualname); |
| 101 | Py_XDECREF(module); |
| 102 | if (r == NULL) { |
| 103 | // error if any of the above PyObject_Repr/PyUnicode_From* fail |
| 104 | err = -1; |
| 105 | } |
| 106 | else { |
| 107 | err = _PyUnicodeWriter_WriteStr(writer, r); |
| 108 | Py_DECREF(r); |
| 109 | } |
| 110 | return err; |
| 111 | } |
| 112 | |
| 113 | static PyObject * |
| 114 | ga_repr(PyObject *self) |
| 115 | { |
| 116 | gaobject *alias = (gaobject *)self; |
| 117 | Py_ssize_t len = PyTuple_GET_SIZE(alias->args); |
| 118 | |
| 119 | _PyUnicodeWriter writer; |
| 120 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 121 | |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 122 | if (ga_repr_item(&writer, alias->origin) < 0) { |
| 123 | goto error; |
| 124 | } |
| 125 | if (_PyUnicodeWriter_WriteASCIIString(&writer, "[", 1) < 0) { |
| 126 | goto error; |
| 127 | } |
| 128 | for (Py_ssize_t i = 0; i < len; i++) { |
| 129 | if (i > 0) { |
| 130 | if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) { |
| 131 | goto error; |
| 132 | } |
| 133 | } |
| 134 | PyObject *p = PyTuple_GET_ITEM(alias->args, i); |
| 135 | if (ga_repr_item(&writer, p) < 0) { |
| 136 | goto error; |
| 137 | } |
| 138 | } |
| 139 | if (len == 0) { |
| 140 | // for something like tuple[()] we should print a "()" |
| 141 | if (_PyUnicodeWriter_WriteASCIIString(&writer, "()", 2) < 0) { |
| 142 | goto error; |
| 143 | } |
| 144 | } |
| 145 | if (_PyUnicodeWriter_WriteASCIIString(&writer, "]", 1) < 0) { |
| 146 | goto error; |
| 147 | } |
| 148 | return _PyUnicodeWriter_Finish(&writer); |
| 149 | error: |
| 150 | _PyUnicodeWriter_Dealloc(&writer); |
| 151 | return NULL; |
| 152 | } |
| 153 | |
| 154 | // isinstance(obj, TypeVar) without importing typing.py. |
| 155 | // Returns -1 for errors. |
| 156 | static int |
| 157 | is_typevar(PyObject *obj) |
| 158 | { |
| 159 | PyTypeObject *type = Py_TYPE(obj); |
| 160 | if (strcmp(type->tp_name, "TypeVar") != 0) { |
| 161 | return 0; |
| 162 | } |
| 163 | PyObject *module = PyObject_GetAttrString((PyObject *)type, "__module__"); |
| 164 | if (module == NULL) { |
| 165 | return -1; |
| 166 | } |
| 167 | int res = PyUnicode_Check(module) |
| 168 | && _PyUnicode_EqualToASCIIString(module, "typing"); |
| 169 | Py_DECREF(module); |
| 170 | return res; |
| 171 | } |
| 172 | |
| 173 | // Index of item in self[:len], or -1 if not found (self is a tuple) |
| 174 | static Py_ssize_t |
| 175 | tuple_index(PyObject *self, Py_ssize_t len, PyObject *item) |
| 176 | { |
| 177 | for (Py_ssize_t i = 0; i < len; i++) { |
| 178 | if (PyTuple_GET_ITEM(self, i) == item) { |
| 179 | return i; |
| 180 | } |
| 181 | } |
| 182 | return -1; |
| 183 | } |
| 184 | |
| 185 | // tuple(t for t in args if isinstance(t, TypeVar)) |
| 186 | static PyObject * |
| 187 | make_parameters(PyObject *args) |
| 188 | { |
| 189 | Py_ssize_t len = PyTuple_GET_SIZE(args); |
| 190 | PyObject *parameters = PyTuple_New(len); |
| 191 | if (parameters == NULL) |
| 192 | return NULL; |
| 193 | Py_ssize_t iparam = 0; |
| 194 | for (Py_ssize_t iarg = 0; iarg < len; iarg++) { |
| 195 | PyObject *t = PyTuple_GET_ITEM(args, iarg); |
| 196 | int typevar = is_typevar(t); |
| 197 | if (typevar < 0) { |
| 198 | Py_XDECREF(parameters); |
| 199 | return NULL; |
| 200 | } |
| 201 | if (typevar) { |
| 202 | if (tuple_index(parameters, iparam, t) < 0) { |
| 203 | Py_INCREF(t); |
| 204 | PyTuple_SET_ITEM(parameters, iparam, t); |
| 205 | iparam++; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | if (iparam < len) { |
| 210 | if (_PyTuple_Resize(¶meters, iparam) < 0) { |
| 211 | Py_XDECREF(parameters); |
| 212 | return NULL; |
| 213 | } |
| 214 | } |
| 215 | return parameters; |
| 216 | } |
| 217 | |
| 218 | static PyObject * |
| 219 | ga_getitem(PyObject *self, PyObject *item) |
| 220 | { |
| 221 | gaobject *alias = (gaobject *)self; |
| 222 | // do a lookup for __parameters__ so it gets populated (if not already) |
| 223 | if (alias->parameters == NULL) { |
| 224 | alias->parameters = make_parameters(alias->args); |
| 225 | if (alias->parameters == NULL) { |
| 226 | return NULL; |
| 227 | } |
| 228 | } |
| 229 | Py_ssize_t nparams = PyTuple_GET_SIZE(alias->parameters); |
| 230 | if (nparams == 0) { |
| 231 | return PyErr_Format(PyExc_TypeError, |
| 232 | "There are no type variables left in %R", |
| 233 | self); |
| 234 | } |
| 235 | int is_tuple = PyTuple_Check(item); |
| 236 | Py_ssize_t nitem = is_tuple ? PyTuple_GET_SIZE(item) : 1; |
| 237 | if (nitem != nparams) { |
| 238 | return PyErr_Format(PyExc_TypeError, |
| 239 | "Too %s arguments for %R", |
| 240 | nitem > nparams ? "many" : "few", |
| 241 | self); |
| 242 | } |
| 243 | Py_ssize_t nargs = PyTuple_GET_SIZE(alias->args); |
| 244 | PyObject *newargs = PyTuple_New(nargs); |
| 245 | if (newargs == NULL) |
| 246 | return NULL; |
| 247 | for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) { |
| 248 | PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg); |
| 249 | int typevar = is_typevar(arg); |
| 250 | if (typevar < 0) { |
| 251 | Py_DECREF(newargs); |
| 252 | return NULL; |
| 253 | } |
| 254 | if (typevar) { |
| 255 | Py_ssize_t iparam = tuple_index(alias->parameters, nparams, arg); |
| 256 | assert(iparam >= 0); |
| 257 | if (is_tuple) { |
| 258 | arg = PyTuple_GET_ITEM(item, iparam); |
| 259 | } |
| 260 | else { |
| 261 | assert(iparam == 0); |
| 262 | arg = item; |
| 263 | } |
| 264 | } |
| 265 | Py_INCREF(arg); |
| 266 | PyTuple_SET_ITEM(newargs, iarg, arg); |
| 267 | } |
| 268 | PyObject *res = Py_GenericAlias(alias->origin, newargs); |
| 269 | Py_DECREF(newargs); |
| 270 | return res; |
| 271 | } |
| 272 | |
| 273 | static PyMappingMethods ga_as_mapping = { |
| 274 | .mp_subscript = ga_getitem, |
| 275 | }; |
| 276 | |
| 277 | static Py_hash_t |
| 278 | ga_hash(PyObject *self) |
| 279 | { |
| 280 | gaobject *alias = (gaobject *)self; |
| 281 | // TODO: Hash in the hash for the origin |
| 282 | Py_hash_t h0 = PyObject_Hash(alias->origin); |
| 283 | if (h0 == -1) { |
| 284 | return -1; |
| 285 | } |
| 286 | Py_hash_t h1 = PyObject_Hash(alias->args); |
| 287 | if (h1 == -1) { |
| 288 | return -1; |
| 289 | } |
| 290 | return h0 ^ h1; |
| 291 | } |
| 292 | |
| 293 | static PyObject * |
| 294 | ga_call(PyObject *self, PyObject *args, PyObject *kwds) |
| 295 | { |
| 296 | gaobject *alias = (gaobject *)self; |
| 297 | PyObject *obj = PyObject_Call(alias->origin, args, kwds); |
| 298 | if (obj != NULL) { |
| 299 | if (PyObject_SetAttrString(obj, "__orig_class__", self) < 0) { |
| 300 | if (!PyErr_ExceptionMatches(PyExc_AttributeError) && |
| 301 | !PyErr_ExceptionMatches(PyExc_TypeError)) |
| 302 | { |
| 303 | Py_DECREF(obj); |
| 304 | return NULL; |
| 305 | } |
| 306 | PyErr_Clear(); |
| 307 | } |
| 308 | } |
| 309 | return obj; |
| 310 | } |
| 311 | |
| 312 | static const char* const attr_exceptions[] = { |
| 313 | "__origin__", |
| 314 | "__args__", |
| 315 | "__parameters__", |
| 316 | "__mro_entries__", |
| 317 | "__reduce_ex__", // needed so we don't look up object.__reduce_ex__ |
| 318 | "__reduce__", |
| 319 | NULL, |
| 320 | }; |
| 321 | |
| 322 | static PyObject * |
| 323 | ga_getattro(PyObject *self, PyObject *name) |
| 324 | { |
| 325 | gaobject *alias = (gaobject *)self; |
| 326 | if (PyUnicode_Check(name)) { |
| 327 | for (const char * const *p = attr_exceptions; ; p++) { |
| 328 | if (*p == NULL) { |
| 329 | return PyObject_GetAttr(alias->origin, name); |
| 330 | } |
| 331 | if (_PyUnicode_EqualToASCIIString(name, *p)) { |
| 332 | break; |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | return PyObject_GenericGetAttr(self, name); |
| 337 | } |
| 338 | |
| 339 | static PyObject * |
| 340 | ga_richcompare(PyObject *a, PyObject *b, int op) |
| 341 | { |
| 342 | if (Py_TYPE(a) != &Py_GenericAliasType || |
| 343 | Py_TYPE(b) != &Py_GenericAliasType || |
| 344 | (op != Py_EQ && op != Py_NE)) |
| 345 | { |
| 346 | Py_RETURN_NOTIMPLEMENTED; |
| 347 | } |
| 348 | |
| 349 | if (op == Py_NE) { |
| 350 | PyObject *eq = ga_richcompare(a, b, Py_EQ); |
| 351 | if (eq == NULL) |
| 352 | return NULL; |
| 353 | Py_DECREF(eq); |
| 354 | if (eq == Py_True) { |
| 355 | Py_RETURN_FALSE; |
| 356 | } |
| 357 | else { |
| 358 | Py_RETURN_TRUE; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | gaobject *aa = (gaobject *)a; |
| 363 | gaobject *bb = (gaobject *)b; |
| 364 | int eq = PyObject_RichCompareBool(aa->origin, bb->origin, Py_EQ); |
| 365 | if (eq < 0) { |
| 366 | return NULL; |
| 367 | } |
| 368 | if (!eq) { |
| 369 | Py_RETURN_FALSE; |
| 370 | } |
| 371 | return PyObject_RichCompare(aa->args, bb->args, Py_EQ); |
| 372 | } |
| 373 | |
| 374 | static PyObject * |
| 375 | ga_mro_entries(PyObject *self, PyObject *args) |
| 376 | { |
| 377 | gaobject *alias = (gaobject *)self; |
| 378 | return PyTuple_Pack(1, alias->origin); |
| 379 | } |
| 380 | |
| 381 | static PyObject * |
| 382 | ga_instancecheck(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 383 | { |
| 384 | PyErr_SetString(PyExc_TypeError, |
| 385 | "isinstance() argument 2 cannot be a parameterized generic"); |
| 386 | return NULL; |
| 387 | } |
| 388 | |
| 389 | static PyObject * |
| 390 | ga_subclasscheck(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 391 | { |
| 392 | PyErr_SetString(PyExc_TypeError, |
| 393 | "issubclass() argument 2 cannot be a parameterized generic"); |
| 394 | return NULL; |
| 395 | } |
| 396 | |
| 397 | static PyObject * |
| 398 | ga_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 399 | { |
| 400 | gaobject *alias = (gaobject *)self; |
| 401 | return Py_BuildValue("O(OO)", Py_TYPE(alias), |
| 402 | alias->origin, alias->args); |
| 403 | } |
| 404 | |
| 405 | static PyMethodDef ga_methods[] = { |
| 406 | {"__mro_entries__", ga_mro_entries, METH_O}, |
| 407 | {"__instancecheck__", ga_instancecheck, METH_O}, |
| 408 | {"__subclasscheck__", ga_subclasscheck, METH_O}, |
| 409 | {"__reduce__", ga_reduce, METH_NOARGS}, |
| 410 | {0} |
| 411 | }; |
| 412 | |
| 413 | static PyMemberDef ga_members[] = { |
| 414 | {"__origin__", T_OBJECT, offsetof(gaobject, origin), READONLY}, |
| 415 | {"__args__", T_OBJECT, offsetof(gaobject, args), READONLY}, |
| 416 | {0} |
| 417 | }; |
| 418 | |
| 419 | static PyObject * |
| 420 | ga_parameters(PyObject *self, void *unused) |
| 421 | { |
| 422 | gaobject *alias = (gaobject *)self; |
| 423 | if (alias->parameters == NULL) { |
| 424 | alias->parameters = make_parameters(alias->args); |
| 425 | if (alias->parameters == NULL) { |
| 426 | return NULL; |
| 427 | } |
| 428 | } |
| 429 | Py_INCREF(alias->parameters); |
| 430 | return alias->parameters; |
| 431 | } |
| 432 | |
| 433 | static PyGetSetDef ga_properties[] = { |
| 434 | {"__parameters__", ga_parameters, (setter)NULL, "Type variables in the GenericAlias.", NULL}, |
| 435 | {0} |
| 436 | }; |
| 437 | |
| 438 | static PyObject * |
| 439 | ga_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 440 | { |
| 441 | if (kwds != NULL && PyDict_GET_SIZE(kwds) != 0) { |
| 442 | PyErr_SetString(PyExc_TypeError, "GenericAlias does not support keyword arguments"); |
| 443 | return NULL; |
| 444 | } |
| 445 | if (PyTuple_GET_SIZE(args) != 2) { |
| 446 | PyErr_SetString(PyExc_TypeError, "GenericAlias expects 2 positional arguments"); |
| 447 | return NULL; |
| 448 | } |
| 449 | PyObject *origin = PyTuple_GET_ITEM(args, 0); |
| 450 | PyObject *arguments = PyTuple_GET_ITEM(args, 1); |
| 451 | return Py_GenericAlias(origin, arguments); |
| 452 | } |
| 453 | |
| 454 | // TODO: |
| 455 | // - argument clinic? |
| 456 | // - __doc__? |
| 457 | // - cache? |
| 458 | PyTypeObject Py_GenericAliasType = { |
| 459 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 460 | .tp_name = "types.GenericAlias", |
| 461 | .tp_doc = "Represent a PEP 585 generic type\n" |
| 462 | "\n" |
| 463 | "E.g. for t = list[int], t.origin is list and t.args is (int,).", |
| 464 | .tp_basicsize = sizeof(gaobject), |
| 465 | .tp_dealloc = ga_dealloc, |
| 466 | .tp_repr = ga_repr, |
| 467 | .tp_as_mapping = &ga_as_mapping, |
| 468 | .tp_hash = ga_hash, |
| 469 | .tp_call = ga_call, |
| 470 | .tp_getattro = ga_getattro, |
| 471 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 472 | .tp_traverse = ga_traverse, |
| 473 | .tp_richcompare = ga_richcompare, |
| 474 | .tp_methods = ga_methods, |
| 475 | .tp_members = ga_members, |
| 476 | .tp_alloc = PyType_GenericAlloc, |
| 477 | .tp_new = ga_new, |
| 478 | .tp_free = PyObject_GC_Del, |
| 479 | .tp_getset = ga_properties, |
| 480 | }; |
| 481 | |
| 482 | PyObject * |
| 483 | Py_GenericAlias(PyObject *origin, PyObject *args) |
| 484 | { |
| 485 | if (!PyTuple_Check(args)) { |
| 486 | args = PyTuple_Pack(1, args); |
| 487 | if (args == NULL) { |
| 488 | return NULL; |
| 489 | } |
| 490 | } |
| 491 | else { |
| 492 | Py_INCREF(args); |
| 493 | } |
| 494 | |
| 495 | gaobject *alias = PyObject_GC_New(gaobject, &Py_GenericAliasType); |
| 496 | if (alias == NULL) { |
| 497 | Py_DECREF(args); |
| 498 | return NULL; |
| 499 | } |
| 500 | |
| 501 | Py_INCREF(origin); |
| 502 | alias->origin = origin; |
| 503 | alias->args = args; |
| 504 | alias->parameters = NULL; |
| 505 | _PyObject_GC_TRACK(alias); |
| 506 | return (PyObject *)alias; |
| 507 | } |