LEFT | RIGHT |
1 /* Type object implementation */ | 1 /* Type object implementation */ |
2 | 2 |
3 #include "Python.h" | 3 #include "Python.h" |
4 #include "frameobject.h" | 4 #include "frameobject.h" |
5 #include "structmember.h" | 5 #include "structmember.h" |
6 | 6 |
7 #include <ctype.h> | 7 #include <ctype.h> |
8 | 8 |
9 | 9 |
10 /* Support type attribute cache */ | 10 /* Support type attribute cache */ |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 | 46 |
47 /* alphabetical order */ | 47 /* alphabetical order */ |
48 _Py_IDENTIFIER(__abstractmethods__); | 48 _Py_IDENTIFIER(__abstractmethods__); |
49 _Py_IDENTIFIER(__class__); | 49 _Py_IDENTIFIER(__class__); |
50 _Py_IDENTIFIER(__delitem__); | 50 _Py_IDENTIFIER(__delitem__); |
51 _Py_IDENTIFIER(__dict__); | 51 _Py_IDENTIFIER(__dict__); |
52 _Py_IDENTIFIER(__doc__); | 52 _Py_IDENTIFIER(__doc__); |
53 _Py_IDENTIFIER(__getattribute__); | 53 _Py_IDENTIFIER(__getattribute__); |
54 _Py_IDENTIFIER(__getitem__); | 54 _Py_IDENTIFIER(__getitem__); |
55 _Py_IDENTIFIER(__hash__); | 55 _Py_IDENTIFIER(__hash__); |
| 56 _Py_IDENTIFIER(__init_subclass__); |
56 _Py_IDENTIFIER(__len__); | 57 _Py_IDENTIFIER(__len__); |
57 _Py_IDENTIFIER(__module__); | 58 _Py_IDENTIFIER(__module__); |
58 _Py_IDENTIFIER(__name__); | 59 _Py_IDENTIFIER(__name__); |
59 _Py_IDENTIFIER(__new__); | 60 _Py_IDENTIFIER(__new__); |
| 61 _Py_IDENTIFIER(__set_name__); |
60 _Py_IDENTIFIER(__setitem__); | 62 _Py_IDENTIFIER(__setitem__); |
61 _Py_IDENTIFIER(builtins); | 63 _Py_IDENTIFIER(builtins); |
62 | 64 |
63 static PyObject * | 65 static PyObject * |
64 slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds); | 66 slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
65 | 67 |
66 static void | 68 static void |
67 clear_slotdefs(void); | 69 clear_slotdefs(void); |
68 | 70 |
69 /* | 71 /* |
(...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 | 447 |
446 et = (PyHeapTypeObject*)type; | 448 et = (PyHeapTypeObject*)type; |
447 Py_INCREF(value); | 449 Py_INCREF(value); |
448 Py_SETREF(et->ht_qualname, value); | 450 Py_SETREF(et->ht_qualname, value); |
449 return 0; | 451 return 0; |
450 } | 452 } |
451 | 453 |
452 static PyObject * | 454 static PyObject * |
453 type_module(PyTypeObject *type, void *context) | 455 type_module(PyTypeObject *type, void *context) |
454 { | 456 { |
455 char *s; | 457 PyObject *mod; |
456 | 458 |
457 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { | 459 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
458 PyObject *mod = _PyDict_GetItemId(type->tp_dict, &PyId___module__); | 460 mod = _PyDict_GetItemId(type->tp_dict, &PyId___module__); |
459 if (!mod) { | 461 if (mod == NULL) { |
460 PyErr_Format(PyExc_AttributeError, "__module__"); | 462 PyErr_Format(PyExc_AttributeError, "__module__"); |
461 return 0; | 463 return NULL; |
462 } | 464 } |
463 Py_INCREF(mod); | 465 Py_INCREF(mod); |
464 return mod; | |
465 } | 466 } |
466 else { | 467 else { |
467 PyObject *name; | 468 const char *s = strrchr(type->tp_name, '.'); |
468 s = strrchr(type->tp_name, '.'); | 469 if (s != NULL) { |
469 if (s != NULL) | 470 mod = PyUnicode_FromStringAndSize( |
470 return PyUnicode_FromStringAndSize( | |
471 type->tp_name, (Py_ssize_t)(s - type->tp_name)); | 471 type->tp_name, (Py_ssize_t)(s - type->tp_name)); |
472 name = _PyUnicode_FromId(&PyId_builtins); | 472 if (mod != NULL) |
473 Py_XINCREF(name); | 473 PyUnicode_InternInPlace(&mod); |
474 return name; | 474 } |
475 } | 475 else { |
| 476 mod = _PyUnicode_FromId(&PyId_builtins); |
| 477 Py_XINCREF(mod); |
| 478 } |
| 479 } |
| 480 return mod; |
476 } | 481 } |
477 | 482 |
478 static int | 483 static int |
479 type_set_module(PyTypeObject *type, PyObject *value, void *context) | 484 type_set_module(PyTypeObject *type, PyObject *value, void *context) |
480 { | 485 { |
481 if (!check_set_special_type_attr(type, value, "__module__")) | 486 if (!check_set_special_type_attr(type, value, "__module__")) |
482 return -1; | 487 return -1; |
483 | 488 |
484 PyType_Modified(type); | 489 PyType_Modified(type); |
485 | 490 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
540 | 545 |
541 static PyObject * | 546 static PyObject * |
542 type_get_bases(PyTypeObject *type, void *context) | 547 type_get_bases(PyTypeObject *type, void *context) |
543 { | 548 { |
544 Py_INCREF(type->tp_bases); | 549 Py_INCREF(type->tp_bases); |
545 return type->tp_bases; | 550 return type->tp_bases; |
546 } | 551 } |
547 | 552 |
548 static PyTypeObject *best_base(PyObject *); | 553 static PyTypeObject *best_base(PyObject *); |
549 static int mro_internal(PyTypeObject *, PyObject **); | 554 static int mro_internal(PyTypeObject *, PyObject **); |
550 Py_LOCAL_INLINE(int) type_is_subtype_base_chain(PyTypeObject *, PyTypeObject *); | 555 static int type_is_subtype_base_chain(PyTypeObject *, PyTypeObject *); |
551 static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, const char
*); | 556 static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, const char
*); |
552 static int add_subclass(PyTypeObject*, PyTypeObject*); | 557 static int add_subclass(PyTypeObject*, PyTypeObject*); |
553 static int add_all_subclasses(PyTypeObject *type, PyObject *bases); | 558 static int add_all_subclasses(PyTypeObject *type, PyObject *bases); |
554 static void remove_subclass(PyTypeObject *, PyTypeObject *); | 559 static void remove_subclass(PyTypeObject *, PyTypeObject *); |
555 static void remove_all_subclasses(PyTypeObject *type, PyObject *bases); | 560 static void remove_all_subclasses(PyTypeObject *type, PyObject *bases); |
556 static void update_all_slots(PyTypeObject *); | 561 static void update_all_slots(PyTypeObject *); |
557 | 562 |
558 typedef int (*update_callback)(PyTypeObject *, void *); | 563 typedef int (*update_callback)(PyTypeObject *, void *); |
559 static int update_subclasses(PyTypeObject *type, PyObject *name, | 564 static int update_subclasses(PyTypeObject *type, PyObject *name, |
560 update_callback callback, void *data); | 565 update_callback callback, void *data); |
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
851 else if (!PyUnicode_Check(mod)) { | 856 else if (!PyUnicode_Check(mod)) { |
852 Py_DECREF(mod); | 857 Py_DECREF(mod); |
853 mod = NULL; | 858 mod = NULL; |
854 } | 859 } |
855 name = type_qualname(type, NULL); | 860 name = type_qualname(type, NULL); |
856 if (name == NULL) { | 861 if (name == NULL) { |
857 Py_XDECREF(mod); | 862 Py_XDECREF(mod); |
858 return NULL; | 863 return NULL; |
859 } | 864 } |
860 | 865 |
861 if (mod != NULL && _PyUnicode_CompareWithId(mod, &PyId_builtins)) | 866 if (mod != NULL && !_PyUnicode_EqualToASCIIId(mod, &PyId_builtins)) |
862 rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name); | 867 rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name); |
863 else | 868 else |
864 rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name); | 869 rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name); |
865 | 870 |
866 Py_XDECREF(mod); | 871 Py_XDECREF(mod); |
867 Py_DECREF(name); | 872 Py_DECREF(name); |
868 return rtn; | 873 return rtn; |
869 } | 874 } |
870 | 875 |
871 static PyObject * | 876 static PyObject * |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1124 /* We get here only if the type has GC */ | 1129 /* We get here only if the type has GC */ |
1125 | 1130 |
1126 /* UnTrack and re-Track around the trashcan macro, alas */ | 1131 /* UnTrack and re-Track around the trashcan macro, alas */ |
1127 /* See explanation at end of function for full disclosure */ | 1132 /* See explanation at end of function for full disclosure */ |
1128 PyObject_GC_UnTrack(self); | 1133 PyObject_GC_UnTrack(self); |
1129 ++_PyTrash_delete_nesting; | 1134 ++_PyTrash_delete_nesting; |
1130 ++ tstate->trash_delete_nesting; | 1135 ++ tstate->trash_delete_nesting; |
1131 Py_TRASHCAN_SAFE_BEGIN(self); | 1136 Py_TRASHCAN_SAFE_BEGIN(self); |
1132 --_PyTrash_delete_nesting; | 1137 --_PyTrash_delete_nesting; |
1133 -- tstate->trash_delete_nesting; | 1138 -- tstate->trash_delete_nesting; |
1134 /* DO NOT restore GC tracking at this point. weakref callbacks | |
1135 * (if any, and whether directly here or indirectly in something we | |
1136 * call) may trigger GC, and if self is tracked at that point, it | |
1137 * will look like trash to GC and GC will try to delete self again. | |
1138 */ | |
1139 | 1139 |
1140 /* Find the nearest base with a different tp_dealloc */ | 1140 /* Find the nearest base with a different tp_dealloc */ |
1141 base = type; | 1141 base = type; |
1142 while ((/*basedealloc =*/ base->tp_dealloc) == subtype_dealloc) { | 1142 while ((/*basedealloc =*/ base->tp_dealloc) == subtype_dealloc) { |
1143 base = base->tp_base; | 1143 base = base->tp_base; |
1144 assert(base); | 1144 assert(base); |
1145 } | 1145 } |
1146 | 1146 |
1147 has_finalizer = type->tp_finalize || type->tp_del; | 1147 has_finalizer = type->tp_finalize || type->tp_del; |
1148 | 1148 |
1149 /* Maybe call finalizer; exit early if resurrected */ | 1149 if (type->tp_finalize) { |
1150 if (has_finalizer) | |
1151 _PyObject_GC_TRACK(self); | 1150 _PyObject_GC_TRACK(self); |
1152 | |
1153 if (type->tp_finalize) { | |
1154 if (PyObject_CallFinalizerFromDealloc(self) < 0) { | 1151 if (PyObject_CallFinalizerFromDealloc(self) < 0) { |
1155 /* Resurrected */ | 1152 /* Resurrected */ |
1156 goto endlabel; | 1153 goto endlabel; |
1157 } | 1154 } |
1158 } | 1155 _PyObject_GC_UNTRACK(self); |
1159 /* If we added a weaklist, we clear it. Do this *before* calling | 1156 } |
1160 tp_del, clearing slots, or clearing the instance dict. */ | 1157 /* |
| 1158 If we added a weaklist, we clear it. Do this *before* calling tp_del, |
| 1159 clearing slots, or clearing the instance dict. |
| 1160 |
| 1161 GC tracking must be off at this point. weakref callbacks (if any, and |
| 1162 whether directly here or indirectly in something we call) may trigger GC, |
| 1163 and if self is tracked at that point, it will look like trash to GC and GC |
| 1164 will try to delete self again. |
| 1165 */ |
1161 if (type->tp_weaklistoffset && !base->tp_weaklistoffset) | 1166 if (type->tp_weaklistoffset && !base->tp_weaklistoffset) |
1162 PyObject_ClearWeakRefs(self); | 1167 PyObject_ClearWeakRefs(self); |
1163 | 1168 |
1164 if (type->tp_del) { | 1169 if (type->tp_del) { |
| 1170 _PyObject_GC_TRACK(self); |
1165 type->tp_del(self); | 1171 type->tp_del(self); |
1166 if (self->ob_refcnt > 0) { | 1172 if (self->ob_refcnt > 0) { |
1167 /* Resurrected */ | 1173 /* Resurrected */ |
1168 goto endlabel; | 1174 goto endlabel; |
1169 } | 1175 } |
| 1176 _PyObject_GC_UNTRACK(self); |
1170 } | 1177 } |
1171 if (has_finalizer) { | 1178 if (has_finalizer) { |
1172 _PyObject_GC_UNTRACK(self); | |
1173 /* New weakrefs could be created during the finalizer call. | 1179 /* New weakrefs could be created during the finalizer call. |
1174 If this occurs, clear them out without calling their | 1180 If this occurs, clear them out without calling their |
1175 finalizers since they might rely on part of the object | 1181 finalizers since they might rely on part of the object |
1176 being finalized that has already been destroyed. */ | 1182 being finalized that has already been destroyed. */ |
1177 if (type->tp_weaklistoffset && !base->tp_weaklistoffset) { | 1183 if (type->tp_weaklistoffset && !base->tp_weaklistoffset) { |
1178 /* Modeled after GET_WEAKREFS_LISTPTR() */ | 1184 /* Modeled after GET_WEAKREFS_LISTPTR() */ |
1179 PyWeakReference **list = (PyWeakReference **) \ | 1185 PyWeakReference **list = (PyWeakReference **) \ |
1180 PyObject_GET_WEAKREFS_LISTPTR(self); | 1186 PyObject_GET_WEAKREFS_LISTPTR(self); |
1181 while (*list) | 1187 while (*list) |
1182 _PyWeakref_ClearRef(*list); | 1188 _PyWeakref_ClearRef(*list); |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1324 compiled in debug mode) before the trashcan level manipulations | 1330 compiled in debug mode) before the trashcan level manipulations |
1325 were added. For more discussion, see SF patches 581742, 575073 | 1331 were added. For more discussion, see SF patches 581742, 575073 |
1326 and bug 574207. | 1332 and bug 574207. |
1327 */ | 1333 */ |
1328 } | 1334 } |
1329 | 1335 |
1330 static PyTypeObject *solid_base(PyTypeObject *type); | 1336 static PyTypeObject *solid_base(PyTypeObject *type); |
1331 | 1337 |
1332 /* type test with subclassing support */ | 1338 /* type test with subclassing support */ |
1333 | 1339 |
1334 Py_LOCAL_INLINE(int) | 1340 static int |
1335 type_is_subtype_base_chain(PyTypeObject *a, PyTypeObject *b) | 1341 type_is_subtype_base_chain(PyTypeObject *a, PyTypeObject *b) |
1336 { | 1342 { |
1337 do { | 1343 do { |
1338 if (a == b) | 1344 if (a == b) |
1339 return 1; | 1345 return 1; |
1340 a = a->tp_base; | 1346 a = a->tp_base; |
1341 } while (a != NULL); | 1347 } while (a != NULL); |
1342 | 1348 |
1343 return (b == &PyBaseObject_Type); | 1349 return (b == &PyBaseObject_Type); |
1344 } | 1350 } |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1415 } | 1421 } |
1416 | 1422 |
1417 /* A variation of PyObject_CallMethod that uses lookup_method() | 1423 /* A variation of PyObject_CallMethod that uses lookup_method() |
1418 instead of PyObject_GetAttrString(). This uses the same convention | 1424 instead of PyObject_GetAttrString(). This uses the same convention |
1419 as lookup_method to cache the interned name string object. */ | 1425 as lookup_method to cache the interned name string object. */ |
1420 | 1426 |
1421 static PyObject * | 1427 static PyObject * |
1422 call_method(PyObject *o, _Py_Identifier *nameid, const char *format, ...) | 1428 call_method(PyObject *o, _Py_Identifier *nameid, const char *format, ...) |
1423 { | 1429 { |
1424 va_list va; | 1430 va_list va; |
1425 PyObject *args, *func = 0, *retval; | 1431 PyObject *func = NULL, *retval; |
1426 va_start(va, format); | |
1427 | 1432 |
1428 func = lookup_maybe(o, nameid); | 1433 func = lookup_maybe(o, nameid); |
1429 if (func == NULL) { | 1434 if (func == NULL) { |
1430 va_end(va); | |
1431 if (!PyErr_Occurred()) | 1435 if (!PyErr_Occurred()) |
1432 PyErr_SetObject(PyExc_AttributeError, nameid->object); | 1436 PyErr_SetObject(PyExc_AttributeError, nameid->object); |
1433 return NULL; | 1437 return NULL; |
1434 } | 1438 } |
1435 | 1439 |
1436 if (format && *format) | 1440 if (format && *format) { |
| 1441 PyObject *args; |
| 1442 |
| 1443 va_start(va, format); |
1437 args = Py_VaBuildValue(format, va); | 1444 args = Py_VaBuildValue(format, va); |
1438 else | 1445 va_end(va); |
1439 args = PyTuple_New(0); | 1446 |
1440 | 1447 if (args == NULL) { |
1441 va_end(va); | 1448 Py_DECREF(func); |
1442 | 1449 return NULL; |
1443 if (args == NULL) | 1450 } |
1444 return NULL; | 1451 assert(PyTuple_Check(args)); |
1445 | 1452 |
1446 assert(PyTuple_Check(args)); | 1453 retval = PyObject_Call(func, args, NULL); |
1447 retval = PyObject_Call(func, args, NULL); | 1454 Py_DECREF(args); |
1448 | 1455 } |
1449 Py_DECREF(args); | 1456 else { |
| 1457 retval = _PyObject_CallNoArg(func); |
| 1458 } |
| 1459 |
1450 Py_DECREF(func); | 1460 Py_DECREF(func); |
1451 | 1461 |
1452 return retval; | 1462 return retval; |
1453 } | 1463 } |
1454 | 1464 |
1455 /* Clone of call_method() that returns NotImplemented when the lookup fails. */ | 1465 /* Clone of call_method() that returns NotImplemented when the lookup fails. */ |
1456 | 1466 |
1457 static PyObject * | 1467 static PyObject * |
1458 call_maybe(PyObject *o, _Py_Identifier *nameid, const char *format, ...) | 1468 call_maybe(PyObject *o, _Py_Identifier *nameid, const char *format, ...) |
1459 { | 1469 { |
1460 va_list va; | 1470 va_list va; |
1461 PyObject *args, *func = 0, *retval; | 1471 PyObject *func = NULL, *retval; |
1462 va_start(va, format); | |
1463 | 1472 |
1464 func = lookup_maybe(o, nameid); | 1473 func = lookup_maybe(o, nameid); |
1465 if (func == NULL) { | 1474 if (func == NULL) { |
1466 va_end(va); | |
1467 if (!PyErr_Occurred()) | 1475 if (!PyErr_Occurred()) |
1468 Py_RETURN_NOTIMPLEMENTED; | 1476 Py_RETURN_NOTIMPLEMENTED; |
1469 return NULL; | 1477 return NULL; |
1470 } | 1478 } |
1471 | 1479 |
1472 if (format && *format) | 1480 if (format && *format) { |
| 1481 PyObject *args; |
| 1482 |
| 1483 va_start(va, format); |
1473 args = Py_VaBuildValue(format, va); | 1484 args = Py_VaBuildValue(format, va); |
1474 else | 1485 va_end(va); |
1475 args = PyTuple_New(0); | 1486 |
1476 | 1487 if (args == NULL) { |
1477 va_end(va); | 1488 Py_DECREF(func); |
1478 | 1489 return NULL; |
1479 if (args == NULL) | 1490 } |
1480 return NULL; | 1491 assert(PyTuple_Check(args)); |
1481 | 1492 |
1482 assert(PyTuple_Check(args)); | 1493 retval = PyObject_Call(func, args, NULL); |
1483 retval = PyObject_Call(func, args, NULL); | 1494 Py_DECREF(args); |
1484 | 1495 } |
1485 Py_DECREF(args); | 1496 else { |
| 1497 retval = _PyObject_CallNoArg(func); |
| 1498 } |
| 1499 |
1486 Py_DECREF(func); | 1500 Py_DECREF(func); |
1487 | 1501 |
1488 return retval; | 1502 return retval; |
1489 } | 1503 } |
1490 | 1504 |
1491 /* | 1505 /* |
1492 Method resolution order algorithm C3 described in | 1506 Method resolution order algorithm C3 described in |
1493 "A Monotonic Superclass Linearization for Dylan", | 1507 "A Monotonic Superclass Linearization for Dylan", |
1494 by Kim Barrett, Bob Cassel, Paul Haahr, | 1508 by Kim Barrett, Bob Cassel, Paul Haahr, |
1495 David A. Moon, Keith Playford, and P. Tucker Withington. | 1509 David A. Moon, Keith Playford, and P. Tucker Withington. |
1496 (OOPSLA 1996) | 1510 (OOPSLA 1996) |
1497 | 1511 |
1498 Some notes about the rules implied by C3: | 1512 Some notes about the rules implied by C3: |
1499 | 1513 |
1500 No duplicate bases. | 1514 No duplicate bases. |
1501 It isn't legal to repeat a class in a list of base classes. | 1515 It isn't legal to repeat a class in a list of base classes. |
1502 | 1516 |
1503 The next three properties are the 3 constraints in "C3". | 1517 The next three properties are the 3 constraints in "C3". |
1504 | 1518 |
1505 Local precendece order. | 1519 Local precedence order. |
1506 If A precedes B in C's MRO, then A will precede B in the MRO of all | 1520 If A precedes B in C's MRO, then A will precede B in the MRO of all |
1507 subclasses of C. | 1521 subclasses of C. |
1508 | 1522 |
1509 Monotonicity. | 1523 Monotonicity. |
1510 The MRO of a class must be an extension without reordering of the | 1524 The MRO of a class must be an extension without reordering of the |
1511 MRO of each of its superclasses. | 1525 MRO of each of its superclasses. |
1512 | 1526 |
1513 Extended Precedence Graph (EPG). | 1527 Extended Precedence Graph (EPG). |
1514 Linearization is consistent if there is a path in the EPG from | 1528 Linearization is consistent if there is a path in the EPG from |
1515 each class to all its successors in the linearization. See | 1529 each class to all its successors in the linearization. See |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1605 } | 1619 } |
1606 n = PyDict_Size(set); | 1620 n = PyDict_Size(set); |
1607 | 1621 |
1608 off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \ | 1622 off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \ |
1609 consistent method resolution\norder (MRO) for bases"); | 1623 consistent method resolution\norder (MRO) for bases"); |
1610 i = 0; | 1624 i = 0; |
1611 while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) { | 1625 while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) { |
1612 PyObject *name = class_name(k); | 1626 PyObject *name = class_name(k); |
1613 char *name_str; | 1627 char *name_str; |
1614 if (name != NULL) { | 1628 if (name != NULL) { |
1615 name_str = _PyUnicode_AsString(name); | 1629 name_str = PyUnicode_AsUTF8(name); |
1616 if (name_str == NULL) | 1630 if (name_str == NULL) |
1617 name_str = "?"; | 1631 name_str = "?"; |
1618 } else | 1632 } else |
1619 name_str = "?"; | 1633 name_str = "?"; |
1620 off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s", name_str); | 1634 off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s", name_str); |
1621 Py_XDECREF(name); | 1635 Py_XDECREF(name); |
1622 if (--n && (size_t)(off+1) < sizeof(buf)) { | 1636 if (--n && (size_t)(off+1) < sizeof(buf)) { |
1623 buf[off++] = ','; | 1637 buf[off++] = ','; |
1624 buf[off] = '\0'; | 1638 buf[off] = '\0'; |
1625 } | 1639 } |
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2020 if (extra_ivars(type, base)) | 2034 if (extra_ivars(type, base)) |
2021 return type; | 2035 return type; |
2022 else | 2036 else |
2023 return base; | 2037 return base; |
2024 } | 2038 } |
2025 | 2039 |
2026 static void object_dealloc(PyObject *); | 2040 static void object_dealloc(PyObject *); |
2027 static int object_init(PyObject *, PyObject *, PyObject *); | 2041 static int object_init(PyObject *, PyObject *, PyObject *); |
2028 static int update_slot(PyTypeObject *, PyObject *); | 2042 static int update_slot(PyTypeObject *, PyObject *); |
2029 static void fixup_slot_dispatchers(PyTypeObject *); | 2043 static void fixup_slot_dispatchers(PyTypeObject *); |
| 2044 static int set_names(PyTypeObject *); |
| 2045 static int init_subclass(PyTypeObject *, PyObject *); |
2030 | 2046 |
2031 /* | 2047 /* |
2032 * Helpers for __dict__ descriptor. We don't want to expose the dicts | 2048 * Helpers for __dict__ descriptor. We don't want to expose the dicts |
2033 * inherited from various builtin types. The builtin base usually provides | 2049 * inherited from various builtin types. The builtin base usually provides |
2034 * its own __dict__ descriptor, so we use that when we can. | 2050 * its own __dict__ descriptor, so we use that when we can. |
2035 */ | 2051 */ |
2036 static PyTypeObject * | 2052 static PyTypeObject * |
2037 get_builtin_base_with_dict(PyTypeObject *type) | 2053 get_builtin_base_with_dict(PyTypeObject *type) |
2038 { | 2054 { |
2039 while (type->tp_base != NULL) { | 2055 while (type->tp_base != NULL) { |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2195 object_init(PyObject *self, PyObject *args, PyObject *kwds); | 2211 object_init(PyObject *self, PyObject *args, PyObject *kwds); |
2196 | 2212 |
2197 static int | 2213 static int |
2198 type_init(PyObject *cls, PyObject *args, PyObject *kwds) | 2214 type_init(PyObject *cls, PyObject *args, PyObject *kwds) |
2199 { | 2215 { |
2200 int res; | 2216 int res; |
2201 | 2217 |
2202 assert(args != NULL && PyTuple_Check(args)); | 2218 assert(args != NULL && PyTuple_Check(args)); |
2203 assert(kwds == NULL || PyDict_Check(kwds)); | 2219 assert(kwds == NULL || PyDict_Check(kwds)); |
2204 | 2220 |
2205 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) { | 2221 if (kwds != NULL && PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 && |
| 2222 PyDict_Check(kwds) && PyDict_Size(kwds) != 0) { |
2206 PyErr_SetString(PyExc_TypeError, | 2223 PyErr_SetString(PyExc_TypeError, |
2207 "type.__init__() takes no keyword arguments"); | 2224 "type.__init__() takes no keyword arguments"); |
2208 return -1; | 2225 return -1; |
2209 } | 2226 } |
2210 | 2227 |
2211 if (args != NULL && PyTuple_Check(args) && | 2228 if (args != NULL && PyTuple_Check(args) && |
2212 (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) { | 2229 (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) { |
2213 PyErr_SetString(PyExc_TypeError, | 2230 PyErr_SetString(PyExc_TypeError, |
2214 "type.__init__() takes 1 or 3 arguments"); | 2231 "type.__init__() takes 1 or 3 arguments"); |
2215 return -1; | 2232 return -1; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2262 "of the metaclasses of all its bases"); | 2279 "of the metaclasses of all its bases"); |
2263 return NULL; | 2280 return NULL; |
2264 } | 2281 } |
2265 return winner; | 2282 return winner; |
2266 } | 2283 } |
2267 | 2284 |
2268 static PyObject * | 2285 static PyObject * |
2269 type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) | 2286 type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) |
2270 { | 2287 { |
2271 PyObject *name, *bases = NULL, *orig_dict, *dict = NULL; | 2288 PyObject *name, *bases = NULL, *orig_dict, *dict = NULL; |
2272 static char *kwlist[] = {"name", "bases", "dict", 0}; | |
2273 PyObject *qualname, *slots = NULL, *tmp, *newslots, *cell; | 2289 PyObject *qualname, *slots = NULL, *tmp, *newslots, *cell; |
2274 PyTypeObject *type = NULL, *base, *tmptype, *winner; | 2290 PyTypeObject *type = NULL, *base, *tmptype, *winner; |
2275 PyHeapTypeObject *et; | 2291 PyHeapTypeObject *et; |
2276 PyMemberDef *mp; | 2292 PyMemberDef *mp; |
2277 Py_ssize_t i, nbases, nslots, slotoffset, name_size; | 2293 Py_ssize_t i, nbases, nslots, slotoffset, name_size; |
2278 int j, may_add_dict, may_add_weak, add_dict, add_weak; | 2294 int j, may_add_dict, may_add_weak, add_dict, add_weak; |
2279 _Py_IDENTIFIER(__qualname__); | 2295 _Py_IDENTIFIER(__qualname__); |
2280 _Py_IDENTIFIER(__slots__); | 2296 _Py_IDENTIFIER(__slots__); |
2281 _Py_IDENTIFIER(__classcell__); | 2297 _Py_IDENTIFIER(__classcell__); |
2282 | 2298 |
2283 assert(args != NULL && PyTuple_Check(args)); | 2299 assert(args != NULL && PyTuple_Check(args)); |
2284 assert(kwds == NULL || PyDict_Check(kwds)); | 2300 assert(kwds == NULL || PyDict_Check(kwds)); |
2285 | 2301 |
2286 /* Special case: type(x) should return x->ob_type */ | 2302 /* Special case: type(x) should return x->ob_type */ |
2287 { | 2303 /* We only want type itself to accept the one-argument form (#27157) |
| 2304 Note: We don't call PyType_CheckExact as that also allows subclasses */ |
| 2305 if (metatype == &PyType_Type) { |
2288 const Py_ssize_t nargs = PyTuple_GET_SIZE(args); | 2306 const Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
2289 const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds); | 2307 const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds); |
2290 | 2308 |
2291 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) { | 2309 if (nargs == 1 && nkwds == 0) { |
2292 PyObject *x = PyTuple_GET_ITEM(args, 0); | 2310 PyObject *x = PyTuple_GET_ITEM(args, 0); |
2293 Py_INCREF(Py_TYPE(x)); | 2311 Py_INCREF(Py_TYPE(x)); |
2294 return (PyObject *) Py_TYPE(x); | 2312 return (PyObject *) Py_TYPE(x); |
2295 } | 2313 } |
2296 | 2314 |
2297 /* SF bug 475327 -- if that didn't trigger, we need 3 | 2315 /* SF bug 475327 -- if that didn't trigger, we need 3 |
2298 arguments. but PyArg_ParseTupleAndKeywords below may give | 2316 arguments. but PyArg_ParseTupleAndKeywords below may give |
2299 a msg saying type() needs exactly 3. */ | 2317 a msg saying type() needs exactly 3. */ |
2300 if (nargs + nkwds != 3) { | 2318 if (nargs != 3) { |
2301 PyErr_SetString(PyExc_TypeError, | 2319 PyErr_SetString(PyExc_TypeError, |
2302 "type() takes 1 or 3 arguments"); | 2320 "type() takes 1 or 3 arguments"); |
2303 return NULL; | 2321 return NULL; |
2304 } | 2322 } |
2305 } | 2323 } |
2306 | 2324 |
2307 /* Check arguments: (name, bases, dict) */ | 2325 /* Check arguments: (name, bases, dict) */ |
2308 if (!PyArg_ParseTupleAndKeywords(args, kwds, "UO!O!:type", kwlist, | 2326 if (!PyArg_ParseTuple(args, "UO!O!:type.__new__", &name, &PyTuple_Type, |
2309 &name, | 2327 &bases, &PyDict_Type, &orig_dict)) |
2310 &PyTuple_Type, &bases, | |
2311 &PyDict_Type, &orig_dict)) | |
2312 return NULL; | 2328 return NULL; |
2313 | 2329 |
2314 /* Determine the proper metatype to deal with this: */ | 2330 /* Determine the proper metatype to deal with this: */ |
2315 winner = _PyType_CalculateMetaclass(metatype, bases); | 2331 winner = _PyType_CalculateMetaclass(metatype, bases); |
2316 if (winner == NULL) { | 2332 if (winner == NULL) { |
2317 return NULL; | 2333 return NULL; |
2318 } | 2334 } |
2319 | 2335 |
2320 if (winner != metatype) { | 2336 if (winner != metatype) { |
2321 if (winner->tp_new != type_new) /* Pass it to the winner */ | 2337 if (winner->tp_new != type_new) /* Pass it to the winner */ |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2380 base->tp_name); | 2396 base->tp_name); |
2381 goto error; | 2397 goto error; |
2382 } | 2398 } |
2383 | 2399 |
2384 /* Check for valid slot names and two special cases */ | 2400 /* Check for valid slot names and two special cases */ |
2385 for (i = 0; i < nslots; i++) { | 2401 for (i = 0; i < nslots; i++) { |
2386 PyObject *tmp = PyTuple_GET_ITEM(slots, i); | 2402 PyObject *tmp = PyTuple_GET_ITEM(slots, i); |
2387 if (!valid_identifier(tmp)) | 2403 if (!valid_identifier(tmp)) |
2388 goto error; | 2404 goto error; |
2389 assert(PyUnicode_Check(tmp)); | 2405 assert(PyUnicode_Check(tmp)); |
2390 if (_PyUnicode_CompareWithId(tmp, &PyId___dict__) == 0) { | 2406 if (_PyUnicode_EqualToASCIIId(tmp, &PyId___dict__)) { |
2391 if (!may_add_dict || add_dict) { | 2407 if (!may_add_dict || add_dict) { |
2392 PyErr_SetString(PyExc_TypeError, | 2408 PyErr_SetString(PyExc_TypeError, |
2393 "__dict__ slot disallowed: " | 2409 "__dict__ slot disallowed: " |
2394 "we already got one"); | 2410 "we already got one"); |
2395 goto error; | 2411 goto error; |
2396 } | 2412 } |
2397 add_dict++; | 2413 add_dict++; |
2398 } | 2414 } |
2399 if (PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0) { | 2415 if (_PyUnicode_EqualToASCIIString(tmp, "__weakref__")) { |
2400 if (!may_add_weak || add_weak) { | 2416 if (!may_add_weak || add_weak) { |
2401 PyErr_SetString(PyExc_TypeError, | 2417 PyErr_SetString(PyExc_TypeError, |
2402 "__weakref__ slot disallowed: " | 2418 "__weakref__ slot disallowed: " |
2403 "either we already got one, " | 2419 "either we already got one, " |
2404 "or __itemsize__ != 0"); | 2420 "or __itemsize__ != 0"); |
2405 goto error; | 2421 goto error; |
2406 } | 2422 } |
2407 add_weak++; | 2423 add_weak++; |
2408 } | 2424 } |
2409 } | 2425 } |
2410 | 2426 |
2411 /* Copy slots into a list, mangle names and sort them. | 2427 /* Copy slots into a list, mangle names and sort them. |
2412 Sorted names are needed for __class__ assignment. | 2428 Sorted names are needed for __class__ assignment. |
2413 Convert them back to tuple at the end. | 2429 Convert them back to tuple at the end. |
2414 */ | 2430 */ |
2415 newslots = PyList_New(nslots - add_dict - add_weak); | 2431 newslots = PyList_New(nslots - add_dict - add_weak); |
2416 if (newslots == NULL) | 2432 if (newslots == NULL) |
2417 goto error; | 2433 goto error; |
2418 for (i = j = 0; i < nslots; i++) { | 2434 for (i = j = 0; i < nslots; i++) { |
2419 tmp = PyTuple_GET_ITEM(slots, i); | 2435 tmp = PyTuple_GET_ITEM(slots, i); |
2420 if ((add_dict && | 2436 if ((add_dict && |
2421 _PyUnicode_CompareWithId(tmp, &PyId___dict__) == 0) || | 2437 _PyUnicode_EqualToASCIIId(tmp, &PyId___dict__)) || |
2422 (add_weak && | 2438 (add_weak && |
2423 PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0)) | 2439 _PyUnicode_EqualToASCIIString(tmp, "__weakref__"))) |
2424 continue; | 2440 continue; |
2425 tmp =_Py_Mangle(name, tmp); | 2441 tmp =_Py_Mangle(name, tmp); |
2426 if (!tmp) { | 2442 if (!tmp) { |
2427 Py_DECREF(newslots); | 2443 Py_DECREF(newslots); |
2428 goto error; | 2444 goto error; |
2429 } | 2445 } |
2430 PyList_SET_ITEM(newslots, j, tmp); | 2446 PyList_SET_ITEM(newslots, j, tmp); |
2431 if (PyDict_GetItem(dict, tmp)) { | 2447 if (PyDict_GetItem(dict, tmp)) { |
2432 PyErr_Format(PyExc_ValueError, | 2448 PyErr_Format(PyExc_ValueError, |
2433 "%R in __slots__ conflicts with class variable", | 2449 "%R in __slots__ conflicts with class variable", |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2552 and is a string. The __doc__ accessor will first look for tp_doc; | 2568 and is a string. The __doc__ accessor will first look for tp_doc; |
2553 if that fails, it will still look into __dict__. | 2569 if that fails, it will still look into __dict__. |
2554 */ | 2570 */ |
2555 { | 2571 { |
2556 PyObject *doc = _PyDict_GetItemId(dict, &PyId___doc__); | 2572 PyObject *doc = _PyDict_GetItemId(dict, &PyId___doc__); |
2557 if (doc != NULL && PyUnicode_Check(doc)) { | 2573 if (doc != NULL && PyUnicode_Check(doc)) { |
2558 Py_ssize_t len; | 2574 Py_ssize_t len; |
2559 char *doc_str; | 2575 char *doc_str; |
2560 char *tp_doc; | 2576 char *tp_doc; |
2561 | 2577 |
2562 doc_str = _PyUnicode_AsString(doc); | 2578 doc_str = PyUnicode_AsUTF8(doc); |
2563 if (doc_str == NULL) | 2579 if (doc_str == NULL) |
2564 goto error; | 2580 goto error; |
2565 /* Silently truncate the docstring if it contains null bytes. */ | 2581 /* Silently truncate the docstring if it contains null bytes. */ |
2566 len = strlen(doc_str); | 2582 len = strlen(doc_str); |
2567 tp_doc = (char *)PyObject_MALLOC(len + 1); | 2583 tp_doc = (char *)PyObject_MALLOC(len + 1); |
2568 if (tp_doc == NULL) { | 2584 if (tp_doc == NULL) { |
2569 PyErr_NoMemory(); | 2585 PyErr_NoMemory(); |
2570 goto error; | 2586 goto error; |
2571 } | 2587 } |
2572 memcpy(tp_doc, doc_str, len + 1); | 2588 memcpy(tp_doc, doc_str, len + 1); |
2573 type->tp_doc = tp_doc; | 2589 type->tp_doc = tp_doc; |
2574 } | 2590 } |
2575 } | 2591 } |
2576 | 2592 |
2577 /* Special-case __new__: if it's a plain function, | 2593 /* Special-case __new__: if it's a plain function, |
2578 make it a static function */ | 2594 make it a static function */ |
2579 tmp = _PyDict_GetItemId(dict, &PyId___new__); | 2595 tmp = _PyDict_GetItemId(dict, &PyId___new__); |
2580 if (tmp != NULL && PyFunction_Check(tmp)) { | 2596 if (tmp != NULL && PyFunction_Check(tmp)) { |
2581 tmp = PyStaticMethod_New(tmp); | 2597 tmp = PyStaticMethod_New(tmp); |
2582 if (tmp == NULL) | 2598 if (tmp == NULL) |
2583 goto error; | 2599 goto error; |
2584 if (_PyDict_SetItemId(dict, &PyId___new__, tmp) < 0) { | 2600 if (_PyDict_SetItemId(dict, &PyId___new__, tmp) < 0) { |
2585 Py_DECREF(tmp); | 2601 Py_DECREF(tmp); |
2586 goto error; | 2602 goto error; |
2587 } | 2603 } |
2588 Py_DECREF(tmp); | 2604 Py_DECREF(tmp); |
2589 } | 2605 } |
2590 | 2606 |
| 2607 /* Special-case __init_subclass__: if it's a plain function, |
| 2608 make it a classmethod */ |
| 2609 tmp = _PyDict_GetItemId(dict, &PyId___init_subclass__); |
| 2610 if (tmp != NULL && PyFunction_Check(tmp)) { |
| 2611 tmp = PyClassMethod_New(tmp); |
| 2612 if (tmp == NULL) |
| 2613 goto error; |
| 2614 if (_PyDict_SetItemId(dict, &PyId___init_subclass__, tmp) < 0) { |
| 2615 Py_DECREF(tmp); |
| 2616 goto error; |
| 2617 } |
| 2618 Py_DECREF(tmp); |
| 2619 } |
| 2620 |
2591 /* Add descriptors for custom slots from __slots__, or for __dict__ */ | 2621 /* Add descriptors for custom slots from __slots__, or for __dict__ */ |
2592 mp = PyHeapType_GET_MEMBERS(et); | 2622 mp = PyHeapType_GET_MEMBERS(et); |
2593 slotoffset = base->tp_basicsize; | 2623 slotoffset = base->tp_basicsize; |
2594 if (et->ht_slots != NULL) { | 2624 if (et->ht_slots != NULL) { |
2595 for (i = 0; i < nslots; i++, mp++) { | 2625 for (i = 0; i < nslots; i++, mp++) { |
2596 mp->name = _PyUnicode_AsString( | 2626 mp->name = PyUnicode_AsUTF8( |
2597 PyTuple_GET_ITEM(et->ht_slots, i)); | 2627 PyTuple_GET_ITEM(et->ht_slots, i)); |
2598 if (mp->name == NULL) | 2628 if (mp->name == NULL) |
2599 goto error; | 2629 goto error; |
2600 mp->type = T_OBJECT_EX; | 2630 mp->type = T_OBJECT_EX; |
2601 mp->offset = slotoffset; | 2631 mp->offset = slotoffset; |
2602 | 2632 |
2603 /* __dict__ and __weakref__ are already filtered out */ | 2633 /* __dict__ and __weakref__ are already filtered out */ |
2604 assert(strcmp(mp->name, "__dict__") != 0); | 2634 assert(strcmp(mp->name, "__dict__") != 0); |
2605 assert(strcmp(mp->name, "__weakref__") != 0); | 2635 assert(strcmp(mp->name, "__weakref__") != 0); |
2606 | 2636 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2650 /* Always override allocation strategy to use regular heap */ | 2680 /* Always override allocation strategy to use regular heap */ |
2651 type->tp_alloc = PyType_GenericAlloc; | 2681 type->tp_alloc = PyType_GenericAlloc; |
2652 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) { | 2682 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) { |
2653 type->tp_free = PyObject_GC_Del; | 2683 type->tp_free = PyObject_GC_Del; |
2654 type->tp_traverse = subtype_traverse; | 2684 type->tp_traverse = subtype_traverse; |
2655 type->tp_clear = subtype_clear; | 2685 type->tp_clear = subtype_clear; |
2656 } | 2686 } |
2657 else | 2687 else |
2658 type->tp_free = PyObject_Del; | 2688 type->tp_free = PyObject_Del; |
2659 | 2689 |
2660 /* store type in class' cell */ | 2690 /* store type in class' cell if one is supplied */ |
2661 cell = _PyDict_GetItemId(dict, &PyId___classcell__); | 2691 cell = _PyDict_GetItemId(dict, &PyId___classcell__); |
2662 if (cell != NULL && PyCell_Check(cell)) { | 2692 if (cell != NULL) { |
| 2693 /* At least one method requires a reference to its defining class */ |
| 2694 if (!PyCell_Check(cell)) { |
| 2695 PyErr_Format(PyExc_TypeError, |
| 2696 "__classcell__ must be a nonlocal cell, not %.200R", |
| 2697 Py_TYPE(cell)); |
| 2698 goto error; |
| 2699 } |
2663 PyCell_Set(cell, (PyObject *) type); | 2700 PyCell_Set(cell, (PyObject *) type); |
2664 _PyDict_DelItemId(dict, &PyId___classcell__); | 2701 _PyDict_DelItemId(dict, &PyId___classcell__); |
2665 PyErr_Clear(); | 2702 PyErr_Clear(); |
2666 } | 2703 } |
2667 | 2704 |
2668 /* Initialize the rest */ | 2705 /* Initialize the rest */ |
2669 if (PyType_Ready(type) < 0) | 2706 if (PyType_Ready(type) < 0) |
2670 goto error; | 2707 goto error; |
2671 | 2708 |
2672 /* Put the proper slots in place */ | 2709 /* Put the proper slots in place */ |
2673 fixup_slot_dispatchers(type); | 2710 fixup_slot_dispatchers(type); |
2674 | 2711 |
2675 if (type->tp_dictoffset) { | 2712 if (type->tp_dictoffset) { |
2676 et->ht_cached_keys = _PyDict_NewKeysForClass(); | 2713 et->ht_cached_keys = _PyDict_NewKeysForClass(); |
2677 } | 2714 } |
| 2715 |
| 2716 if (set_names(type) < 0) |
| 2717 goto error; |
| 2718 |
| 2719 if (init_subclass(type, kwds) < 0) |
| 2720 goto error; |
2678 | 2721 |
2679 Py_DECREF(dict); | 2722 Py_DECREF(dict); |
2680 return (PyObject *)type; | 2723 return (PyObject *)type; |
2681 | 2724 |
2682 error: | 2725 error: |
2683 Py_XDECREF(dict); | 2726 Py_XDECREF(dict); |
2684 Py_XDECREF(bases); | 2727 Py_XDECREF(bases); |
2685 Py_XDECREF(slots); | 2728 Py_XDECREF(slots); |
2686 Py_XDECREF(type); | 2729 Py_XDECREF(type); |
2687 return NULL; | 2730 return NULL; |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2805 if (PyType_Ready(type) < 0) | 2848 if (PyType_Ready(type) < 0) |
2806 goto fail; | 2849 goto fail; |
2807 | 2850 |
2808 if (type->tp_dictoffset) { | 2851 if (type->tp_dictoffset) { |
2809 res->ht_cached_keys = _PyDict_NewKeysForClass(); | 2852 res->ht_cached_keys = _PyDict_NewKeysForClass(); |
2810 } | 2853 } |
2811 | 2854 |
2812 /* Set type.__module__ */ | 2855 /* Set type.__module__ */ |
2813 s = strrchr(spec->name, '.'); | 2856 s = strrchr(spec->name, '.'); |
2814 if (s != NULL) { | 2857 if (s != NULL) { |
| 2858 int err; |
2815 modname = PyUnicode_FromStringAndSize( | 2859 modname = PyUnicode_FromStringAndSize( |
2816 spec->name, (Py_ssize_t)(s - spec->name)); | 2860 spec->name, (Py_ssize_t)(s - spec->name)); |
2817 if (modname == NULL) { | 2861 if (modname == NULL) { |
2818 goto fail; | 2862 goto fail; |
2819 } | 2863 } |
2820 _PyDict_SetItemId(type->tp_dict, &PyId___module__, modname); | 2864 err = _PyDict_SetItemId(type->tp_dict, &PyId___module__, modname); |
2821 Py_DECREF(modname); | 2865 Py_DECREF(modname); |
| 2866 if (err != 0) |
| 2867 goto fail; |
2822 } else { | 2868 } else { |
2823 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, | 2869 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
2824 "builtin type %.200s has no __module__ attribute", | 2870 "builtin type %.200s has no __module__ attribute", |
2825 spec->name)) | 2871 spec->name)) |
2826 goto fail; | 2872 goto fail; |
2827 } | 2873 } |
2828 | 2874 |
2829 return (PyObject*)res; | 2875 return (PyObject*)res; |
2830 | 2876 |
2831 fail: | 2877 fail: |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2871 #if MCACHE_STATS | 2917 #if MCACHE_STATS |
2872 method_cache_hits++; | 2918 method_cache_hits++; |
2873 #endif | 2919 #endif |
2874 return method_cache[h].value; | 2920 return method_cache[h].value; |
2875 } | 2921 } |
2876 } | 2922 } |
2877 | 2923 |
2878 /* Look in tp_dict of types in MRO */ | 2924 /* Look in tp_dict of types in MRO */ |
2879 mro = type->tp_mro; | 2925 mro = type->tp_mro; |
2880 | 2926 |
2881 /* If mro is NULL, the type is either not yet initialized | 2927 if (mro == NULL) { |
2882 by PyType_Ready(), or already cleared by type_clear(). | 2928 if ((type->tp_flags & Py_TPFLAGS_READYING) == 0 && |
2883 Either way the safest thing to do is to return NULL. */ | 2929 PyType_Ready(type) < 0) { |
2884 if (mro == NULL) | 2930 /* It's not ideal to clear the error condition, |
2885 return NULL; | 2931 but this function is documented as not setting |
| 2932 an exception, and I don't want to change that. |
| 2933 When PyType_Ready() can't proceed, it won't |
| 2934 set the "ready" flag, so future attempts to ready |
| 2935 the same type will call it again -- hopefully |
| 2936 in a context that propagates the exception out. |
| 2937 */ |
| 2938 PyErr_Clear(); |
| 2939 return NULL; |
| 2940 } |
| 2941 mro = type->tp_mro; |
| 2942 if (mro == NULL) { |
| 2943 return NULL; |
| 2944 } |
| 2945 } |
2886 | 2946 |
2887 res = NULL; | 2947 res = NULL; |
2888 /* keep a strong reference to mro because type->tp_mro can be replaced | 2948 /* keep a strong reference to mro because type->tp_mro can be replaced |
2889 during PyDict_GetItem(dict, name) */ | 2949 during PyDict_GetItem(dict, name) */ |
2890 Py_INCREF(mro); | 2950 Py_INCREF(mro); |
2891 assert(PyTuple_Check(mro)); | 2951 assert(PyTuple_Check(mro)); |
2892 n = PyTuple_GET_SIZE(mro); | 2952 n = PyTuple_GET_SIZE(mro); |
2893 for (i = 0; i < n; i++) { | 2953 for (i = 0; i < n; i++) { |
2894 base = PyTuple_GET_ITEM(mro, i); | 2954 base = PyTuple_GET_ITEM(mro, i); |
2895 assert(PyType_Check(base)); | 2955 assert(PyType_Check(base)); |
(...skipping 582 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3478 PyErr_Clear(); | 3538 PyErr_Clear(); |
3479 else if (!PyUnicode_Check(mod)) { | 3539 else if (!PyUnicode_Check(mod)) { |
3480 Py_DECREF(mod); | 3540 Py_DECREF(mod); |
3481 mod = NULL; | 3541 mod = NULL; |
3482 } | 3542 } |
3483 name = type_qualname(type, NULL); | 3543 name = type_qualname(type, NULL); |
3484 if (name == NULL) { | 3544 if (name == NULL) { |
3485 Py_XDECREF(mod); | 3545 Py_XDECREF(mod); |
3486 return NULL; | 3546 return NULL; |
3487 } | 3547 } |
3488 if (mod != NULL && _PyUnicode_CompareWithId(mod, &PyId_builtins)) | 3548 if (mod != NULL && !_PyUnicode_EqualToASCIIId(mod, &PyId_builtins)) |
3489 rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self); | 3549 rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self); |
3490 else | 3550 else |
3491 rtn = PyUnicode_FromFormat("<%s object at %p>", | 3551 rtn = PyUnicode_FromFormat("<%s object at %p>", |
3492 type->tp_name, self); | 3552 type->tp_name, self); |
3493 Py_XDECREF(mod); | 3553 Py_XDECREF(mod); |
3494 Py_DECREF(name); | 3554 Py_DECREF(name); |
3495 return rtn; | 3555 return rtn; |
3496 } | 3556 } |
3497 | 3557 |
3498 static PyObject * | 3558 static PyObject * |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3762 PyInterpreterState *interp = PyThreadState_GET()->interp; | 3822 PyInterpreterState *interp = PyThreadState_GET()->interp; |
3763 _Py_IDENTIFIER(copyreg); | 3823 _Py_IDENTIFIER(copyreg); |
3764 | 3824 |
3765 copyreg_str = _PyUnicode_FromId(&PyId_copyreg); | 3825 copyreg_str = _PyUnicode_FromId(&PyId_copyreg); |
3766 if (copyreg_str == NULL) { | 3826 if (copyreg_str == NULL) { |
3767 return NULL; | 3827 return NULL; |
3768 } | 3828 } |
3769 /* Try to fetch cached copy of copyreg from sys.modules first in an | 3829 /* Try to fetch cached copy of copyreg from sys.modules first in an |
3770 attempt to avoid the import overhead. Previously this was implemented | 3830 attempt to avoid the import overhead. Previously this was implemented |
3771 by storing a reference to the cached module in a static variable, but | 3831 by storing a reference to the cached module in a static variable, but |
3772 this broke when multiple embeded interpreters were in use (see issue | 3832 this broke when multiple embedded interpreters were in use (see issue |
3773 #17408 and #19088). */ | 3833 #17408 and #19088). */ |
3774 copyreg_module = PyDict_GetItemWithError(interp->modules, copyreg_str); | 3834 copyreg_module = PyDict_GetItemWithError(interp->modules, copyreg_str); |
3775 if (copyreg_module != NULL) { | 3835 if (copyreg_module != NULL) { |
3776 Py_INCREF(copyreg_module); | 3836 Py_INCREF(copyreg_module); |
3777 return copyreg_module; | 3837 return copyreg_module; |
3778 } | 3838 } |
3779 if (PyErr_Occurred()) { | 3839 if (PyErr_Occurred()) { |
3780 return NULL; | 3840 return NULL; |
3781 } | 3841 } |
3782 return PyImport_Import(copyreg_str); | 3842 return PyImport_Import(copyreg_str); |
3783 } | 3843 } |
3784 | 3844 |
3785 Py_LOCAL(PyObject *) | 3845 static PyObject * |
3786 _PyType_GetSlotNames(PyTypeObject *cls) | 3846 _PyType_GetSlotNames(PyTypeObject *cls) |
3787 { | 3847 { |
3788 PyObject *copyreg; | 3848 PyObject *copyreg; |
3789 PyObject *slotnames; | 3849 PyObject *slotnames; |
3790 _Py_IDENTIFIER(__slotnames__); | 3850 _Py_IDENTIFIER(__slotnames__); |
3791 _Py_IDENTIFIER(_slotnames); | 3851 _Py_IDENTIFIER(_slotnames); |
3792 | 3852 |
3793 assert(PyType_Check(cls)); | 3853 assert(PyType_Check(cls)); |
3794 | 3854 |
3795 /* Get the slot names from the cache in the class if possible. */ | 3855 /* Get the slot names from the cache in the class if possible. */ |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3828 if (slotnames != Py_None && !PyList_Check(slotnames)) { | 3888 if (slotnames != Py_None && !PyList_Check(slotnames)) { |
3829 PyErr_SetString(PyExc_TypeError, | 3889 PyErr_SetString(PyExc_TypeError, |
3830 "copyreg._slotnames didn't return a list or None"); | 3890 "copyreg._slotnames didn't return a list or None"); |
3831 Py_DECREF(slotnames); | 3891 Py_DECREF(slotnames); |
3832 return NULL; | 3892 return NULL; |
3833 } | 3893 } |
3834 | 3894 |
3835 return slotnames; | 3895 return slotnames; |
3836 } | 3896 } |
3837 | 3897 |
3838 Py_LOCAL(PyObject *) | 3898 static PyObject * |
3839 _PyObject_GetState(PyObject *obj, int required) | 3899 _PyObject_GetState(PyObject *obj, int required) |
3840 { | 3900 { |
3841 PyObject *state; | 3901 PyObject *state; |
3842 PyObject *getstate; | 3902 PyObject *getstate; |
3843 _Py_IDENTIFIER(__getstate__); | 3903 _Py_IDENTIFIER(__getstate__); |
3844 | 3904 |
3845 getstate = _PyObject_GetAttrId(obj, &PyId___getstate__); | 3905 getstate = _PyObject_GetAttrId(obj, &PyId___getstate__); |
3846 if (getstate == NULL) { | 3906 if (getstate == NULL) { |
3847 PyObject *slotnames; | 3907 PyObject *slotnames; |
3848 | 3908 |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3974 else { /* getstate != NULL */ | 4034 else { /* getstate != NULL */ |
3975 state = PyObject_CallObject(getstate, NULL); | 4035 state = PyObject_CallObject(getstate, NULL); |
3976 Py_DECREF(getstate); | 4036 Py_DECREF(getstate); |
3977 if (state == NULL) | 4037 if (state == NULL) |
3978 return NULL; | 4038 return NULL; |
3979 } | 4039 } |
3980 | 4040 |
3981 return state; | 4041 return state; |
3982 } | 4042 } |
3983 | 4043 |
3984 Py_LOCAL(int) | 4044 static int |
3985 _PyObject_GetNewArguments(PyObject *obj, PyObject **args, PyObject **kwargs) | 4045 _PyObject_GetNewArguments(PyObject *obj, PyObject **args, PyObject **kwargs) |
3986 { | 4046 { |
3987 PyObject *getnewargs, *getnewargs_ex; | 4047 PyObject *getnewargs, *getnewargs_ex; |
3988 _Py_IDENTIFIER(__getnewargs_ex__); | 4048 _Py_IDENTIFIER(__getnewargs_ex__); |
3989 _Py_IDENTIFIER(__getnewargs__); | 4049 _Py_IDENTIFIER(__getnewargs__); |
3990 | 4050 |
3991 if (args == NULL || kwargs == NULL) { | 4051 if (args == NULL || kwargs == NULL) { |
3992 PyErr_BadInternalCall(); | 4052 PyErr_BadInternalCall(); |
3993 return -1; | 4053 return -1; |
3994 } | 4054 } |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4070 | 4130 |
4071 /* The object does not have __getnewargs_ex__ and __getnewargs__. This may | 4131 /* The object does not have __getnewargs_ex__ and __getnewargs__. This may |
4072 mean __new__ does not takes any arguments on this object, or that the | 4132 mean __new__ does not takes any arguments on this object, or that the |
4073 object does not implement the reduce protocol for pickling or | 4133 object does not implement the reduce protocol for pickling or |
4074 copying. */ | 4134 copying. */ |
4075 *args = NULL; | 4135 *args = NULL; |
4076 *kwargs = NULL; | 4136 *kwargs = NULL; |
4077 return 0; | 4137 return 0; |
4078 } | 4138 } |
4079 | 4139 |
4080 Py_LOCAL(int) | 4140 static int |
4081 _PyObject_GetItemsIter(PyObject *obj, PyObject **listitems, | 4141 _PyObject_GetItemsIter(PyObject *obj, PyObject **listitems, |
4082 PyObject **dictitems) | 4142 PyObject **dictitems) |
4083 { | 4143 { |
4084 if (listitems == NULL || dictitems == NULL) { | 4144 if (listitems == NULL || dictitems == NULL) { |
4085 PyErr_BadInternalCall(); | 4145 PyErr_BadInternalCall(); |
4086 return -1; | 4146 return -1; |
4087 } | 4147 } |
4088 | 4148 |
4089 if (!PyList_Check(obj)) { | 4149 if (!PyList_Check(obj)) { |
4090 *listitems = Py_None; | 4150 *listitems = Py_None; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4169 cls = (PyObject *) Py_TYPE(obj); | 4229 cls = (PyObject *) Py_TYPE(obj); |
4170 Py_INCREF(cls); | 4230 Py_INCREF(cls); |
4171 PyTuple_SET_ITEM(newargs, 0, cls); | 4231 PyTuple_SET_ITEM(newargs, 0, cls); |
4172 for (i = 0; i < n; i++) { | 4232 for (i = 0; i < n; i++) { |
4173 PyObject *v = PyTuple_GET_ITEM(args, i); | 4233 PyObject *v = PyTuple_GET_ITEM(args, i); |
4174 Py_INCREF(v); | 4234 Py_INCREF(v); |
4175 PyTuple_SET_ITEM(newargs, i+1, v); | 4235 PyTuple_SET_ITEM(newargs, i+1, v); |
4176 } | 4236 } |
4177 Py_XDECREF(args); | 4237 Py_XDECREF(args); |
4178 } | 4238 } |
4179 else { | 4239 else if (args != NULL) { |
4180 _Py_IDENTIFIER(__newobj_ex__); | 4240 _Py_IDENTIFIER(__newobj_ex__); |
4181 | 4241 |
4182 newobj = _PyObject_GetAttrId(copyreg, &PyId___newobj_ex__); | 4242 newobj = _PyObject_GetAttrId(copyreg, &PyId___newobj_ex__); |
4183 Py_DECREF(copyreg); | 4243 Py_DECREF(copyreg); |
4184 if (newobj == NULL) { | 4244 if (newobj == NULL) { |
4185 Py_DECREF(args); | 4245 Py_DECREF(args); |
4186 Py_DECREF(kwargs); | 4246 Py_DECREF(kwargs); |
4187 return NULL; | 4247 return NULL; |
4188 } | 4248 } |
4189 newargs = PyTuple_Pack(3, Py_TYPE(obj), args, kwargs); | 4249 newargs = PyTuple_Pack(3, Py_TYPE(obj), args, kwargs); |
4190 Py_DECREF(args); | 4250 Py_DECREF(args); |
4191 Py_DECREF(kwargs); | 4251 Py_DECREF(kwargs); |
4192 if (newargs == NULL) { | 4252 if (newargs == NULL) { |
4193 Py_DECREF(newobj); | 4253 Py_DECREF(newobj); |
4194 return NULL; | 4254 return NULL; |
4195 } | 4255 } |
| 4256 } |
| 4257 else { |
| 4258 /* args == NULL */ |
| 4259 Py_DECREF(kwargs); |
| 4260 PyErr_BadInternalCall(); |
| 4261 return NULL; |
4196 } | 4262 } |
4197 | 4263 |
4198 state = _PyObject_GetState(obj, | 4264 state = _PyObject_GetState(obj, |
4199 !hasargs && !PyList_Check(obj) && !PyDict_Check(obj)); | 4265 !hasargs && !PyList_Check(obj) && !PyDict_Check(obj)); |
4200 if (state == NULL) { | 4266 if (state == NULL) { |
4201 Py_DECREF(newobj); | 4267 Py_DECREF(newobj); |
4202 Py_DECREF(newargs); | 4268 Py_DECREF(newargs); |
4203 return NULL; | 4269 return NULL; |
4204 } | 4270 } |
4205 if (_PyObject_GetItemsIter(obj, &listitems, &dictitems) < 0) { | 4271 if (_PyObject_GetItemsIter(obj, &listitems, &dictitems) < 0) { |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4314 } | 4380 } |
4315 | 4381 |
4316 PyDoc_STRVAR(object_subclasshook_doc, | 4382 PyDoc_STRVAR(object_subclasshook_doc, |
4317 "Abstract classes can override this to customize issubclass().\n" | 4383 "Abstract classes can override this to customize issubclass().\n" |
4318 "\n" | 4384 "\n" |
4319 "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n" | 4385 "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n" |
4320 "It should return True, False or NotImplemented. If it returns\n" | 4386 "It should return True, False or NotImplemented. If it returns\n" |
4321 "NotImplemented, the normal algorithm is used. Otherwise, it\n" | 4387 "NotImplemented, the normal algorithm is used. Otherwise, it\n" |
4322 "overrides the normal algorithm (and the outcome is cached).\n"); | 4388 "overrides the normal algorithm (and the outcome is cached).\n"); |
4323 | 4389 |
4324 /* | 4390 static PyObject * |
4325 from PEP 3101, this code implements: | 4391 object_init_subclass(PyObject *cls, PyObject *arg) |
4326 | 4392 { |
4327 class object: | 4393 Py_RETURN_NONE; |
4328 def __format__(self, format_spec): | 4394 } |
4329 return format(str(self), format_spec) | 4395 |
4330 */ | 4396 PyDoc_STRVAR(object_init_subclass_doc, |
| 4397 "This method is called when a class is subclassed.\n" |
| 4398 "\n" |
| 4399 "The default implementation does nothing. It may be\n" |
| 4400 "overridden to extend subclasses.\n"); |
| 4401 |
4331 static PyObject * | 4402 static PyObject * |
4332 object_format(PyObject *self, PyObject *args) | 4403 object_format(PyObject *self, PyObject *args) |
4333 { | 4404 { |
4334 PyObject *format_spec; | 4405 PyObject *format_spec; |
4335 PyObject *self_as_str = NULL; | 4406 PyObject *self_as_str = NULL; |
4336 PyObject *result = NULL; | 4407 PyObject *result = NULL; |
4337 | 4408 |
4338 if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) | 4409 if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
4339 return NULL; | 4410 return NULL; |
4340 | 4411 |
| 4412 /* Issue 7994: If we're converting to a string, we |
| 4413 should reject format specifications */ |
| 4414 if (PyUnicode_GET_LENGTH(format_spec) > 0) { |
| 4415 PyErr_Format(PyExc_TypeError, |
| 4416 "unsupported format string passed to %.200s.__format__", |
| 4417 self->ob_type->tp_name); |
| 4418 return NULL; |
| 4419 } |
4341 self_as_str = PyObject_Str(self); | 4420 self_as_str = PyObject_Str(self); |
4342 if (self_as_str != NULL) { | 4421 if (self_as_str != NULL) { |
4343 /* Issue 7994: If we're converting to a string, we | |
4344 should reject format specifications */ | |
4345 if (PyUnicode_GET_LENGTH(format_spec) > 0) { | |
4346 PyErr_SetString(PyExc_TypeError, | |
4347 "non-empty format string passed to object.__format__"); | |
4348 goto done; | |
4349 } | |
4350 | |
4351 result = PyObject_Format(self_as_str, format_spec); | 4422 result = PyObject_Format(self_as_str, format_spec); |
4352 } | 4423 Py_DECREF(self_as_str); |
4353 | 4424 } |
4354 done: | |
4355 Py_XDECREF(self_as_str); | |
4356 | |
4357 return result; | 4425 return result; |
4358 } | 4426 } |
4359 | 4427 |
4360 static PyObject * | 4428 static PyObject * |
4361 object_sizeof(PyObject *self, PyObject *args) | 4429 object_sizeof(PyObject *self, PyObject *args) |
4362 { | 4430 { |
4363 Py_ssize_t res, isize; | 4431 Py_ssize_t res, isize; |
4364 | 4432 |
4365 res = 0; | 4433 res = 0; |
4366 isize = self->ob_type->tp_itemsize; | 4434 isize = self->ob_type->tp_itemsize; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4418 return result; | 4486 return result; |
4419 } | 4487 } |
4420 | 4488 |
4421 static PyMethodDef object_methods[] = { | 4489 static PyMethodDef object_methods[] = { |
4422 {"__reduce_ex__", object_reduce_ex, METH_VARARGS, | 4490 {"__reduce_ex__", object_reduce_ex, METH_VARARGS, |
4423 PyDoc_STR("helper for pickle")}, | 4491 PyDoc_STR("helper for pickle")}, |
4424 {"__reduce__", object_reduce, METH_VARARGS, | 4492 {"__reduce__", object_reduce, METH_VARARGS, |
4425 PyDoc_STR("helper for pickle")}, | 4493 PyDoc_STR("helper for pickle")}, |
4426 {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS, | 4494 {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS, |
4427 object_subclasshook_doc}, | 4495 object_subclasshook_doc}, |
| 4496 {"__init_subclass__", object_init_subclass, METH_CLASS | METH_NOARGS, |
| 4497 object_init_subclass_doc}, |
4428 {"__format__", object_format, METH_VARARGS, | 4498 {"__format__", object_format, METH_VARARGS, |
4429 PyDoc_STR("default object formatter")}, | 4499 PyDoc_STR("default object formatter")}, |
4430 {"__sizeof__", object_sizeof, METH_NOARGS, | 4500 {"__sizeof__", object_sizeof, METH_NOARGS, |
4431 PyDoc_STR("__sizeof__() -> int\nsize of object in memory, in bytes")}, | 4501 PyDoc_STR("__sizeof__() -> int\nsize of object in memory, in bytes")}, |
4432 {"__dir__", object_dir, METH_NOARGS, | 4502 {"__dir__", object_dir, METH_NOARGS, |
4433 PyDoc_STR("__dir__() -> list\ndefault dir() implementation")}, | 4503 PyDoc_STR("__dir__() -> list\ndefault dir() implementation")}, |
4434 {0} | 4504 {0} |
4435 }; | 4505 }; |
4436 | 4506 |
4437 | 4507 |
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4830 | 4900 |
4831 #ifdef Py_TRACE_REFS | 4901 #ifdef Py_TRACE_REFS |
4832 /* PyType_Ready is the closest thing we have to a choke point | 4902 /* PyType_Ready is the closest thing we have to a choke point |
4833 * for type objects, so is the best place I can think of to try | 4903 * for type objects, so is the best place I can think of to try |
4834 * to get type objects into the doubly-linked list of all objects. | 4904 * to get type objects into the doubly-linked list of all objects. |
4835 * Still, not all type objects go thru PyType_Ready. | 4905 * Still, not all type objects go thru PyType_Ready. |
4836 */ | 4906 */ |
4837 _Py_AddToAllObjects((PyObject *)type, 0); | 4907 _Py_AddToAllObjects((PyObject *)type, 0); |
4838 #endif | 4908 #endif |
4839 | 4909 |
| 4910 if (type->tp_name == NULL) { |
| 4911 PyErr_Format(PyExc_SystemError, |
| 4912 "Type does not define the tp_name field."); |
| 4913 goto error; |
| 4914 } |
| 4915 |
4840 /* Initialize tp_base (defaults to BaseObject unless that's us) */ | 4916 /* Initialize tp_base (defaults to BaseObject unless that's us) */ |
4841 base = type->tp_base; | 4917 base = type->tp_base; |
4842 if (base == NULL && type != &PyBaseObject_Type) { | 4918 if (base == NULL && type != &PyBaseObject_Type) { |
4843 base = type->tp_base = &PyBaseObject_Type; | 4919 base = type->tp_base = &PyBaseObject_Type; |
4844 Py_INCREF(base); | 4920 Py_INCREF(base); |
4845 } | 4921 } |
4846 | 4922 |
4847 /* Now the only way base can still be NULL is if type is | 4923 /* Now the only way base can still be NULL is if type is |
4848 * &PyBaseObject_Type. | 4924 * &PyBaseObject_Type. |
4849 */ | 4925 */ |
(...skipping 800 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5650 } | 5726 } |
5651 | 5727 |
5652 /* Slot wrappers that call the corresponding __foo__ slot. See comments | 5728 /* Slot wrappers that call the corresponding __foo__ slot. See comments |
5653 below at override_slots() for more explanation. */ | 5729 below at override_slots() for more explanation. */ |
5654 | 5730 |
5655 #define SLOT0(FUNCNAME, OPSTR) \ | 5731 #define SLOT0(FUNCNAME, OPSTR) \ |
5656 static PyObject * \ | 5732 static PyObject * \ |
5657 FUNCNAME(PyObject *self) \ | 5733 FUNCNAME(PyObject *self) \ |
5658 { \ | 5734 { \ |
5659 _Py_static_string(id, OPSTR); \ | 5735 _Py_static_string(id, OPSTR); \ |
5660 return call_method(self, &id, "()"); \ | 5736 return call_method(self, &id, NULL); \ |
5661 } | 5737 } |
5662 | 5738 |
5663 #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \ | 5739 #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \ |
5664 static PyObject * \ | 5740 static PyObject * \ |
5665 FUNCNAME(PyObject *self, ARG1TYPE arg1) \ | 5741 FUNCNAME(PyObject *self, ARG1TYPE arg1) \ |
5666 { \ | 5742 { \ |
5667 _Py_static_string(id, OPSTR); \ | 5743 _Py_static_string(id, OPSTR); \ |
5668 return call_method(self, &id, "(" ARGCODES ")", arg1); \ | 5744 return call_method(self, &id, "(" ARGCODES ")", arg1); \ |
5669 } | 5745 } |
5670 | 5746 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5743 static PyObject * \ | 5819 static PyObject * \ |
5744 FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \ | 5820 FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \ |
5745 { \ | 5821 { \ |
5746 _Py_static_string(id, #OPSTR); \ | 5822 _Py_static_string(id, #OPSTR); \ |
5747 return call_method(self, &id, "(" ARGCODES ")", arg1, arg2); \ | 5823 return call_method(self, &id, "(" ARGCODES ")", arg1, arg2); \ |
5748 } | 5824 } |
5749 | 5825 |
5750 static Py_ssize_t | 5826 static Py_ssize_t |
5751 slot_sq_length(PyObject *self) | 5827 slot_sq_length(PyObject *self) |
5752 { | 5828 { |
5753 PyObject *res = call_method(self, &PyId___len__, "()"); | 5829 PyObject *res = call_method(self, &PyId___len__, NULL); |
5754 Py_ssize_t len; | 5830 Py_ssize_t len; |
5755 | 5831 |
5756 if (res == NULL) | 5832 if (res == NULL) |
5757 return -1; | 5833 return -1; |
5758 len = PyNumber_AsSsize_t(res, PyExc_OverflowError); | 5834 len = PyNumber_AsSsize_t(res, PyExc_OverflowError); |
5759 Py_DECREF(res); | 5835 Py_DECREF(res); |
5760 if (len < 0) { | 5836 if (len < 0) { |
5761 if (!PyErr_Occurred()) | 5837 if (!PyErr_Occurred()) |
5762 PyErr_SetString(PyExc_ValueError, | 5838 PyErr_SetString(PyExc_ValueError, |
5763 "__len__() should return >= 0"); | 5839 "__len__() should return >= 0"); |
5764 return -1; | 5840 return -1; |
5765 } | 5841 } |
5766 return len; | 5842 return len; |
5767 } | 5843 } |
5768 | 5844 |
5769 /* Super-optimized version of slot_sq_item. | 5845 /* Super-optimized version of slot_sq_item. |
5770 Other slots could do the same... */ | 5846 Other slots could do the same... */ |
5771 static PyObject * | 5847 static PyObject * |
5772 slot_sq_item(PyObject *self, Py_ssize_t i) | 5848 slot_sq_item(PyObject *self, Py_ssize_t i) |
5773 { | 5849 { |
5774 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL; | 5850 PyObject *func, *ival = NULL, *retval = NULL; |
5775 descrgetfunc f; | 5851 descrgetfunc f; |
5776 | 5852 |
5777 func = _PyType_LookupId(Py_TYPE(self), &PyId___getitem__); | 5853 func = _PyType_LookupId(Py_TYPE(self), &PyId___getitem__); |
5778 if (func != NULL) { | 5854 if (func == NULL) { |
5779 if ((f = Py_TYPE(func)->tp_descr_get) == NULL) | |
5780 Py_INCREF(func); | |
5781 else { | |
5782 func = f(func, self, (PyObject *)(Py_TYPE(self))); | |
5783 if (func == NULL) { | |
5784 return NULL; | |
5785 } | |
5786 } | |
5787 ival = PyLong_FromSsize_t(i); | |
5788 if (ival != NULL) { | |
5789 args = PyTuple_New(1); | |
5790 if (args != NULL) { | |
5791 PyTuple_SET_ITEM(args, 0, ival); | |
5792 retval = PyObject_Call(func, args, NULL); | |
5793 Py_DECREF(args); | |
5794 Py_DECREF(func); | |
5795 return retval; | |
5796 } | |
5797 } | |
5798 } | |
5799 else { | |
5800 PyObject *getitem_str = _PyUnicode_FromId(&PyId___getitem__); | 5855 PyObject *getitem_str = _PyUnicode_FromId(&PyId___getitem__); |
5801 PyErr_SetObject(PyExc_AttributeError, getitem_str); | 5856 PyErr_SetObject(PyExc_AttributeError, getitem_str); |
5802 } | 5857 return NULL; |
5803 Py_XDECREF(args); | 5858 } |
5804 Py_XDECREF(ival); | 5859 |
5805 Py_XDECREF(func); | 5860 f = Py_TYPE(func)->tp_descr_get; |
| 5861 if (f == NULL) { |
| 5862 Py_INCREF(func); |
| 5863 } |
| 5864 else { |
| 5865 func = f(func, self, (PyObject *)(Py_TYPE(self))); |
| 5866 if (func == NULL) { |
| 5867 return NULL; |
| 5868 } |
| 5869 } |
| 5870 |
| 5871 ival = PyLong_FromSsize_t(i); |
| 5872 if (ival == NULL) { |
| 5873 goto error; |
| 5874 } |
| 5875 |
| 5876 retval = _PyObject_CallArg1(func, ival); |
| 5877 Py_DECREF(func); |
| 5878 Py_DECREF(ival); |
| 5879 return retval; |
| 5880 |
| 5881 error: |
| 5882 Py_DECREF(func); |
5806 return NULL; | 5883 return NULL; |
5807 } | 5884 } |
5808 | 5885 |
5809 static int | 5886 static int |
5810 slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value) | 5887 slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value) |
5811 { | 5888 { |
5812 PyObject *res; | 5889 PyObject *res; |
5813 | 5890 |
5814 if (value == NULL) | 5891 if (value == NULL) |
5815 res = call_method(self, &PyId___delitem__, "(n)", index); | 5892 res = call_method(self, &PyId___delitem__, "(n)", index); |
5816 else | 5893 else |
5817 res = call_method(self, &PyId___setitem__, "(nO)", index, value); | 5894 res = call_method(self, &PyId___setitem__, "(nO)", index, value); |
5818 if (res == NULL) | 5895 if (res == NULL) |
5819 return -1; | 5896 return -1; |
5820 Py_DECREF(res); | 5897 Py_DECREF(res); |
5821 return 0; | 5898 return 0; |
5822 } | 5899 } |
5823 | 5900 |
5824 static int | 5901 static int |
5825 slot_sq_contains(PyObject *self, PyObject *value) | 5902 slot_sq_contains(PyObject *self, PyObject *value) |
5826 { | 5903 { |
5827 PyObject *func, *res, *args; | 5904 PyObject *func, *res; |
5828 int result = -1; | 5905 int result = -1; |
5829 _Py_IDENTIFIER(__contains__); | 5906 _Py_IDENTIFIER(__contains__); |
5830 | 5907 |
5831 func = lookup_maybe(self, &PyId___contains__); | 5908 func = lookup_maybe(self, &PyId___contains__); |
| 5909 if (func == Py_None) { |
| 5910 Py_DECREF(func); |
| 5911 PyErr_Format(PyExc_TypeError, |
| 5912 "'%.200s' object is not a container", |
| 5913 Py_TYPE(self)->tp_name); |
| 5914 return -1; |
| 5915 } |
5832 if (func != NULL) { | 5916 if (func != NULL) { |
5833 args = PyTuple_Pack(1, value); | 5917 res = _PyObject_CallArg1(func, value); |
5834 if (args == NULL) | |
5835 res = NULL; | |
5836 else { | |
5837 res = PyObject_Call(func, args, NULL); | |
5838 Py_DECREF(args); | |
5839 } | |
5840 Py_DECREF(func); | 5918 Py_DECREF(func); |
5841 if (res != NULL) { | 5919 if (res != NULL) { |
5842 result = PyObject_IsTrue(res); | 5920 result = PyObject_IsTrue(res); |
5843 Py_DECREF(res); | 5921 Py_DECREF(res); |
5844 } | 5922 } |
5845 } | 5923 } |
5846 else if (! PyErr_Occurred()) { | 5924 else if (! PyErr_Occurred()) { |
5847 /* Possible results: -1 and 1 */ | 5925 /* Possible results: -1 and 1 */ |
5848 result = (int)_PySequence_IterSearch(self, value, | 5926 result = (int)_PySequence_IterSearch(self, value, |
5849 PY_ITERSEARCH_CONTAINS); | 5927 PY_ITERSEARCH_CONTAINS); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5900 Py_RETURN_NOTIMPLEMENTED; | 5978 Py_RETURN_NOTIMPLEMENTED; |
5901 } | 5979 } |
5902 | 5980 |
5903 SLOT0(slot_nb_negative, "__neg__") | 5981 SLOT0(slot_nb_negative, "__neg__") |
5904 SLOT0(slot_nb_positive, "__pos__") | 5982 SLOT0(slot_nb_positive, "__pos__") |
5905 SLOT0(slot_nb_absolute, "__abs__") | 5983 SLOT0(slot_nb_absolute, "__abs__") |
5906 | 5984 |
5907 static int | 5985 static int |
5908 slot_nb_bool(PyObject *self) | 5986 slot_nb_bool(PyObject *self) |
5909 { | 5987 { |
5910 PyObject *func, *args; | 5988 PyObject *func, *value; |
5911 int result = -1; | 5989 int result; |
5912 int using_len = 0; | 5990 int using_len = 0; |
5913 _Py_IDENTIFIER(__bool__); | 5991 _Py_IDENTIFIER(__bool__); |
5914 | 5992 |
5915 func = lookup_maybe(self, &PyId___bool__); | 5993 func = lookup_maybe(self, &PyId___bool__); |
5916 if (func == NULL) { | 5994 if (func == NULL) { |
5917 if (PyErr_Occurred()) | 5995 if (PyErr_Occurred()) { |
5918 return -1; | 5996 return -1; |
| 5997 } |
| 5998 |
5919 func = lookup_maybe(self, &PyId___len__); | 5999 func = lookup_maybe(self, &PyId___len__); |
5920 if (func == NULL) | 6000 if (func == NULL) { |
5921 return PyErr_Occurred() ? -1 : 1; | 6001 if (PyErr_Occurred()) { |
| 6002 return -1; |
| 6003 } |
| 6004 return 1; |
| 6005 } |
5922 using_len = 1; | 6006 using_len = 1; |
5923 } | 6007 } |
5924 args = PyTuple_New(0); | 6008 |
5925 if (args != NULL) { | 6009 value = _PyObject_CallNoArg(func); |
5926 PyObject *temp = PyObject_Call(func, args, NULL); | 6010 if (value == NULL) { |
5927 Py_DECREF(args); | 6011 goto error; |
5928 if (temp != NULL) { | 6012 } |
5929 if (using_len) { | 6013 |
5930 /* enforced by slot_nb_len */ | 6014 if (using_len) { |
5931 result = PyObject_IsTrue(temp); | 6015 /* bool type enforced by slot_nb_len */ |
5932 } | 6016 result = PyObject_IsTrue(value); |
5933 else if (PyBool_Check(temp)) { | 6017 } |
5934 result = PyObject_IsTrue(temp); | 6018 else if (PyBool_Check(value)) { |
5935 } | 6019 result = PyObject_IsTrue(value); |
5936 else { | 6020 } |
5937 PyErr_Format(PyExc_TypeError, | 6021 else { |
5938 "__bool__ should return " | 6022 PyErr_Format(PyExc_TypeError, |
5939 "bool, returned %s", | 6023 "__bool__ should return " |
5940 Py_TYPE(temp)->tp_name); | 6024 "bool, returned %s", |
5941 result = -1; | 6025 Py_TYPE(value)->tp_name); |
5942 } | 6026 result = -1; |
5943 Py_DECREF(temp); | 6027 } |
5944 } | 6028 |
5945 } | 6029 Py_DECREF(value); |
5946 Py_DECREF(func); | 6030 Py_DECREF(func); |
5947 return result; | 6031 return result; |
| 6032 |
| 6033 error: |
| 6034 Py_DECREF(func); |
| 6035 return -1; |
5948 } | 6036 } |
5949 | 6037 |
5950 | 6038 |
5951 static PyObject * | 6039 static PyObject * |
5952 slot_nb_index(PyObject *self) | 6040 slot_nb_index(PyObject *self) |
5953 { | 6041 { |
5954 _Py_IDENTIFIER(__index__); | 6042 _Py_IDENTIFIER(__index__); |
5955 return call_method(self, &PyId___index__, "()"); | 6043 return call_method(self, &PyId___index__, NULL); |
5956 } | 6044 } |
5957 | 6045 |
5958 | 6046 |
5959 SLOT0(slot_nb_invert, "__invert__") | 6047 SLOT0(slot_nb_invert, "__invert__") |
5960 SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__") | 6048 SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__") |
5961 SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__") | 6049 SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__") |
5962 SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__") | 6050 SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__") |
5963 SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__") | 6051 SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__") |
5964 SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__") | 6052 SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__") |
5965 | 6053 |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6182 {0, "__le__", 0}, | 6270 {0, "__le__", 0}, |
6183 {0, "__eq__", 0}, | 6271 {0, "__eq__", 0}, |
6184 {0, "__ne__", 0}, | 6272 {0, "__ne__", 0}, |
6185 {0, "__gt__", 0}, | 6273 {0, "__gt__", 0}, |
6186 {0, "__ge__", 0} | 6274 {0, "__ge__", 0} |
6187 }; | 6275 }; |
6188 | 6276 |
6189 static PyObject * | 6277 static PyObject * |
6190 slot_tp_richcompare(PyObject *self, PyObject *other, int op) | 6278 slot_tp_richcompare(PyObject *self, PyObject *other, int op) |
6191 { | 6279 { |
6192 PyObject *func, *args, *res; | 6280 PyObject *func, *res; |
6193 | 6281 |
6194 func = lookup_method(self, &name_op[op]); | 6282 func = lookup_method(self, &name_op[op]); |
6195 if (func == NULL) { | 6283 if (func == NULL) { |
6196 PyErr_Clear(); | 6284 PyErr_Clear(); |
6197 Py_RETURN_NOTIMPLEMENTED; | 6285 Py_RETURN_NOTIMPLEMENTED; |
6198 } | 6286 } |
6199 args = PyTuple_Pack(1, other); | 6287 res = _PyObject_CallArg1(func, other); |
6200 if (args == NULL) | |
6201 res = NULL; | |
6202 else { | |
6203 res = PyObject_Call(func, args, NULL); | |
6204 Py_DECREF(args); | |
6205 } | |
6206 Py_DECREF(func); | 6288 Py_DECREF(func); |
6207 return res; | 6289 return res; |
6208 } | 6290 } |
6209 | 6291 |
6210 static PyObject * | 6292 static PyObject * |
6211 slot_tp_iter(PyObject *self) | 6293 slot_tp_iter(PyObject *self) |
6212 { | 6294 { |
6213 PyObject *func, *res; | 6295 PyObject *func, *res; |
6214 _Py_IDENTIFIER(__iter__); | 6296 _Py_IDENTIFIER(__iter__); |
6215 | 6297 |
6216 func = lookup_method(self, &PyId___iter__); | 6298 func = lookup_method(self, &PyId___iter__); |
| 6299 if (func == Py_None) { |
| 6300 Py_DECREF(func); |
| 6301 PyErr_Format(PyExc_TypeError, |
| 6302 "'%.200s' object is not iterable", |
| 6303 Py_TYPE(self)->tp_name); |
| 6304 return NULL; |
| 6305 } |
| 6306 |
6217 if (func != NULL) { | 6307 if (func != NULL) { |
6218 PyObject *args; | 6308 res = _PyObject_CallNoArg(func); |
6219 args = res = PyTuple_New(0); | |
6220 if (args != NULL) { | |
6221 res = PyObject_Call(func, args, NULL); | |
6222 Py_DECREF(args); | |
6223 } | |
6224 Py_DECREF(func); | 6309 Py_DECREF(func); |
6225 return res; | 6310 return res; |
6226 } | 6311 } |
| 6312 |
6227 PyErr_Clear(); | 6313 PyErr_Clear(); |
6228 func = lookup_method(self, &PyId___getitem__); | 6314 func = lookup_method(self, &PyId___getitem__); |
6229 if (func == NULL) { | 6315 if (func == NULL) { |
6230 PyErr_Format(PyExc_TypeError, | 6316 PyErr_Format(PyExc_TypeError, |
6231 "'%.200s' object is not iterable", | 6317 "'%.200s' object is not iterable", |
6232 Py_TYPE(self)->tp_name); | 6318 Py_TYPE(self)->tp_name); |
6233 return NULL; | 6319 return NULL; |
6234 } | 6320 } |
6235 Py_DECREF(func); | 6321 Py_DECREF(func); |
6236 return PySeqIter_New(self); | 6322 return PySeqIter_New(self); |
6237 } | 6323 } |
6238 | 6324 |
6239 static PyObject * | 6325 static PyObject * |
6240 slot_tp_iternext(PyObject *self) | 6326 slot_tp_iternext(PyObject *self) |
6241 { | 6327 { |
6242 _Py_IDENTIFIER(__next__); | 6328 _Py_IDENTIFIER(__next__); |
6243 return call_method(self, &PyId___next__, "()"); | 6329 return call_method(self, &PyId___next__, NULL); |
6244 } | 6330 } |
6245 | 6331 |
6246 static PyObject * | 6332 static PyObject * |
6247 slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type) | 6333 slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
6248 { | 6334 { |
6249 PyTypeObject *tp = Py_TYPE(self); | 6335 PyTypeObject *tp = Py_TYPE(self); |
6250 PyObject *get; | 6336 PyObject *get; |
6251 _Py_IDENTIFIER(__get__); | 6337 _Py_IDENTIFIER(__get__); |
6252 | 6338 |
6253 get = _PyType_LookupId(tp, &PyId___get__); | 6339 get = _PyType_LookupId(tp, &PyId___get__); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6302 Py_DECREF(res); | 6388 Py_DECREF(res); |
6303 return -1; | 6389 return -1; |
6304 } | 6390 } |
6305 Py_DECREF(res); | 6391 Py_DECREF(res); |
6306 return 0; | 6392 return 0; |
6307 } | 6393 } |
6308 | 6394 |
6309 static PyObject * | 6395 static PyObject * |
6310 slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | 6396 slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
6311 { | 6397 { |
6312 PyObject *func; | 6398 PyObject *func, *result; |
6313 PyObject *newargs, *x; | |
6314 Py_ssize_t i, n; | |
6315 | 6399 |
6316 func = _PyObject_GetAttrId((PyObject *)type, &PyId___new__); | 6400 func = _PyObject_GetAttrId((PyObject *)type, &PyId___new__); |
6317 if (func == NULL) | 6401 if (func == NULL) { |
6318 return NULL; | 6402 return NULL; |
6319 assert(PyTuple_Check(args)); | 6403 } |
6320 n = PyTuple_GET_SIZE(args); | 6404 |
6321 newargs = PyTuple_New(n+1); | 6405 result = _PyObject_Call_Prepend(func, (PyObject *)type, args, kwds); |
6322 if (newargs == NULL) | |
6323 return NULL; | |
6324 Py_INCREF(type); | |
6325 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type); | |
6326 for (i = 0; i < n; i++) { | |
6327 x = PyTuple_GET_ITEM(args, i); | |
6328 Py_INCREF(x); | |
6329 PyTuple_SET_ITEM(newargs, i+1, x); | |
6330 } | |
6331 x = PyObject_Call(func, newargs, kwds); | |
6332 Py_DECREF(newargs); | |
6333 Py_DECREF(func); | 6406 Py_DECREF(func); |
6334 return x; | 6407 return result; |
6335 } | 6408 } |
6336 | 6409 |
6337 static void | 6410 static void |
6338 slot_tp_finalize(PyObject *self) | 6411 slot_tp_finalize(PyObject *self) |
6339 { | 6412 { |
6340 _Py_IDENTIFIER(__del__); | 6413 _Py_IDENTIFIER(__del__); |
6341 PyObject *del, *res; | 6414 PyObject *del, *res; |
6342 PyObject *error_type, *error_value, *error_traceback; | 6415 PyObject *error_type, *error_value, *error_traceback; |
6343 | 6416 |
6344 /* Save the current exception, if any. */ | 6417 /* Save the current exception, if any. */ |
(...skipping 582 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6927 { | 7000 { |
6928 slotdef *p; | 7001 slotdef *p; |
6929 | 7002 |
6930 init_slotdefs(); | 7003 init_slotdefs(); |
6931 for (p = slotdefs; p->name; p++) { | 7004 for (p = slotdefs; p->name; p++) { |
6932 /* update_slot returns int but can't actually fail */ | 7005 /* update_slot returns int but can't actually fail */ |
6933 update_slot(type, p->name_strobj); | 7006 update_slot(type, p->name_strobj); |
6934 } | 7007 } |
6935 } | 7008 } |
6936 | 7009 |
| 7010 /* Call __set_name__ on all descriptors in a newly generated type */ |
| 7011 static int |
| 7012 set_names(PyTypeObject *type) |
| 7013 { |
| 7014 PyObject *names_to_set, *key, *value, *set_name, *tmp; |
| 7015 Py_ssize_t i = 0; |
| 7016 |
| 7017 names_to_set = PyDict_Copy(type->tp_dict); |
| 7018 if (names_to_set == NULL) |
| 7019 return -1; |
| 7020 |
| 7021 while (PyDict_Next(names_to_set, &i, &key, &value)) { |
| 7022 set_name = lookup_maybe(value, &PyId___set_name__); |
| 7023 if (set_name != NULL) { |
| 7024 tmp = PyObject_CallFunctionObjArgs(set_name, type, key, NULL); |
| 7025 Py_DECREF(set_name); |
| 7026 if (tmp == NULL) { |
| 7027 _PyErr_FormatFromCause(PyExc_RuntimeError, |
| 7028 "Error calling __set_name__ on '%.100s' instance %R " |
| 7029 "in '%.100s'", |
| 7030 value->ob_type->tp_name, key, type->tp_name); |
| 7031 Py_DECREF(names_to_set); |
| 7032 return -1; |
| 7033 } |
| 7034 else |
| 7035 Py_DECREF(tmp); |
| 7036 } |
| 7037 else if (PyErr_Occurred()) { |
| 7038 Py_DECREF(names_to_set); |
| 7039 return -1; |
| 7040 } |
| 7041 } |
| 7042 |
| 7043 Py_DECREF(names_to_set); |
| 7044 return 0; |
| 7045 } |
| 7046 |
| 7047 /* Call __init_subclass__ on the parent of a newly generated type */ |
| 7048 static int |
| 7049 init_subclass(PyTypeObject *type, PyObject *kwds) |
| 7050 { |
| 7051 PyObject *super, *func, *result; |
| 7052 PyObject *args[2] = {(PyObject *)type, (PyObject *)type}; |
| 7053 |
| 7054 super = _PyObject_FastCall((PyObject *)&PySuper_Type, args, 2); |
| 7055 if (super == NULL) { |
| 7056 return -1; |
| 7057 } |
| 7058 |
| 7059 func = _PyObject_GetAttrId(super, &PyId___init_subclass__); |
| 7060 Py_DECREF(super); |
| 7061 if (func == NULL) { |
| 7062 return -1; |
| 7063 } |
| 7064 |
| 7065 |
| 7066 result = _PyObject_FastCallDict(func, NULL, 0, kwds); |
| 7067 Py_DECREF(func); |
| 7068 if (result == NULL) { |
| 7069 return -1; |
| 7070 } |
| 7071 |
| 7072 Py_DECREF(result); |
| 7073 return 0; |
| 7074 } |
| 7075 |
6937 /* recurse_down_subclasses() and update_subclasses() are mutually | 7076 /* recurse_down_subclasses() and update_subclasses() are mutually |
6938 recursive functions to call a callback for all subclasses, | 7077 recursive functions to call a callback for all subclasses, |
6939 but refraining from recursing into subclasses that define 'name'. */ | 7078 but refraining from recursing into subclasses that define 'name'. */ |
6940 | 7079 |
6941 static int | 7080 static int |
6942 update_subclasses(PyTypeObject *type, PyObject *name, | 7081 update_subclasses(PyTypeObject *type, PyObject *name, |
6943 update_callback callback, void *data) | 7082 update_callback callback, void *data) |
6944 { | 7083 { |
6945 if (callback(type, data) < 0) | 7084 if (callback(type, data) < 0) |
6946 return -1; | 7085 return -1; |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7107 Py_ssize_t i, n; | 7246 Py_ssize_t i, n; |
7108 | 7247 |
7109 starttype = su->obj_type; | 7248 starttype = su->obj_type; |
7110 if (starttype == NULL) | 7249 if (starttype == NULL) |
7111 goto skip; | 7250 goto skip; |
7112 | 7251 |
7113 /* We want __class__ to return the class of the super object | 7252 /* We want __class__ to return the class of the super object |
7114 (i.e. super, or a subclass), not the class of su->obj. */ | 7253 (i.e. super, or a subclass), not the class of su->obj. */ |
7115 if (PyUnicode_Check(name) && | 7254 if (PyUnicode_Check(name) && |
7116 PyUnicode_GET_LENGTH(name) == 9 && | 7255 PyUnicode_GET_LENGTH(name) == 9 && |
7117 _PyUnicode_CompareWithId(name, &PyId___class__) == 0) | 7256 _PyUnicode_EqualToASCIIId(name, &PyId___class__)) |
7118 goto skip; | 7257 goto skip; |
7119 | 7258 |
7120 mro = starttype->tp_mro; | 7259 mro = starttype->tp_mro; |
7121 if (mro == NULL) | 7260 if (mro == NULL) |
7122 goto skip; | 7261 goto skip; |
7123 | 7262 |
7124 assert(PyTuple_Check(mro)); | 7263 assert(PyTuple_Check(mro)); |
7125 n = PyTuple_GET_SIZE(mro); | 7264 n = PyTuple_GET_SIZE(mro); |
7126 | 7265 |
7127 /* No need to check the last one: it's gonna be skipped anyway. */ | 7266 /* No need to check the last one: it's gonna be skipped anyway. */ |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7319 } | 7458 } |
7320 if (co->co_freevars == NULL) | 7459 if (co->co_freevars == NULL) |
7321 n = 0; | 7460 n = 0; |
7322 else { | 7461 else { |
7323 assert(PyTuple_Check(co->co_freevars)); | 7462 assert(PyTuple_Check(co->co_freevars)); |
7324 n = PyTuple_GET_SIZE(co->co_freevars); | 7463 n = PyTuple_GET_SIZE(co->co_freevars); |
7325 } | 7464 } |
7326 for (i = 0; i < n; i++) { | 7465 for (i = 0; i < n; i++) { |
7327 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); | 7466 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
7328 assert(PyUnicode_Check(name)); | 7467 assert(PyUnicode_Check(name)); |
7329 if (!_PyUnicode_CompareWithId(name, &PyId___class__)) { | 7468 if (_PyUnicode_EqualToASCIIId(name, &PyId___class__)) { |
7330 Py_ssize_t index = co->co_nlocals + | 7469 Py_ssize_t index = co->co_nlocals + |
7331 PyTuple_GET_SIZE(co->co_cellvars) + i; | 7470 PyTuple_GET_SIZE(co->co_cellvars) + i; |
7332 PyObject *cell = f->f_localsplus[index]; | 7471 PyObject *cell = f->f_localsplus[index]; |
7333 if (cell == NULL || !PyCell_Check(cell)) { | 7472 if (cell == NULL || !PyCell_Check(cell)) { |
7334 PyErr_SetString(PyExc_RuntimeError, | 7473 PyErr_SetString(PyExc_RuntimeError, |
7335 "super(): bad __class__ cell"); | 7474 "super(): bad __class__ cell"); |
7336 return -1; | 7475 return -1; |
7337 } | 7476 } |
7338 type = (PyTypeObject *) PyCell_GET(cell); | 7477 type = (PyTypeObject *) PyCell_GET(cell); |
7339 if (type == NULL) { | 7478 if (type == NULL) { |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7435 0, /* tp_base */ | 7574 0, /* tp_base */ |
7436 0, /* tp_dict */ | 7575 0, /* tp_dict */ |
7437 super_descr_get, /* tp_descr_get */ | 7576 super_descr_get, /* tp_descr_get */ |
7438 0, /* tp_descr_set */ | 7577 0, /* tp_descr_set */ |
7439 0, /* tp_dictoffset */ | 7578 0, /* tp_dictoffset */ |
7440 super_init, /* tp_init */ | 7579 super_init, /* tp_init */ |
7441 PyType_GenericAlloc, /* tp_alloc */ | 7580 PyType_GenericAlloc, /* tp_alloc */ |
7442 PyType_GenericNew, /* tp_new */ | 7581 PyType_GenericNew, /* tp_new */ |
7443 PyObject_GC_Del, /* tp_free */ | 7582 PyObject_GC_Del, /* tp_free */ |
7444 }; | 7583 }; |
LEFT | RIGHT |