Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(24)

Delta Between Two Patch Sets: Objects/typeobject.c

Issue 25958: Implicit ABCs have no means of "anti-registration"
Left Patch Set: Created 4 years, 1 month ago
Right Patch Set: Created 3 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Objects/enumobject.c ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
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 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 /* for stress-testing: next_version_tag &= 0xFF; */ 311 /* for stress-testing: next_version_tag &= 0xFF; */
310 312
311 if (type->tp_version_tag == 0) { 313 if (type->tp_version_tag == 0) {
312 /* wrap-around or just starting Python - clear the whole 314 /* wrap-around or just starting Python - clear the whole
313 cache by filling names with references to Py_None. 315 cache by filling names with references to Py_None.
314 Values are also set to NULL for added protection, as they 316 Values are also set to NULL for added protection, as they
315 are borrowed reference */ 317 are borrowed reference */
316 for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) { 318 for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
317 method_cache[i].value = NULL; 319 method_cache[i].value = NULL;
318 Py_INCREF(Py_None); 320 Py_INCREF(Py_None);
319 Py_SETREF(method_cache[i].name, Py_None); 321 Py_XSETREF(method_cache[i].name, Py_None);
320 } 322 }
321 /* mark all version tags as invalid */ 323 /* mark all version tags as invalid */
322 PyType_Modified(&PyBaseObject_Type); 324 PyType_Modified(&PyBaseObject_Type);
323 return 1; 325 return 1;
324 } 326 }
325 bases = type->tp_bases; 327 bases = type->tp_bases;
326 n = PyTuple_GET_SIZE(bases); 328 n = PyTuple_GET_SIZE(bases);
327 for (i = 0; i < n; i++) { 329 for (i = 0; i < n; i++) {
328 PyObject *b = PyTuple_GET_ITEM(bases, i); 330 PyObject *b = PyTuple_GET_ITEM(bases, i);
329 assert(PyType_Check(b)); 331 assert(PyType_Check(b));
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
453 type_module(PyTypeObject *type, void *context) 455 type_module(PyTypeObject *type, void *context)
454 { 456 {
455 char *s; 457 char *s;
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 PyObject *mod = _PyDict_GetItemId(type->tp_dict, &PyId___module__);
459 if (!mod) { 461 if (!mod) {
460 PyErr_Format(PyExc_AttributeError, "__module__"); 462 PyErr_Format(PyExc_AttributeError, "__module__");
461 return 0; 463 return 0;
462 } 464 }
463 Py_XINCREF(mod); 465 Py_INCREF(mod);
464 return mod; 466 return mod;
465 } 467 }
466 else { 468 else {
467 PyObject *name; 469 PyObject *name;
468 s = strrchr(type->tp_name, '.'); 470 s = strrchr(type->tp_name, '.');
469 if (s != NULL) 471 if (s != NULL)
470 return PyUnicode_FromStringAndSize( 472 return PyUnicode_FromStringAndSize(
471 type->tp_name, (Py_ssize_t)(s - type->tp_name)); 473 type->tp_name, (Py_ssize_t)(s - type->tp_name));
472 name = _PyUnicode_FromId(&PyId_builtins); 474 name = _PyUnicode_FromId(&PyId_builtins);
473 Py_XINCREF(name); 475 Py_XINCREF(name);
(...skipping 19 matching lines...) Expand all
493 /* type itself has an __abstractmethods__ descriptor (this). Don't return 495 /* type itself has an __abstractmethods__ descriptor (this). Don't return
494 that. */ 496 that. */
495 if (type != &PyType_Type) 497 if (type != &PyType_Type)
496 mod = _PyDict_GetItemId(type->tp_dict, &PyId___abstractmethods__); 498 mod = _PyDict_GetItemId(type->tp_dict, &PyId___abstractmethods__);
497 if (!mod) { 499 if (!mod) {
498 PyObject *message = _PyUnicode_FromId(&PyId___abstractmethods__); 500 PyObject *message = _PyUnicode_FromId(&PyId___abstractmethods__);
499 if (message) 501 if (message)
500 PyErr_SetObject(PyExc_AttributeError, message); 502 PyErr_SetObject(PyExc_AttributeError, message);
501 return NULL; 503 return NULL;
502 } 504 }
503 Py_XINCREF(mod); 505 Py_INCREF(mod);
504 return mod; 506 return mod;
505 } 507 }
506 508
507 static int 509 static int
508 type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context) 510 type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context)
509 { 511 {
510 /* __abstractmethods__ should only be set once on a type, in 512 /* __abstractmethods__ should only be set once on a type, in
511 abc.ABCMeta.__new__, so this function doesn't do anything 513 abc.ABCMeta.__new__, so this function doesn't do anything
512 special to update subclasses. 514 special to update subclasses.
513 */ 515 */
(...skipping 981 matching lines...) Expand 10 before | Expand all | Expand 10 after
1495 David A. Moon, Keith Playford, and P. Tucker Withington. 1497 David A. Moon, Keith Playford, and P. Tucker Withington.
1496 (OOPSLA 1996) 1498 (OOPSLA 1996)
1497 1499
1498 Some notes about the rules implied by C3: 1500 Some notes about the rules implied by C3:
1499 1501
1500 No duplicate bases. 1502 No duplicate bases.
1501 It isn't legal to repeat a class in a list of base classes. 1503 It isn't legal to repeat a class in a list of base classes.
1502 1504
1503 The next three properties are the 3 constraints in "C3". 1505 The next three properties are the 3 constraints in "C3".
1504 1506
1505 Local precendece order. 1507 Local precedence order.
1506 If A precedes B in C's MRO, then A will precede B in the MRO of all 1508 If A precedes B in C's MRO, then A will precede B in the MRO of all
1507 subclasses of C. 1509 subclasses of C.
1508 1510
1509 Monotonicity. 1511 Monotonicity.
1510 The MRO of a class must be an extension without reordering of the 1512 The MRO of a class must be an extension without reordering of the
1511 MRO of each of its superclasses. 1513 MRO of each of its superclasses.
1512 1514
1513 Extended Precedence Graph (EPG). 1515 Extended Precedence Graph (EPG).
1514 Linearization is consistent if there is a path in the EPG from 1516 Linearization is consistent if there is a path in the EPG from
1515 each class to all its successors in the linearization. See 1517 each class to all its successors in the linearization. See
(...skipping 11 matching lines...) Expand all
1527 } 1529 }
1528 return 0; 1530 return 0;
1529 } 1531 }
1530 1532
1531 static PyObject * 1533 static PyObject *
1532 class_name(PyObject *cls) 1534 class_name(PyObject *cls)
1533 { 1535 {
1534 PyObject *name = _PyObject_GetAttrId(cls, &PyId___name__); 1536 PyObject *name = _PyObject_GetAttrId(cls, &PyId___name__);
1535 if (name == NULL) { 1537 if (name == NULL) {
1536 PyErr_Clear(); 1538 PyErr_Clear();
1537 Py_XDECREF(name);
1538 name = PyObject_Repr(cls); 1539 name = PyObject_Repr(cls);
1539 } 1540 }
1540 if (name == NULL) 1541 if (name == NULL)
1541 return NULL; 1542 return NULL;
1542 if (!PyUnicode_Check(name)) { 1543 if (!PyUnicode_Check(name)) {
1543 Py_DECREF(name); 1544 Py_DECREF(name);
1544 return NULL; 1545 return NULL;
1545 } 1546 }
1546 return name; 1547 return name;
1547 } 1548 }
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after
2021 if (extra_ivars(type, base)) 2022 if (extra_ivars(type, base))
2022 return type; 2023 return type;
2023 else 2024 else
2024 return base; 2025 return base;
2025 } 2026 }
2026 2027
2027 static void object_dealloc(PyObject *); 2028 static void object_dealloc(PyObject *);
2028 static int object_init(PyObject *, PyObject *, PyObject *); 2029 static int object_init(PyObject *, PyObject *, PyObject *);
2029 static int update_slot(PyTypeObject *, PyObject *); 2030 static int update_slot(PyTypeObject *, PyObject *);
2030 static void fixup_slot_dispatchers(PyTypeObject *); 2031 static void fixup_slot_dispatchers(PyTypeObject *);
2032 static int set_names(PyTypeObject *);
2033 static int init_subclass(PyTypeObject *, PyObject *);
2031 2034
2032 /* 2035 /*
2033 * Helpers for __dict__ descriptor. We don't want to expose the dicts 2036 * Helpers for __dict__ descriptor. We don't want to expose the dicts
2034 * inherited from various builtin types. The builtin base usually provides 2037 * inherited from various builtin types. The builtin base usually provides
2035 * its own __dict__ descriptor, so we use that when we can. 2038 * its own __dict__ descriptor, so we use that when we can.
2036 */ 2039 */
2037 static PyTypeObject * 2040 static PyTypeObject *
2038 get_builtin_base_with_dict(PyTypeObject *type) 2041 get_builtin_base_with_dict(PyTypeObject *type)
2039 { 2042 {
2040 while (type->tp_base != NULL) { 2043 while (type->tp_base != NULL) {
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
2117 "This object has no __dict__"); 2120 "This object has no __dict__");
2118 return -1; 2121 return -1;
2119 } 2122 }
2120 if (value != NULL && !PyDict_Check(value)) { 2123 if (value != NULL && !PyDict_Check(value)) {
2121 PyErr_Format(PyExc_TypeError, 2124 PyErr_Format(PyExc_TypeError,
2122 "__dict__ must be set to a dictionary, " 2125 "__dict__ must be set to a dictionary, "
2123 "not a '%.200s'", Py_TYPE(value)->tp_name); 2126 "not a '%.200s'", Py_TYPE(value)->tp_name);
2124 return -1; 2127 return -1;
2125 } 2128 }
2126 Py_XINCREF(value); 2129 Py_XINCREF(value);
2127 Py_SETREF(*dictptr, value); 2130 Py_XSETREF(*dictptr, value);
2128 return 0; 2131 return 0;
2129 } 2132 }
2130 2133
2131 static PyObject * 2134 static PyObject *
2132 subtype_getweakref(PyObject *obj, void *context) 2135 subtype_getweakref(PyObject *obj, void *context)
2133 { 2136 {
2134 PyObject **weaklistptr; 2137 PyObject **weaklistptr;
2135 PyObject *result; 2138 PyObject *result;
2136 2139
2137 if (Py_TYPE(obj)->tp_weaklistoffset == 0) { 2140 if (Py_TYPE(obj)->tp_weaklistoffset == 0) {
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
2196 object_init(PyObject *self, PyObject *args, PyObject *kwds); 2199 object_init(PyObject *self, PyObject *args, PyObject *kwds);
2197 2200
2198 static int 2201 static int
2199 type_init(PyObject *cls, PyObject *args, PyObject *kwds) 2202 type_init(PyObject *cls, PyObject *args, PyObject *kwds)
2200 { 2203 {
2201 int res; 2204 int res;
2202 2205
2203 assert(args != NULL && PyTuple_Check(args)); 2206 assert(args != NULL && PyTuple_Check(args));
2204 assert(kwds == NULL || PyDict_Check(kwds)); 2207 assert(kwds == NULL || PyDict_Check(kwds));
2205 2208
2206 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) { 2209 if (kwds != NULL && PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
2210 PyDict_Check(kwds) && PyDict_Size(kwds) != 0) {
2207 PyErr_SetString(PyExc_TypeError, 2211 PyErr_SetString(PyExc_TypeError,
2208 "type.__init__() takes no keyword arguments"); 2212 "type.__init__() takes no keyword arguments");
2209 return -1; 2213 return -1;
2210 } 2214 }
2211 2215
2212 if (args != NULL && PyTuple_Check(args) && 2216 if (args != NULL && PyTuple_Check(args) &&
2213 (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) { 2217 (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) {
2214 PyErr_SetString(PyExc_TypeError, 2218 PyErr_SetString(PyExc_TypeError,
2215 "type.__init__() takes 1 or 3 arguments"); 2219 "type.__init__() takes 1 or 3 arguments");
2216 return -1; 2220 return -1;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
2263 "of the metaclasses of all its bases"); 2267 "of the metaclasses of all its bases");
2264 return NULL; 2268 return NULL;
2265 } 2269 }
2266 return winner; 2270 return winner;
2267 } 2271 }
2268 2272
2269 static PyObject * 2273 static PyObject *
2270 type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) 2274 type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
2271 { 2275 {
2272 PyObject *name, *bases = NULL, *orig_dict, *dict = NULL; 2276 PyObject *name, *bases = NULL, *orig_dict, *dict = NULL;
2273 static char *kwlist[] = {"name", "bases", "dict", 0};
2274 PyObject *qualname, *slots = NULL, *tmp, *newslots; 2277 PyObject *qualname, *slots = NULL, *tmp, *newslots;
2275 PyTypeObject *type = NULL, *base, *tmptype, *winner; 2278 PyTypeObject *type = NULL, *base, *tmptype, *winner;
2276 PyHeapTypeObject *et; 2279 PyHeapTypeObject *et;
2277 PyMemberDef *mp; 2280 PyMemberDef *mp;
2278 Py_ssize_t i, nbases, nslots, slotoffset, name_size; 2281 Py_ssize_t i, nbases, nslots, slotoffset, name_size;
2279 int j, may_add_dict, may_add_weak, add_dict, add_weak; 2282 int j, may_add_dict, may_add_weak, add_dict, add_weak;
2280 _Py_IDENTIFIER(__qualname__); 2283 _Py_IDENTIFIER(__qualname__);
2281 _Py_IDENTIFIER(__slots__); 2284 _Py_IDENTIFIER(__slots__);
2282 2285
2283 assert(args != NULL && PyTuple_Check(args)); 2286 assert(args != NULL && PyTuple_Check(args));
2284 assert(kwds == NULL || PyDict_Check(kwds)); 2287 assert(kwds == NULL || PyDict_Check(kwds));
2285 2288
2286 /* Special case: type(x) should return x->ob_type */ 2289 /* Special case: type(x) should return x->ob_type */
2287 { 2290 {
2288 const Py_ssize_t nargs = PyTuple_GET_SIZE(args); 2291 const Py_ssize_t nargs = PyTuple_GET_SIZE(args);
2289 const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds); 2292 const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
2290 2293
2291 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) { 2294 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
2292 PyObject *x = PyTuple_GET_ITEM(args, 0); 2295 PyObject *x = PyTuple_GET_ITEM(args, 0);
2293 Py_INCREF(Py_TYPE(x)); 2296 Py_INCREF(Py_TYPE(x));
2294 return (PyObject *) Py_TYPE(x); 2297 return (PyObject *) Py_TYPE(x);
2295 } 2298 }
2296 2299
2297 /* SF bug 475327 -- if that didn't trigger, we need 3 2300 /* SF bug 475327 -- if that didn't trigger, we need 3
2298 arguments. but PyArg_ParseTupleAndKeywords below may give 2301 arguments. but PyArg_ParseTupleAndKeywords below may give
2299 a msg saying type() needs exactly 3. */ 2302 a msg saying type() needs exactly 3. */
2300 if (nargs + nkwds != 3) { 2303 if (nargs != 3) {
2301 PyErr_SetString(PyExc_TypeError, 2304 PyErr_SetString(PyExc_TypeError,
2302 "type() takes 1 or 3 arguments"); 2305 "type() takes 1 or 3 arguments");
2303 return NULL; 2306 return NULL;
2304 } 2307 }
2305 } 2308 }
2306 2309
2307 /* Check arguments: (name, bases, dict) */ 2310 /* Check arguments: (name, bases, dict) */
2308 if (!PyArg_ParseTupleAndKeywords(args, kwds, "UO!O!:type", kwlist, 2311 if (!PyArg_ParseTuple(args, "UO!O!:type", &name, &PyTuple_Type, &bases,
2309 &name, 2312 &PyDict_Type, &orig_dict))
2310 &PyTuple_Type, &bases,
2311 &PyDict_Type, &orig_dict))
2312 return NULL; 2313 return NULL;
2313 2314
2314 /* Determine the proper metatype to deal with this: */ 2315 /* Determine the proper metatype to deal with this: */
2315 winner = _PyType_CalculateMetaclass(metatype, bases); 2316 winner = _PyType_CalculateMetaclass(metatype, bases);
2316 if (winner == NULL) { 2317 if (winner == NULL) {
2317 return NULL; 2318 return NULL;
2318 } 2319 }
2319 2320
2320 if (winner != metatype) { 2321 if (winner != metatype) {
2321 if (winner->tp_new != type_new) /* Pass it to the winner */ 2322 if (winner->tp_new != type_new) /* Pass it to the winner */
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
2574 } 2575 }
2575 } 2576 }
2576 2577
2577 /* Special-case __new__: if it's a plain function, 2578 /* Special-case __new__: if it's a plain function,
2578 make it a static function */ 2579 make it a static function */
2579 tmp = _PyDict_GetItemId(dict, &PyId___new__); 2580 tmp = _PyDict_GetItemId(dict, &PyId___new__);
2580 if (tmp != NULL && PyFunction_Check(tmp)) { 2581 if (tmp != NULL && PyFunction_Check(tmp)) {
2581 tmp = PyStaticMethod_New(tmp); 2582 tmp = PyStaticMethod_New(tmp);
2582 if (tmp == NULL) 2583 if (tmp == NULL)
2583 goto error; 2584 goto error;
2584 if (_PyDict_SetItemId(dict, &PyId___new__, tmp) < 0) 2585 if (_PyDict_SetItemId(dict, &PyId___new__, tmp) < 0) {
2586 Py_DECREF(tmp);
2585 goto error; 2587 goto error;
2588 }
2589 Py_DECREF(tmp);
2590 }
2591
2592 /* Special-case __init_subclass__: if it's a plain function,
2593 make it a classmethod */
2594 tmp = _PyDict_GetItemId(dict, &PyId___init_subclass__);
2595 if (tmp != NULL && PyFunction_Check(tmp)) {
2596 tmp = PyClassMethod_New(tmp);
2597 if (tmp == NULL)
2598 goto error;
2599 if (_PyDict_SetItemId(dict, &PyId___init_subclass__, tmp) < 0) {
2600 Py_DECREF(tmp);
2601 goto error;
2602 }
2586 Py_DECREF(tmp); 2603 Py_DECREF(tmp);
2587 } 2604 }
2588 2605
2589 /* Add descriptors for custom slots from __slots__, or for __dict__ */ 2606 /* Add descriptors for custom slots from __slots__, or for __dict__ */
2590 mp = PyHeapType_GET_MEMBERS(et); 2607 mp = PyHeapType_GET_MEMBERS(et);
2591 slotoffset = base->tp_basicsize; 2608 slotoffset = base->tp_basicsize;
2592 if (et->ht_slots != NULL) { 2609 if (et->ht_slots != NULL) {
2593 for (i = 0; i < nslots; i++, mp++) { 2610 for (i = 0; i < nslots; i++, mp++) {
2594 mp->name = _PyUnicode_AsString( 2611 mp->name = _PyUnicode_AsString(
2595 PyTuple_GET_ITEM(et->ht_slots, i)); 2612 PyTuple_GET_ITEM(et->ht_slots, i));
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
2658 /* Initialize the rest */ 2675 /* Initialize the rest */
2659 if (PyType_Ready(type) < 0) 2676 if (PyType_Ready(type) < 0)
2660 goto error; 2677 goto error;
2661 2678
2662 /* Put the proper slots in place */ 2679 /* Put the proper slots in place */
2663 fixup_slot_dispatchers(type); 2680 fixup_slot_dispatchers(type);
2664 2681
2665 if (type->tp_dictoffset) { 2682 if (type->tp_dictoffset) {
2666 et->ht_cached_keys = _PyDict_NewKeysForClass(); 2683 et->ht_cached_keys = _PyDict_NewKeysForClass();
2667 } 2684 }
2685
2686 if (set_names(type) < 0)
2687 goto error;
2688
2689 if (init_subclass(type, kwds) < 0)
2690 goto error;
2668 2691
2669 Py_DECREF(dict); 2692 Py_DECREF(dict);
2670 return (PyObject *)type; 2693 return (PyObject *)type;
2671 2694
2672 error: 2695 error:
2673 Py_XDECREF(dict); 2696 Py_XDECREF(dict);
2674 Py_XDECREF(bases); 2697 Py_XDECREF(bases);
2675 Py_XDECREF(slots); 2698 Py_XDECREF(slots);
2676 Py_XDECREF(type); 2699 Py_XDECREF(type);
2677 return NULL; 2700 return NULL;
(...skipping 1240 matching lines...) Expand 10 before | Expand all | Expand 10 after
3918 } 3941 }
3919 else { 3942 else {
3920 int err = PyDict_SetItem(slots, name, value); 3943 int err = PyDict_SetItem(slots, name, value);
3921 Py_DECREF(name); 3944 Py_DECREF(name);
3922 Py_DECREF(value); 3945 Py_DECREF(value);
3923 if (err) { 3946 if (err) {
3924 goto error; 3947 goto error;
3925 } 3948 }
3926 } 3949 }
3927 3950
3928 /* The list is stored on the class so it may mutates while we 3951 /* The list is stored on the class so it may mutate while we
3929 iterate over it */ 3952 iterate over it */
3930 if (slotnames_size != Py_SIZE(slotnames)) { 3953 if (slotnames_size != Py_SIZE(slotnames)) {
3931 PyErr_Format(PyExc_RuntimeError, 3954 PyErr_Format(PyExc_RuntimeError,
3932 "__slotsname__ changed size during iteration"); 3955 "__slotsname__ changed size during iteration");
3933 goto error; 3956 goto error;
3934 } 3957 }
3935 3958
3936 /* We handle errors within the loop here. */ 3959 /* We handle errors within the loop here. */
3937 if (0) { 3960 if (0) {
3938 error: 3961 error:
3939 Py_DECREF(slotnames); 3962 Py_DECREF(slotnames);
3940 Py_DECREF(slots); 3963 Py_DECREF(slots);
3941 Py_DECREF(state); 3964 Py_DECREF(state);
3942 return NULL; 3965 return NULL;
3943 } 3966 }
3944 } 3967 }
3945 3968
3946 /* If we found some slot attributes, pack them in a tuple along 3969 /* If we found some slot attributes, pack them in a tuple along
3947 the orginal attribute dictionary. */ 3970 the original attribute dictionary. */
3948 if (PyDict_Size(slots) > 0) { 3971 if (PyDict_Size(slots) > 0) {
3949 PyObject *state2; 3972 PyObject *state2;
3950 3973
3951 state2 = PyTuple_Pack(2, state, slots); 3974 state2 = PyTuple_Pack(2, state, slots);
3952 Py_DECREF(state); 3975 Py_DECREF(state);
3953 if (state2 == NULL) { 3976 if (state2 == NULL) {
3954 Py_DECREF(slotnames); 3977 Py_DECREF(slotnames);
3955 Py_DECREF(slots); 3978 Py_DECREF(slots);
3956 return NULL; 3979 return NULL;
3957 } 3980 }
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
4052 Py_CLEAR(*args); 4075 Py_CLEAR(*args);
4053 return -1; 4076 return -1;
4054 } 4077 }
4055 *kwargs = NULL; 4078 *kwargs = NULL;
4056 return 0; 4079 return 0;
4057 } else if (PyErr_Occurred()) { 4080 } else if (PyErr_Occurred()) {
4058 return -1; 4081 return -1;
4059 } 4082 }
4060 4083
4061 /* The object does not have __getnewargs_ex__ and __getnewargs__. This may 4084 /* The object does not have __getnewargs_ex__ and __getnewargs__. This may
4062 means __new__ does not takes any arguments on this object, or that the 4085 mean __new__ does not takes any arguments on this object, or that the
4063 object does not implement the reduce protocol for pickling or 4086 object does not implement the reduce protocol for pickling or
4064 copying. */ 4087 copying. */
4065 *args = NULL; 4088 *args = NULL;
4066 *kwargs = NULL; 4089 *kwargs = NULL;
4067 return 0; 4090 return 0;
4068 } 4091 }
4069 4092
4070 Py_LOCAL(int) 4093 Py_LOCAL(int)
4071 _PyObject_GetItemsIter(PyObject *obj, PyObject **listitems, 4094 _PyObject_GetItemsIter(PyObject *obj, PyObject **listitems,
4072 PyObject **dictitems) 4095 PyObject **dictitems)
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
4304 } 4327 }
4305 4328
4306 PyDoc_STRVAR(object_subclasshook_doc, 4329 PyDoc_STRVAR(object_subclasshook_doc,
4307 "Abstract classes can override this to customize issubclass().\n" 4330 "Abstract classes can override this to customize issubclass().\n"
4308 "\n" 4331 "\n"
4309 "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n" 4332 "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n"
4310 "It should return True, False or NotImplemented. If it returns\n" 4333 "It should return True, False or NotImplemented. If it returns\n"
4311 "NotImplemented, the normal algorithm is used. Otherwise, it\n" 4334 "NotImplemented, the normal algorithm is used. Otherwise, it\n"
4312 "overrides the normal algorithm (and the outcome is cached).\n"); 4335 "overrides the normal algorithm (and the outcome is cached).\n");
4313 4336
4337 static PyObject *
4338 object_init_subclass(PyObject *cls, PyObject *arg)
4339 {
4340 Py_RETURN_NONE;
4341 }
4342
4343 PyDoc_STRVAR(object_init_subclass_doc,
4344 "This method is called when a class is subclassed.\n"
4345 "\n"
4346 "The default implementation does nothing. It may be\n"
4347 "overridden to extend subclasses.\n");
4348
4314 /* 4349 /*
4315 from PEP 3101, this code implements: 4350 from PEP 3101, this code implements:
4316 4351
4317 class object: 4352 class object:
4318 def __format__(self, format_spec): 4353 def __format__(self, format_spec):
4319 return format(str(self), format_spec) 4354 return format(str(self), format_spec)
4320 */ 4355 */
4321 static PyObject * 4356 static PyObject *
4322 object_format(PyObject *self, PyObject *args) 4357 object_format(PyObject *self, PyObject *args)
4323 { 4358 {
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
4408 return result; 4443 return result;
4409 } 4444 }
4410 4445
4411 static PyMethodDef object_methods[] = { 4446 static PyMethodDef object_methods[] = {
4412 {"__reduce_ex__", object_reduce_ex, METH_VARARGS, 4447 {"__reduce_ex__", object_reduce_ex, METH_VARARGS,
4413 PyDoc_STR("helper for pickle")}, 4448 PyDoc_STR("helper for pickle")},
4414 {"__reduce__", object_reduce, METH_VARARGS, 4449 {"__reduce__", object_reduce, METH_VARARGS,
4415 PyDoc_STR("helper for pickle")}, 4450 PyDoc_STR("helper for pickle")},
4416 {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS, 4451 {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS,
4417 object_subclasshook_doc}, 4452 object_subclasshook_doc},
4453 {"__init_subclass__", object_init_subclass, METH_CLASS | METH_NOARGS,
4454 object_init_subclass_doc},
4418 {"__format__", object_format, METH_VARARGS, 4455 {"__format__", object_format, METH_VARARGS,
4419 PyDoc_STR("default object formatter")}, 4456 PyDoc_STR("default object formatter")},
4420 {"__sizeof__", object_sizeof, METH_NOARGS, 4457 {"__sizeof__", object_sizeof, METH_NOARGS,
4421 PyDoc_STR("__sizeof__() -> int\nsize of object in memory, in bytes")}, 4458 PyDoc_STR("__sizeof__() -> int\nsize of object in memory, in bytes")},
4422 {"__dir__", object_dir, METH_NOARGS, 4459 {"__dir__", object_dir, METH_NOARGS,
4423 PyDoc_STR("__dir__() -> list\ndefault dir() implementation")}, 4460 PyDoc_STR("__dir__() -> list\ndefault dir() implementation")},
4424 {0} 4461 {0}
4425 }; 4462 };
4426 4463
4427 4464
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
4514 { 4551 {
4515 PyObject *dict = type->tp_dict; 4552 PyObject *dict = type->tp_dict;
4516 4553
4517 for (; memb->name != NULL; memb++) { 4554 for (; memb->name != NULL; memb++) {
4518 PyObject *descr; 4555 PyObject *descr;
4519 if (PyDict_GetItemString(dict, memb->name)) 4556 if (PyDict_GetItemString(dict, memb->name))
4520 continue; 4557 continue;
4521 descr = PyDescr_NewMember(type, memb); 4558 descr = PyDescr_NewMember(type, memb);
4522 if (descr == NULL) 4559 if (descr == NULL)
4523 return -1; 4560 return -1;
4524 if (PyDict_SetItemString(dict, memb->name, descr) < 0) 4561 if (PyDict_SetItemString(dict, memb->name, descr) < 0) {
4562 Py_DECREF(descr);
4525 return -1; 4563 return -1;
4564 }
4526 Py_DECREF(descr); 4565 Py_DECREF(descr);
4527 } 4566 }
4528 return 0; 4567 return 0;
4529 } 4568 }
4530 4569
4531 static int 4570 static int
4532 add_getset(PyTypeObject *type, PyGetSetDef *gsp) 4571 add_getset(PyTypeObject *type, PyGetSetDef *gsp)
4533 { 4572 {
4534 PyObject *dict = type->tp_dict; 4573 PyObject *dict = type->tp_dict;
4535 4574
4536 for (; gsp->name != NULL; gsp++) { 4575 for (; gsp->name != NULL; gsp++) {
4537 PyObject *descr; 4576 PyObject *descr;
4538 if (PyDict_GetItemString(dict, gsp->name)) 4577 if (PyDict_GetItemString(dict, gsp->name))
4539 continue; 4578 continue;
4540 descr = PyDescr_NewGetSet(type, gsp); 4579 descr = PyDescr_NewGetSet(type, gsp);
4541 4580
4542 if (descr == NULL) 4581 if (descr == NULL)
4543 return -1; 4582 return -1;
4544 if (PyDict_SetItemString(dict, gsp->name, descr) < 0) 4583 if (PyDict_SetItemString(dict, gsp->name, descr) < 0) {
4584 Py_DECREF(descr);
4545 return -1; 4585 return -1;
4586 }
4546 Py_DECREF(descr); 4587 Py_DECREF(descr);
4547 } 4588 }
4548 return 0; 4589 return 0;
4549 } 4590 }
4550 4591
4551 static void 4592 static void
4552 inherit_special(PyTypeObject *type, PyTypeObject *base) 4593 inherit_special(PyTypeObject *type, PyTypeObject *base)
4553 { 4594 {
4554 4595
4555 /* Copying basicsize is connected to the GC flags */ 4596 /* Copying basicsize is connected to the GC flags */
(...skipping 1213 matching lines...) Expand 10 before | Expand all | Expand 10 after
5769 if (func == NULL) { 5810 if (func == NULL) {
5770 return NULL; 5811 return NULL;
5771 } 5812 }
5772 } 5813 }
5773 ival = PyLong_FromSsize_t(i); 5814 ival = PyLong_FromSsize_t(i);
5774 if (ival != NULL) { 5815 if (ival != NULL) {
5775 args = PyTuple_New(1); 5816 args = PyTuple_New(1);
5776 if (args != NULL) { 5817 if (args != NULL) {
5777 PyTuple_SET_ITEM(args, 0, ival); 5818 PyTuple_SET_ITEM(args, 0, ival);
5778 retval = PyObject_Call(func, args, NULL); 5819 retval = PyObject_Call(func, args, NULL);
5779 Py_XDECREF(args); 5820 Py_DECREF(args);
5780 Py_XDECREF(func); 5821 Py_DECREF(func);
5781 return retval; 5822 return retval;
5782 } 5823 }
5783 } 5824 }
5784 } 5825 }
5785 else { 5826 else {
5786 PyObject *getitem_str = _PyUnicode_FromId(&PyId___getitem__); 5827 PyObject *getitem_str = _PyUnicode_FromId(&PyId___getitem__);
5787 PyErr_SetObject(PyExc_AttributeError, getitem_str); 5828 PyErr_SetObject(PyExc_AttributeError, getitem_str);
5788 } 5829 }
5789 Py_XDECREF(args); 5830 Py_XDECREF(args);
5790 Py_XDECREF(ival); 5831 Py_XDECREF(ival);
(...skipping 1136 matching lines...) Expand 10 before | Expand all | Expand 10 after
6927 { 6968 {
6928 slotdef *p; 6969 slotdef *p;
6929 6970
6930 init_slotdefs(); 6971 init_slotdefs();
6931 for (p = slotdefs; p->name; p++) { 6972 for (p = slotdefs; p->name; p++) {
6932 /* update_slot returns int but can't actually fail */ 6973 /* update_slot returns int but can't actually fail */
6933 update_slot(type, p->name_strobj); 6974 update_slot(type, p->name_strobj);
6934 } 6975 }
6935 } 6976 }
6936 6977
6978 /* Call __set_name__ on all descriptors in a newly generated type */
6979 static int
6980 set_names(PyTypeObject *type)
6981 {
6982 PyObject *key, *value, *tmp;
6983 Py_ssize_t i = 0;
6984
6985 while (PyDict_Next(type->tp_dict, &i, &key, &value)) {
6986 if (PyObject_HasAttr(value, _PyUnicode_FromId(&PyId___set_name__))) {
6987 tmp = PyObject_CallMethodObjArgs(
6988 value, _PyUnicode_FromId(&PyId___set_name__),
6989 type, key, NULL);
6990 if (tmp == NULL)
6991 return -1;
6992 else
6993 Py_DECREF(tmp);
6994 }
6995 }
6996
6997 return 0;
6998 }
6999
7000 /* Call __init_subclass__ on the parent of a newly generated type */
7001 static int
7002 init_subclass(PyTypeObject *type, PyObject *kwds)
7003 {
7004 PyObject *super, *func, *tmp, *tuple;
7005
7006 super = PyObject_CallFunctionObjArgs((PyObject *) &PySuper_Type,
7007 type, type, NULL);
7008 func = _PyObject_GetAttrId(super, &PyId___init_subclass__);
7009 Py_DECREF(super);
7010
7011 if (func == NULL)
7012 return -1;
7013
7014 tuple = PyTuple_New(0);
7015 tmp = PyObject_Call(func, tuple, kwds);
7016 Py_DECREF(tuple);
7017 Py_DECREF(func);
7018
7019 if (tmp == NULL)
7020 return -1;
7021
7022 Py_DECREF(tmp);
7023 return 0;
7024 }
7025
6937 /* recurse_down_subclasses() and update_subclasses() are mutually 7026 /* recurse_down_subclasses() and update_subclasses() are mutually
6938 recursive functions to call a callback for all subclasses, 7027 recursive functions to call a callback for all subclasses,
6939 but refraining from recursing into subclasses that define 'name'. */ 7028 but refraining from recursing into subclasses that define 'name'. */
6940 7029
6941 static int 7030 static int
6942 update_subclasses(PyTypeObject *type, PyObject *name, 7031 update_subclasses(PyTypeObject *type, PyObject *name,
6943 update_callback callback, void *data) 7032 update_callback callback, void *data)
6944 { 7033 {
6945 if (callback(type, data) < 0) 7034 if (callback(type, data) < 0)
6946 return -1; 7035 return -1;
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
7029 /* Classes may prevent the inheritance of the tp_hash 7118 /* Classes may prevent the inheritance of the tp_hash
7030 slot by storing PyObject_HashNotImplemented in it. Make it 7119 slot by storing PyObject_HashNotImplemented in it. Make it
7031 visible as a None value for the __hash__ attribute. */ 7120 visible as a None value for the __hash__ attribute. */
7032 if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0) 7121 if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0)
7033 return -1; 7122 return -1;
7034 } 7123 }
7035 else { 7124 else {
7036 descr = PyDescr_NewWrapper(type, p, *ptr); 7125 descr = PyDescr_NewWrapper(type, p, *ptr);
7037 if (descr == NULL) 7126 if (descr == NULL)
7038 return -1; 7127 return -1;
7039 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) 7128 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) {
7129 Py_DECREF(descr);
7040 return -1; 7130 return -1;
7131 }
7041 Py_DECREF(descr); 7132 Py_DECREF(descr);
7042 } 7133 }
7043 } 7134 }
7044 if (type->tp_new != NULL) { 7135 if (type->tp_new != NULL) {
7045 if (add_tp_new_wrapper(type) < 0) 7136 if (add_tp_new_wrapper(type) < 0)
7046 return -1; 7137 return -1;
7047 } 7138 }
7048 return 0; 7139 return 0;
7049 } 7140 }
7050 7141
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after
7357 7448
7358 if (obj == Py_None) 7449 if (obj == Py_None)
7359 obj = NULL; 7450 obj = NULL;
7360 if (obj != NULL) { 7451 if (obj != NULL) {
7361 obj_type = supercheck(type, obj); 7452 obj_type = supercheck(type, obj);
7362 if (obj_type == NULL) 7453 if (obj_type == NULL)
7363 return -1; 7454 return -1;
7364 Py_INCREF(obj); 7455 Py_INCREF(obj);
7365 } 7456 }
7366 Py_INCREF(type); 7457 Py_INCREF(type);
7367 su->type = type; 7458 Py_XSETREF(su->type, type);
7368 su->obj = obj; 7459 Py_XSETREF(su->obj, obj);
7369 su->obj_type = obj_type; 7460 Py_XSETREF(su->obj_type, obj_type);
7370 return 0; 7461 return 0;
7371 } 7462 }
7372 7463
7373 PyDoc_STRVAR(super_doc, 7464 PyDoc_STRVAR(super_doc,
7374 "super() -> same as super(__class__, <first argument>)\n" 7465 "super() -> same as super(__class__, <first argument>)\n"
7375 "super(type) -> unbound super object\n" 7466 "super(type) -> unbound super object\n"
7376 "super(type, obj) -> bound super object; requires isinstance(obj, type)\n" 7467 "super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
7377 "super(type, type2) -> bound super object; requires issubclass(type2, type)\n" 7468 "super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
7378 "Typical use to call a cooperative superclass method:\n" 7469 "Typical use to call a cooperative superclass method:\n"
7379 "class C(B):\n" 7470 "class C(B):\n"
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
7433 0, /* tp_base */ 7524 0, /* tp_base */
7434 0, /* tp_dict */ 7525 0, /* tp_dict */
7435 super_descr_get, /* tp_descr_get */ 7526 super_descr_get, /* tp_descr_get */
7436 0, /* tp_descr_set */ 7527 0, /* tp_descr_set */
7437 0, /* tp_dictoffset */ 7528 0, /* tp_dictoffset */
7438 super_init, /* tp_init */ 7529 super_init, /* tp_init */
7439 PyType_GenericAlloc, /* tp_alloc */ 7530 PyType_GenericAlloc, /* tp_alloc */
7440 PyType_GenericNew, /* tp_new */ 7531 PyType_GenericNew, /* tp_new */
7441 PyObject_GC_Del, /* tp_free */ 7532 PyObject_GC_Del, /* tp_free */
7442 }; 7533 };
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+