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

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 27 matching lines...) Expand all
541 static PyObject * 543 static PyObject *
542 type_get_bases(PyTypeObject *type, void *context) 544 type_get_bases(PyTypeObject *type, void *context)
543 { 545 {
544 Py_INCREF(type->tp_bases); 546 Py_INCREF(type->tp_bases);
545 return type->tp_bases; 547 return type->tp_bases;
546 } 548 }
547 549
548 static PyTypeObject *best_base(PyObject *); 550 static PyTypeObject *best_base(PyObject *);
549 static int mro_internal(PyTypeObject *, PyObject **); 551 static int mro_internal(PyTypeObject *, PyObject **);
550 Py_LOCAL_INLINE(int) type_is_subtype_base_chain(PyTypeObject *, PyTypeObject *); 552 Py_LOCAL_INLINE(int) type_is_subtype_base_chain(PyTypeObject *, PyTypeObject *);
551 static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *); 553 static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, const char *);
552 static int add_subclass(PyTypeObject*, PyTypeObject*); 554 static int add_subclass(PyTypeObject*, PyTypeObject*);
553 static int add_all_subclasses(PyTypeObject *type, PyObject *bases); 555 static int add_all_subclasses(PyTypeObject *type, PyObject *bases);
554 static void remove_subclass(PyTypeObject *, PyTypeObject *); 556 static void remove_subclass(PyTypeObject *, PyTypeObject *);
555 static void remove_all_subclasses(PyTypeObject *type, PyObject *bases); 557 static void remove_all_subclasses(PyTypeObject *type, PyObject *bases);
556 static void update_all_slots(PyTypeObject *); 558 static void update_all_slots(PyTypeObject *);
557 559
558 typedef int (*update_callback)(PyTypeObject *, void *); 560 typedef int (*update_callback)(PyTypeObject *, void *);
559 static int update_subclasses(PyTypeObject *type, PyObject *name, 561 static int update_subclasses(PyTypeObject *type, PyObject *name,
560 update_callback callback, void *data); 562 update_callback callback, void *data);
561 static int recurse_down_subclasses(PyTypeObject *type, PyObject *name, 563 static int recurse_down_subclasses(PyTypeObject *type, PyObject *name,
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
881 } 883 }
882 884
883 #ifdef Py_DEBUG 885 #ifdef Py_DEBUG
884 /* type_call() must not be called with an exception set, 886 /* type_call() must not be called with an exception set,
885 because it may clear it (directly or indirectly) and so the 887 because it may clear it (directly or indirectly) and so the
886 caller loses its exception */ 888 caller loses its exception */
887 assert(!PyErr_Occurred()); 889 assert(!PyErr_Occurred());
888 #endif 890 #endif
889 891
890 obj = type->tp_new(type, args, kwds); 892 obj = type->tp_new(type, args, kwds);
891 if (obj != NULL) { 893 obj = _Py_CheckFunctionResult((PyObject*)type, obj, NULL);
892 /* Ugly exception: when the call was type(something), 894 if (obj == NULL)
893 don't call tp_init on the result. */ 895 return NULL;
894 if (type == &PyType_Type && 896
895 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 && 897 /* Ugly exception: when the call was type(something),
896 (kwds == NULL || 898 don't call tp_init on the result. */
897 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) 899 if (type == &PyType_Type &&
898 return obj; 900 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
899 /* If the returned object is not an instance of type, 901 (kwds == NULL ||
900 it won't be initialized. */ 902 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
901 if (!PyType_IsSubtype(Py_TYPE(obj), type)) 903 return obj;
902 return obj; 904
903 type = Py_TYPE(obj); 905 /* If the returned object is not an instance of type,
904 if (type->tp_init != NULL) { 906 it won't be initialized. */
905 int res = type->tp_init(obj, args, kwds); 907 if (!PyType_IsSubtype(Py_TYPE(obj), type))
906 if (res < 0) { 908 return obj;
907 Py_DECREF(obj); 909
908 obj = NULL; 910 type = Py_TYPE(obj);
909 } 911 if (type->tp_init != NULL) {
912 int res = type->tp_init(obj, args, kwds);
913 if (res < 0) {
914 assert(PyErr_Occurred());
915 Py_DECREF(obj);
916 obj = NULL;
917 }
918 else {
919 assert(!PyErr_Occurred());
910 } 920 }
911 } 921 }
912 return obj; 922 return obj;
913 } 923 }
914 924
915 PyObject * 925 PyObject *
916 PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) 926 PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
917 { 927 {
918 PyObject *obj; 928 PyObject *obj;
919 const size_t size = _PyObject_VAR_SIZE(type, nitems+1); 929 const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
(...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after
1404 _PyObject_LookupSpecial(PyObject *self, _Py_Identifier *attrid) 1414 _PyObject_LookupSpecial(PyObject *self, _Py_Identifier *attrid)
1405 { 1415 {
1406 return lookup_maybe(self, attrid); 1416 return lookup_maybe(self, attrid);
1407 } 1417 }
1408 1418
1409 /* A variation of PyObject_CallMethod that uses lookup_method() 1419 /* A variation of PyObject_CallMethod that uses lookup_method()
1410 instead of PyObject_GetAttrString(). This uses the same convention 1420 instead of PyObject_GetAttrString(). This uses the same convention
1411 as lookup_method to cache the interned name string object. */ 1421 as lookup_method to cache the interned name string object. */
1412 1422
1413 static PyObject * 1423 static PyObject *
1414 call_method(PyObject *o, _Py_Identifier *nameid, char *format, ...) 1424 call_method(PyObject *o, _Py_Identifier *nameid, const char *format, ...)
1415 { 1425 {
1416 va_list va; 1426 va_list va;
1417 PyObject *args, *func = 0, *retval; 1427 PyObject *args, *func = 0, *retval;
1418 va_start(va, format); 1428 va_start(va, format);
1419 1429
1420 func = lookup_maybe(o, nameid); 1430 func = lookup_maybe(o, nameid);
1421 if (func == NULL) { 1431 if (func == NULL) {
1422 va_end(va); 1432 va_end(va);
1423 if (!PyErr_Occurred()) 1433 if (!PyErr_Occurred())
1424 PyErr_SetObject(PyExc_AttributeError, nameid->object); 1434 PyErr_SetObject(PyExc_AttributeError, nameid->object);
(...skipping 15 matching lines...) Expand all
1440 1450
1441 Py_DECREF(args); 1451 Py_DECREF(args);
1442 Py_DECREF(func); 1452 Py_DECREF(func);
1443 1453
1444 return retval; 1454 return retval;
1445 } 1455 }
1446 1456
1447 /* Clone of call_method() that returns NotImplemented when the lookup fails. */ 1457 /* Clone of call_method() that returns NotImplemented when the lookup fails. */
1448 1458
1449 static PyObject * 1459 static PyObject *
1450 call_maybe(PyObject *o, _Py_Identifier *nameid, char *format, ...) 1460 call_maybe(PyObject *o, _Py_Identifier *nameid, const char *format, ...)
1451 { 1461 {
1452 va_list va; 1462 va_list va;
1453 PyObject *args, *func = 0, *retval; 1463 PyObject *args, *func = 0, *retval;
1454 va_start(va, format); 1464 va_start(va, format);
1455 1465
1456 func = lookup_maybe(o, nameid); 1466 func = lookup_maybe(o, nameid);
1457 if (func == NULL) { 1467 if (func == NULL) {
1458 va_end(va); 1468 va_end(va);
1459 if (!PyErr_Occurred()) 1469 if (!PyErr_Occurred())
1460 Py_RETURN_NOTIMPLEMENTED; 1470 Py_RETURN_NOTIMPLEMENTED;
(...skipping 26 matching lines...) Expand all
1487 David A. Moon, Keith Playford, and P. Tucker Withington. 1497 David A. Moon, Keith Playford, and P. Tucker Withington.
1488 (OOPSLA 1996) 1498 (OOPSLA 1996)
1489 1499
1490 Some notes about the rules implied by C3: 1500 Some notes about the rules implied by C3:
1491 1501
1492 No duplicate bases. 1502 No duplicate bases.
1493 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.
1494 1504
1495 The next three properties are the 3 constraints in "C3". 1505 The next three properties are the 3 constraints in "C3".
1496 1506
1497 Local precendece order. 1507 Local precedence order.
1498 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
1499 subclasses of C. 1509 subclasses of C.
1500 1510
1501 Monotonicity. 1511 Monotonicity.
1502 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
1503 MRO of each of its superclasses. 1513 MRO of each of its superclasses.
1504 1514
1505 Extended Precedence Graph (EPG). 1515 Extended Precedence Graph (EPG).
1506 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
1507 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
1519 } 1529 }
1520 return 0; 1530 return 0;
1521 } 1531 }
1522 1532
1523 static PyObject * 1533 static PyObject *
1524 class_name(PyObject *cls) 1534 class_name(PyObject *cls)
1525 { 1535 {
1526 PyObject *name = _PyObject_GetAttrId(cls, &PyId___name__); 1536 PyObject *name = _PyObject_GetAttrId(cls, &PyId___name__);
1527 if (name == NULL) { 1537 if (name == NULL) {
1528 PyErr_Clear(); 1538 PyErr_Clear();
1529 Py_XDECREF(name);
1530 name = PyObject_Repr(cls); 1539 name = PyObject_Repr(cls);
1531 } 1540 }
1532 if (name == NULL) 1541 if (name == NULL)
1533 return NULL; 1542 return NULL;
1534 if (!PyUnicode_Check(name)) { 1543 if (!PyUnicode_Check(name)) {
1535 Py_DECREF(name); 1544 Py_DECREF(name);
1536 return NULL; 1545 return NULL;
1537 } 1546 }
1538 return name; 1547 return name;
1539 } 1548 }
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after
2013 if (extra_ivars(type, base)) 2022 if (extra_ivars(type, base))
2014 return type; 2023 return type;
2015 else 2024 else
2016 return base; 2025 return base;
2017 } 2026 }
2018 2027
2019 static void object_dealloc(PyObject *); 2028 static void object_dealloc(PyObject *);
2020 static int object_init(PyObject *, PyObject *, PyObject *); 2029 static int object_init(PyObject *, PyObject *, PyObject *);
2021 static int update_slot(PyTypeObject *, PyObject *); 2030 static int update_slot(PyTypeObject *, PyObject *);
2022 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 *);
2023 2034
2024 /* 2035 /*
2025 * 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
2026 * inherited from various builtin types. The builtin base usually provides 2037 * inherited from various builtin types. The builtin base usually provides
2027 * its own __dict__ descriptor, so we use that when we can. 2038 * its own __dict__ descriptor, so we use that when we can.
2028 */ 2039 */
2029 static PyTypeObject * 2040 static PyTypeObject *
2030 get_builtin_base_with_dict(PyTypeObject *type) 2041 get_builtin_base_with_dict(PyTypeObject *type)
2031 { 2042 {
2032 while (type->tp_base != NULL) { 2043 while (type->tp_base != NULL) {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
2077 return NULL; 2088 return NULL;
2078 } 2089 }
2079 return func(descr, obj, (PyObject *)(Py_TYPE(obj))); 2090 return func(descr, obj, (PyObject *)(Py_TYPE(obj)));
2080 } 2091 }
2081 return PyObject_GenericGetDict(obj, context); 2092 return PyObject_GenericGetDict(obj, context);
2082 } 2093 }
2083 2094
2084 static int 2095 static int
2085 subtype_setdict(PyObject *obj, PyObject *value, void *context) 2096 subtype_setdict(PyObject *obj, PyObject *value, void *context)
2086 { 2097 {
2087 PyObject *dict, **dictptr; 2098 PyObject **dictptr;
2088 PyTypeObject *base; 2099 PyTypeObject *base;
2089 2100
2090 base = get_builtin_base_with_dict(Py_TYPE(obj)); 2101 base = get_builtin_base_with_dict(Py_TYPE(obj));
2091 if (base != NULL) { 2102 if (base != NULL) {
2092 descrsetfunc func; 2103 descrsetfunc func;
2093 PyObject *descr = get_dict_descriptor(base); 2104 PyObject *descr = get_dict_descriptor(base);
2094 if (descr == NULL) { 2105 if (descr == NULL) {
2095 raise_dict_descr_error(obj); 2106 raise_dict_descr_error(obj);
2096 return -1; 2107 return -1;
2097 } 2108 }
(...skipping 10 matching lines...) Expand all
2108 PyErr_SetString(PyExc_AttributeError, 2119 PyErr_SetString(PyExc_AttributeError,
2109 "This object has no __dict__"); 2120 "This object has no __dict__");
2110 return -1; 2121 return -1;
2111 } 2122 }
2112 if (value != NULL && !PyDict_Check(value)) { 2123 if (value != NULL && !PyDict_Check(value)) {
2113 PyErr_Format(PyExc_TypeError, 2124 PyErr_Format(PyExc_TypeError,
2114 "__dict__ must be set to a dictionary, " 2125 "__dict__ must be set to a dictionary, "
2115 "not a '%.200s'", Py_TYPE(value)->tp_name); 2126 "not a '%.200s'", Py_TYPE(value)->tp_name);
2116 return -1; 2127 return -1;
2117 } 2128 }
2118 dict = *dictptr;
2119 Py_XINCREF(value); 2129 Py_XINCREF(value);
2120 *dictptr = value; 2130 Py_XSETREF(*dictptr, value);
2121 Py_XDECREF(dict);
2122 return 0; 2131 return 0;
2123 } 2132 }
2124 2133
2125 static PyObject * 2134 static PyObject *
2126 subtype_getweakref(PyObject *obj, void *context) 2135 subtype_getweakref(PyObject *obj, void *context)
2127 { 2136 {
2128 PyObject **weaklistptr; 2137 PyObject **weaklistptr;
2129 PyObject *result; 2138 PyObject *result;
2130 2139
2131 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
2190 object_init(PyObject *self, PyObject *args, PyObject *kwds); 2199 object_init(PyObject *self, PyObject *args, PyObject *kwds);
2191 2200
2192 static int 2201 static int
2193 type_init(PyObject *cls, PyObject *args, PyObject *kwds) 2202 type_init(PyObject *cls, PyObject *args, PyObject *kwds)
2194 { 2203 {
2195 int res; 2204 int res;
2196 2205
2197 assert(args != NULL && PyTuple_Check(args)); 2206 assert(args != NULL && PyTuple_Check(args));
2198 assert(kwds == NULL || PyDict_Check(kwds)); 2207 assert(kwds == NULL || PyDict_Check(kwds));
2199 2208
2200 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) {
2201 PyErr_SetString(PyExc_TypeError, 2211 PyErr_SetString(PyExc_TypeError,
2202 "type.__init__() takes no keyword arguments"); 2212 "type.__init__() takes no keyword arguments");
2203 return -1; 2213 return -1;
2204 } 2214 }
2205 2215
2206 if (args != NULL && PyTuple_Check(args) && 2216 if (args != NULL && PyTuple_Check(args) &&
2207 (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) { 2217 (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) {
2208 PyErr_SetString(PyExc_TypeError, 2218 PyErr_SetString(PyExc_TypeError,
2209 "type.__init__() takes 1 or 3 arguments"); 2219 "type.__init__() takes 1 or 3 arguments");
2210 return -1; 2220 return -1;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
2257 "of the metaclasses of all its bases"); 2267 "of the metaclasses of all its bases");
2258 return NULL; 2268 return NULL;
2259 } 2269 }
2260 return winner; 2270 return winner;
2261 } 2271 }
2262 2272
2263 static PyObject * 2273 static PyObject *
2264 type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) 2274 type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
2265 { 2275 {
2266 PyObject *name, *bases = NULL, *orig_dict, *dict = NULL; 2276 PyObject *name, *bases = NULL, *orig_dict, *dict = NULL;
2267 static char *kwlist[] = {"name", "bases", "dict", 0};
2268 PyObject *qualname, *slots = NULL, *tmp, *newslots; 2277 PyObject *qualname, *slots = NULL, *tmp, *newslots;
2269 PyTypeObject *type = NULL, *base, *tmptype, *winner; 2278 PyTypeObject *type = NULL, *base, *tmptype, *winner;
2270 PyHeapTypeObject *et; 2279 PyHeapTypeObject *et;
2271 PyMemberDef *mp; 2280 PyMemberDef *mp;
2272 Py_ssize_t i, nbases, nslots, slotoffset, name_size; 2281 Py_ssize_t i, nbases, nslots, slotoffset, name_size;
2273 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;
2274 _Py_IDENTIFIER(__qualname__); 2283 _Py_IDENTIFIER(__qualname__);
2275 _Py_IDENTIFIER(__slots__); 2284 _Py_IDENTIFIER(__slots__);
2276 2285
2277 assert(args != NULL && PyTuple_Check(args)); 2286 assert(args != NULL && PyTuple_Check(args));
2278 assert(kwds == NULL || PyDict_Check(kwds)); 2287 assert(kwds == NULL || PyDict_Check(kwds));
2279 2288
2280 /* Special case: type(x) should return x->ob_type */ 2289 /* Special case: type(x) should return x->ob_type */
2281 { 2290 {
2282 const Py_ssize_t nargs = PyTuple_GET_SIZE(args); 2291 const Py_ssize_t nargs = PyTuple_GET_SIZE(args);
2283 const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds); 2292 const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
2284 2293
2285 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) { 2294 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
2286 PyObject *x = PyTuple_GET_ITEM(args, 0); 2295 PyObject *x = PyTuple_GET_ITEM(args, 0);
2287 Py_INCREF(Py_TYPE(x)); 2296 Py_INCREF(Py_TYPE(x));
2288 return (PyObject *) Py_TYPE(x); 2297 return (PyObject *) Py_TYPE(x);
2289 } 2298 }
2290 2299
2291 /* SF bug 475327 -- if that didn't trigger, we need 3 2300 /* SF bug 475327 -- if that didn't trigger, we need 3
2292 arguments. but PyArg_ParseTupleAndKeywords below may give 2301 arguments. but PyArg_ParseTupleAndKeywords below may give
2293 a msg saying type() needs exactly 3. */ 2302 a msg saying type() needs exactly 3. */
2294 if (nargs + nkwds != 3) { 2303 if (nargs != 3) {
2295 PyErr_SetString(PyExc_TypeError, 2304 PyErr_SetString(PyExc_TypeError,
2296 "type() takes 1 or 3 arguments"); 2305 "type() takes 1 or 3 arguments");
2297 return NULL; 2306 return NULL;
2298 } 2307 }
2299 } 2308 }
2300 2309
2301 /* Check arguments: (name, bases, dict) */ 2310 /* Check arguments: (name, bases, dict) */
2302 if (!PyArg_ParseTupleAndKeywords(args, kwds, "UO!O!:type", kwlist, 2311 if (!PyArg_ParseTuple(args, "UO!O!:type", &name, &PyTuple_Type, &bases,
2303 &name, 2312 &PyDict_Type, &orig_dict))
2304 &PyTuple_Type, &bases,
2305 &PyDict_Type, &orig_dict))
2306 return NULL; 2313 return NULL;
2307 2314
2308 /* Determine the proper metatype to deal with this: */ 2315 /* Determine the proper metatype to deal with this: */
2309 winner = _PyType_CalculateMetaclass(metatype, bases); 2316 winner = _PyType_CalculateMetaclass(metatype, bases);
2310 if (winner == NULL) { 2317 if (winner == NULL) {
2311 return NULL; 2318 return NULL;
2312 } 2319 }
2313 2320
2314 if (winner != metatype) { 2321 if (winner != metatype) {
2315 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
2568 } 2575 }
2569 } 2576 }
2570 2577
2571 /* Special-case __new__: if it's a plain function, 2578 /* Special-case __new__: if it's a plain function,
2572 make it a static function */ 2579 make it a static function */
2573 tmp = _PyDict_GetItemId(dict, &PyId___new__); 2580 tmp = _PyDict_GetItemId(dict, &PyId___new__);
2574 if (tmp != NULL && PyFunction_Check(tmp)) { 2581 if (tmp != NULL && PyFunction_Check(tmp)) {
2575 tmp = PyStaticMethod_New(tmp); 2582 tmp = PyStaticMethod_New(tmp);
2576 if (tmp == NULL) 2583 if (tmp == NULL)
2577 goto error; 2584 goto error;
2578 if (_PyDict_SetItemId(dict, &PyId___new__, tmp) < 0) 2585 if (_PyDict_SetItemId(dict, &PyId___new__, tmp) < 0) {
2586 Py_DECREF(tmp);
2579 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 }
2580 Py_DECREF(tmp); 2603 Py_DECREF(tmp);
2581 } 2604 }
2582 2605
2583 /* Add descriptors for custom slots from __slots__, or for __dict__ */ 2606 /* Add descriptors for custom slots from __slots__, or for __dict__ */
2584 mp = PyHeapType_GET_MEMBERS(et); 2607 mp = PyHeapType_GET_MEMBERS(et);
2585 slotoffset = base->tp_basicsize; 2608 slotoffset = base->tp_basicsize;
2586 if (et->ht_slots != NULL) { 2609 if (et->ht_slots != NULL) {
2587 for (i = 0; i < nslots; i++, mp++) { 2610 for (i = 0; i < nslots; i++, mp++) {
2588 mp->name = _PyUnicode_AsString( 2611 mp->name = _PyUnicode_AsString(
2589 PyTuple_GET_ITEM(et->ht_slots, i)); 2612 PyTuple_GET_ITEM(et->ht_slots, i));
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
2653 if (PyType_Ready(type) < 0) 2676 if (PyType_Ready(type) < 0)
2654 goto error; 2677 goto error;
2655 2678
2656 /* Put the proper slots in place */ 2679 /* Put the proper slots in place */
2657 fixup_slot_dispatchers(type); 2680 fixup_slot_dispatchers(type);
2658 2681
2659 if (type->tp_dictoffset) { 2682 if (type->tp_dictoffset) {
2660 et->ht_cached_keys = _PyDict_NewKeysForClass(); 2683 et->ht_cached_keys = _PyDict_NewKeysForClass();
2661 } 2684 }
2662 2685
2686 if (set_names(type) < 0)
2687 goto error;
2688
2689 if (init_subclass(type, kwds) < 0)
2690 goto error;
2691
2663 Py_DECREF(dict); 2692 Py_DECREF(dict);
2664 return (PyObject *)type; 2693 return (PyObject *)type;
2665 2694
2666 error: 2695 error:
2667 Py_XDECREF(dict); 2696 Py_XDECREF(dict);
2668 Py_XDECREF(bases); 2697 Py_XDECREF(bases);
2669 Py_XDECREF(slots); 2698 Py_XDECREF(slots);
2670 Py_XDECREF(type); 2699 Py_XDECREF(type);
2671 return NULL; 2700 return NULL;
2672 } 2701 }
2673 2702
2674 static short slotoffsets[] = { 2703 static const short slotoffsets[] = {
2675 -1, /* invalid slot */ 2704 -1, /* invalid slot */
2676 #include "typeslots.inc" 2705 #include "typeslots.inc"
2677 }; 2706 };
2678 2707
2679 PyObject * 2708 PyObject *
2680 PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases) 2709 PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
2681 { 2710 {
2682 PyHeapTypeObject *res = (PyHeapTypeObject*)PyType_GenericAlloc(&PyType_Type, 0); 2711 PyHeapTypeObject *res = (PyHeapTypeObject*)PyType_GenericAlloc(&PyType_Type, 0);
2683 PyTypeObject *type, *base; 2712 PyTypeObject *type, *base;
2684 PyObject *modname; 2713 PyObject *modname;
(...skipping 898 matching lines...) Expand 10 before | Expand all | Expand 10 after
3583 slots_b = ((PyHeapTypeObject *)b)->ht_slots; 3612 slots_b = ((PyHeapTypeObject *)b)->ht_slots;
3584 if (slots_a && slots_b) { 3613 if (slots_a && slots_b) {
3585 if (PyObject_RichCompareBool(slots_a, slots_b, Py_EQ) != 1) 3614 if (PyObject_RichCompareBool(slots_a, slots_b, Py_EQ) != 1)
3586 return 0; 3615 return 0;
3587 size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a); 3616 size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a);
3588 } 3617 }
3589 return size == a->tp_basicsize && size == b->tp_basicsize; 3618 return size == a->tp_basicsize && size == b->tp_basicsize;
3590 } 3619 }
3591 3620
3592 static int 3621 static int
3593 compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr) 3622 compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, const char* attr)
3594 { 3623 {
3595 PyTypeObject *newbase, *oldbase; 3624 PyTypeObject *newbase, *oldbase;
3596 3625
3597 if (newto->tp_free != oldto->tp_free) { 3626 if (newto->tp_free != oldto->tp_free) {
3598 PyErr_Format(PyExc_TypeError, 3627 PyErr_Format(PyExc_TypeError,
3599 "%s assignment: " 3628 "%s assignment: "
3600 "'%s' deallocator differs from '%s'", 3629 "'%s' deallocator differs from '%s'",
3601 attr, 3630 attr,
3602 newto->tp_name, 3631 newto->tp_name,
3603 oldto->tp_name); 3632 oldto->tp_name);
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after
3912 } 3941 }
3913 else { 3942 else {
3914 int err = PyDict_SetItem(slots, name, value); 3943 int err = PyDict_SetItem(slots, name, value);
3915 Py_DECREF(name); 3944 Py_DECREF(name);
3916 Py_DECREF(value); 3945 Py_DECREF(value);
3917 if (err) { 3946 if (err) {
3918 goto error; 3947 goto error;
3919 } 3948 }
3920 } 3949 }
3921 3950
3922 /* 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
3923 iterate over it */ 3952 iterate over it */
3924 if (slotnames_size != Py_SIZE(slotnames)) { 3953 if (slotnames_size != Py_SIZE(slotnames)) {
3925 PyErr_Format(PyExc_RuntimeError, 3954 PyErr_Format(PyExc_RuntimeError,
3926 "__slotsname__ changed size during iteration"); 3955 "__slotsname__ changed size during iteration");
3927 goto error; 3956 goto error;
3928 } 3957 }
3929 3958
3930 /* We handle errors within the loop here. */ 3959 /* We handle errors within the loop here. */
3931 if (0) { 3960 if (0) {
3932 error: 3961 error:
3933 Py_DECREF(slotnames); 3962 Py_DECREF(slotnames);
3934 Py_DECREF(slots); 3963 Py_DECREF(slots);
3935 Py_DECREF(state); 3964 Py_DECREF(state);
3936 return NULL; 3965 return NULL;
3937 } 3966 }
3938 } 3967 }
3939 3968
3940 /* 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
3941 the orginal attribute dictionary. */ 3970 the original attribute dictionary. */
3942 if (PyDict_Size(slots) > 0) { 3971 if (PyDict_Size(slots) > 0) {
3943 PyObject *state2; 3972 PyObject *state2;
3944 3973
3945 state2 = PyTuple_Pack(2, state, slots); 3974 state2 = PyTuple_Pack(2, state, slots);
3946 Py_DECREF(state); 3975 Py_DECREF(state);
3947 if (state2 == NULL) { 3976 if (state2 == NULL) {
3948 Py_DECREF(slotnames); 3977 Py_DECREF(slotnames);
3949 Py_DECREF(slots); 3978 Py_DECREF(slots);
3950 return NULL; 3979 return NULL;
3951 } 3980 }
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
4046 Py_CLEAR(*args); 4075 Py_CLEAR(*args);
4047 return -1; 4076 return -1;
4048 } 4077 }
4049 *kwargs = NULL; 4078 *kwargs = NULL;
4050 return 0; 4079 return 0;
4051 } else if (PyErr_Occurred()) { 4080 } else if (PyErr_Occurred()) {
4052 return -1; 4081 return -1;
4053 } 4082 }
4054 4083
4055 /* The object does not have __getnewargs_ex__ and __getnewargs__. This may 4084 /* The object does not have __getnewargs_ex__ and __getnewargs__. This may
4056 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
4057 object does not implement the reduce protocol for pickling or 4086 object does not implement the reduce protocol for pickling or
4058 copying. */ 4087 copying. */
4059 *args = NULL; 4088 *args = NULL;
4060 *kwargs = NULL; 4089 *kwargs = NULL;
4061 return 0; 4090 return 0;
4062 } 4091 }
4063 4092
4064 Py_LOCAL(int) 4093 Py_LOCAL(int)
4065 _PyObject_GetItemsIter(PyObject *obj, PyObject **listitems, 4094 _PyObject_GetItemsIter(PyObject *obj, PyObject **listitems,
4066 PyObject **dictitems) 4095 PyObject **dictitems)
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
4100 return -1; 4129 return -1;
4101 } 4130 }
4102 } 4131 }
4103 4132
4104 assert(*listitems != NULL && *dictitems != NULL); 4133 assert(*listitems != NULL && *dictitems != NULL);
4105 4134
4106 return 0; 4135 return 0;
4107 } 4136 }
4108 4137
4109 static PyObject * 4138 static PyObject *
4110 reduce_newobj(PyObject *obj, int proto) 4139 reduce_newobj(PyObject *obj)
4111 { 4140 {
4112 PyObject *args = NULL, *kwargs = NULL; 4141 PyObject *args = NULL, *kwargs = NULL;
4113 PyObject *copyreg; 4142 PyObject *copyreg;
4114 PyObject *newobj, *newargs, *state, *listitems, *dictitems; 4143 PyObject *newobj, *newargs, *state, *listitems, *dictitems;
4115 PyObject *result; 4144 PyObject *result;
4116 int hasargs; 4145 int hasargs;
4117 4146
4118 if (Py_TYPE(obj)->tp_new == NULL) { 4147 if (Py_TYPE(obj)->tp_new == NULL) {
4119 PyErr_Format(PyExc_TypeError, 4148 PyErr_Format(PyExc_TypeError,
4120 "can't pickle %.200s objects", 4149 "can't pickle %.200s objects",
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
4153 cls = (PyObject *) Py_TYPE(obj); 4182 cls = (PyObject *) Py_TYPE(obj);
4154 Py_INCREF(cls); 4183 Py_INCREF(cls);
4155 PyTuple_SET_ITEM(newargs, 0, cls); 4184 PyTuple_SET_ITEM(newargs, 0, cls);
4156 for (i = 0; i < n; i++) { 4185 for (i = 0; i < n; i++) {
4157 PyObject *v = PyTuple_GET_ITEM(args, i); 4186 PyObject *v = PyTuple_GET_ITEM(args, i);
4158 Py_INCREF(v); 4187 Py_INCREF(v);
4159 PyTuple_SET_ITEM(newargs, i+1, v); 4188 PyTuple_SET_ITEM(newargs, i+1, v);
4160 } 4189 }
4161 Py_XDECREF(args); 4190 Py_XDECREF(args);
4162 } 4191 }
4163 else if (proto >= 4) { 4192 else {
4164 _Py_IDENTIFIER(__newobj_ex__); 4193 _Py_IDENTIFIER(__newobj_ex__);
4165 4194
4166 newobj = _PyObject_GetAttrId(copyreg, &PyId___newobj_ex__); 4195 newobj = _PyObject_GetAttrId(copyreg, &PyId___newobj_ex__);
4167 Py_DECREF(copyreg); 4196 Py_DECREF(copyreg);
4168 if (newobj == NULL) { 4197 if (newobj == NULL) {
4169 Py_DECREF(args); 4198 Py_DECREF(args);
4170 Py_DECREF(kwargs); 4199 Py_DECREF(kwargs);
4171 return NULL; 4200 return NULL;
4172 } 4201 }
4173 newargs = PyTuple_Pack(3, Py_TYPE(obj), args, kwargs); 4202 newargs = PyTuple_Pack(3, Py_TYPE(obj), args, kwargs);
4174 Py_DECREF(args); 4203 Py_DECREF(args);
4175 Py_DECREF(kwargs); 4204 Py_DECREF(kwargs);
4176 if (newargs == NULL) { 4205 if (newargs == NULL) {
4177 Py_DECREF(newobj); 4206 Py_DECREF(newobj);
4178 return NULL; 4207 return NULL;
4179 } 4208 }
4180 }
4181 else {
4182 PyErr_SetString(PyExc_ValueError,
4183 "must use protocol 4 or greater to copy this "
4184 "object; since __getnewargs_ex__ returned "
4185 "keyword arguments.");
4186 Py_DECREF(args);
4187 Py_DECREF(kwargs);
4188 Py_DECREF(copyreg);
4189 return NULL;
4190 } 4209 }
4191 4210
4192 state = _PyObject_GetState(obj, 4211 state = _PyObject_GetState(obj,
4193 !hasargs && !PyList_Check(obj) && !PyDict_Check(obj)); 4212 !hasargs && !PyList_Check(obj) && !PyDict_Check(obj));
4194 if (state == NULL) { 4213 if (state == NULL) {
4195 Py_DECREF(newobj); 4214 Py_DECREF(newobj);
4196 Py_DECREF(newargs); 4215 Py_DECREF(newargs);
4197 return NULL; 4216 return NULL;
4198 } 4217 }
4199 if (_PyObject_GetItemsIter(obj, &listitems, &dictitems) < 0) { 4218 if (_PyObject_GetItemsIter(obj, &listitems, &dictitems) < 0) {
(...skipping 26 matching lines...) Expand all
4226 * behave in the same way, even if this is not required by the pickle 4245 * behave in the same way, even if this is not required by the pickle
4227 * protocol. This common functionality was moved to the _common_reduce 4246 * protocol. This common functionality was moved to the _common_reduce
4228 * function. 4247 * function.
4229 */ 4248 */
4230 static PyObject * 4249 static PyObject *
4231 _common_reduce(PyObject *self, int proto) 4250 _common_reduce(PyObject *self, int proto)
4232 { 4251 {
4233 PyObject *copyreg, *res; 4252 PyObject *copyreg, *res;
4234 4253
4235 if (proto >= 2) 4254 if (proto >= 2)
4236 return reduce_newobj(self, proto); 4255 return reduce_newobj(self);
4237 4256
4238 copyreg = import_copyreg(); 4257 copyreg = import_copyreg();
4239 if (!copyreg) 4258 if (!copyreg)
4240 return NULL; 4259 return NULL;
4241 4260
4242 res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto); 4261 res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto);
4243 Py_DECREF(copyreg); 4262 Py_DECREF(copyreg);
4244 4263
4245 return res; 4264 return res;
4246 } 4265 }
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
4307 Py_RETURN_NOTIMPLEMENTED; 4326 Py_RETURN_NOTIMPLEMENTED;
4308 } 4327 }
4309 4328
4310 PyDoc_STRVAR(object_subclasshook_doc, 4329 PyDoc_STRVAR(object_subclasshook_doc,
4311 "Abstract classes can override this to customize issubclass().\n" 4330 "Abstract classes can override this to customize issubclass().\n"
4312 "\n" 4331 "\n"
4313 "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n" 4332 "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n"
4314 "It should return True, False or NotImplemented. If it returns\n" 4333 "It should return True, False or NotImplemented. If it returns\n"
4315 "NotImplemented, the normal algorithm is used. Otherwise, it\n" 4334 "NotImplemented, the normal algorithm is used. Otherwise, it\n"
4316 "overrides the normal algorithm (and the outcome is cached).\n"); 4335 "overrides the normal algorithm (and the outcome is cached).\n");
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");
4317 4348
4318 /* 4349 /*
4319 from PEP 3101, this code implements: 4350 from PEP 3101, this code implements:
4320 4351
4321 class object: 4352 class object:
4322 def __format__(self, format_spec): 4353 def __format__(self, format_spec):
4323 return format(str(self), format_spec) 4354 return format(str(self), format_spec)
4324 */ 4355 */
4325 static PyObject * 4356 static PyObject *
4326 object_format(PyObject *self, PyObject *args) 4357 object_format(PyObject *self, PyObject *args)
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
4412 return result; 4443 return result;
4413 } 4444 }
4414 4445
4415 static PyMethodDef object_methods[] = { 4446 static PyMethodDef object_methods[] = {
4416 {"__reduce_ex__", object_reduce_ex, METH_VARARGS, 4447 {"__reduce_ex__", object_reduce_ex, METH_VARARGS,
4417 PyDoc_STR("helper for pickle")}, 4448 PyDoc_STR("helper for pickle")},
4418 {"__reduce__", object_reduce, METH_VARARGS, 4449 {"__reduce__", object_reduce, METH_VARARGS,
4419 PyDoc_STR("helper for pickle")}, 4450 PyDoc_STR("helper for pickle")},
4420 {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS, 4451 {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS,
4421 object_subclasshook_doc}, 4452 object_subclasshook_doc},
4453 {"__init_subclass__", object_init_subclass, METH_CLASS | METH_NOARGS,
4454 object_init_subclass_doc},
4422 {"__format__", object_format, METH_VARARGS, 4455 {"__format__", object_format, METH_VARARGS,
4423 PyDoc_STR("default object formatter")}, 4456 PyDoc_STR("default object formatter")},
4424 {"__sizeof__", object_sizeof, METH_NOARGS, 4457 {"__sizeof__", object_sizeof, METH_NOARGS,
4425 PyDoc_STR("__sizeof__() -> int\nsize of object in memory, in bytes")}, 4458 PyDoc_STR("__sizeof__() -> int\nsize of object in memory, in bytes")},
4426 {"__dir__", object_dir, METH_NOARGS, 4459 {"__dir__", object_dir, METH_NOARGS,
4427 PyDoc_STR("__dir__() -> list\ndefault dir() implementation")}, 4460 PyDoc_STR("__dir__() -> list\ndefault dir() implementation")},
4428 {0} 4461 {0}
4429 }; 4462 };
4430 4463
4431 4464
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
4518 { 4551 {
4519 PyObject *dict = type->tp_dict; 4552 PyObject *dict = type->tp_dict;
4520 4553
4521 for (; memb->name != NULL; memb++) { 4554 for (; memb->name != NULL; memb++) {
4522 PyObject *descr; 4555 PyObject *descr;
4523 if (PyDict_GetItemString(dict, memb->name)) 4556 if (PyDict_GetItemString(dict, memb->name))
4524 continue; 4557 continue;
4525 descr = PyDescr_NewMember(type, memb); 4558 descr = PyDescr_NewMember(type, memb);
4526 if (descr == NULL) 4559 if (descr == NULL)
4527 return -1; 4560 return -1;
4528 if (PyDict_SetItemString(dict, memb->name, descr) < 0) 4561 if (PyDict_SetItemString(dict, memb->name, descr) < 0) {
4562 Py_DECREF(descr);
4529 return -1; 4563 return -1;
4564 }
4530 Py_DECREF(descr); 4565 Py_DECREF(descr);
4531 } 4566 }
4532 return 0; 4567 return 0;
4533 } 4568 }
4534 4569
4535 static int 4570 static int
4536 add_getset(PyTypeObject *type, PyGetSetDef *gsp) 4571 add_getset(PyTypeObject *type, PyGetSetDef *gsp)
4537 { 4572 {
4538 PyObject *dict = type->tp_dict; 4573 PyObject *dict = type->tp_dict;
4539 4574
4540 for (; gsp->name != NULL; gsp++) { 4575 for (; gsp->name != NULL; gsp++) {
4541 PyObject *descr; 4576 PyObject *descr;
4542 if (PyDict_GetItemString(dict, gsp->name)) 4577 if (PyDict_GetItemString(dict, gsp->name))
4543 continue; 4578 continue;
4544 descr = PyDescr_NewGetSet(type, gsp); 4579 descr = PyDescr_NewGetSet(type, gsp);
4545 4580
4546 if (descr == NULL) 4581 if (descr == NULL)
4547 return -1; 4582 return -1;
4548 if (PyDict_SetItemString(dict, gsp->name, descr) < 0) 4583 if (PyDict_SetItemString(dict, gsp->name, descr) < 0) {
4584 Py_DECREF(descr);
4549 return -1; 4585 return -1;
4586 }
4550 Py_DECREF(descr); 4587 Py_DECREF(descr);
4551 } 4588 }
4552 return 0; 4589 return 0;
4553 } 4590 }
4554 4591
4555 static void 4592 static void
4556 inherit_special(PyTypeObject *type, PyTypeObject *base) 4593 inherit_special(PyTypeObject *type, PyTypeObject *base)
4557 { 4594 {
4558 4595
4559 /* Copying basicsize is connected to the GC flags */ 4596 /* Copying basicsize is connected to the GC flags */
(...skipping 793 matching lines...) Expand 10 before | Expand all | Expand 10 after
5353 res = (*func)(self, key, NULL); 5390 res = (*func)(self, key, NULL);
5354 if (res == -1 && PyErr_Occurred()) 5391 if (res == -1 && PyErr_Occurred())
5355 return NULL; 5392 return NULL;
5356 Py_INCREF(Py_None); 5393 Py_INCREF(Py_None);
5357 return Py_None; 5394 return Py_None;
5358 } 5395 }
5359 5396
5360 /* Helper to check for object.__setattr__ or __delattr__ applied to a type. 5397 /* Helper to check for object.__setattr__ or __delattr__ applied to a type.
5361 This is called the Carlo Verre hack after its discoverer. */ 5398 This is called the Carlo Verre hack after its discoverer. */
5362 static int 5399 static int
5363 hackcheck(PyObject *self, setattrofunc func, char *what) 5400 hackcheck(PyObject *self, setattrofunc func, const char *what)
5364 { 5401 {
5365 PyTypeObject *type = Py_TYPE(self); 5402 PyTypeObject *type = Py_TYPE(self);
5366 while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE) 5403 while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
5367 type = type->tp_base; 5404 type = type->tp_base;
5368 /* If type is NULL now, this is a really weird type. 5405 /* If type is NULL now, this is a really weird type.
5369 In the spirit of backwards compatibility (?), just shut up. */ 5406 In the spirit of backwards compatibility (?), just shut up. */
5370 if (type && type->tp_setattro != func) { 5407 if (type && type->tp_setattro != func) {
5371 PyErr_Format(PyExc_TypeError, 5408 PyErr_Format(PyExc_TypeError,
5372 "can't apply this %s to %s object", 5409 "can't apply this %s to %s object",
5373 what, 5410 what,
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after
5773 if (func == NULL) { 5810 if (func == NULL) {
5774 return NULL; 5811 return NULL;
5775 } 5812 }
5776 } 5813 }
5777 ival = PyLong_FromSsize_t(i); 5814 ival = PyLong_FromSsize_t(i);
5778 if (ival != NULL) { 5815 if (ival != NULL) {
5779 args = PyTuple_New(1); 5816 args = PyTuple_New(1);
5780 if (args != NULL) { 5817 if (args != NULL) {
5781 PyTuple_SET_ITEM(args, 0, ival); 5818 PyTuple_SET_ITEM(args, 0, ival);
5782 retval = PyObject_Call(func, args, NULL); 5819 retval = PyObject_Call(func, args, NULL);
5783 Py_XDECREF(args); 5820 Py_DECREF(args);
5784 Py_XDECREF(func); 5821 Py_DECREF(func);
5785 return retval; 5822 return retval;
5786 } 5823 }
5787 } 5824 }
5788 } 5825 }
5789 else { 5826 else {
5790 PyObject *getitem_str = _PyUnicode_FromId(&PyId___getitem__); 5827 PyObject *getitem_str = _PyUnicode_FromId(&PyId___getitem__);
5791 PyErr_SetObject(PyExc_AttributeError, getitem_str); 5828 PyErr_SetObject(PyExc_AttributeError, getitem_str);
5792 } 5829 }
5793 Py_XDECREF(args); 5830 Py_XDECREF(args);
5794 Py_XDECREF(ival); 5831 Py_XDECREF(ival);
(...skipping 20 matching lines...) Expand all
5815 slot_sq_contains(PyObject *self, PyObject *value) 5852 slot_sq_contains(PyObject *self, PyObject *value)
5816 { 5853 {
5817 PyObject *func, *res, *args; 5854 PyObject *func, *res, *args;
5818 int result = -1; 5855 int result = -1;
5819 _Py_IDENTIFIER(__contains__); 5856 _Py_IDENTIFIER(__contains__);
5820 5857
5821 func = lookup_maybe(self, &PyId___contains__); 5858 func = lookup_maybe(self, &PyId___contains__);
5822 if (func == Py_None) { 5859 if (func == Py_None) {
5823 Py_DECREF(func); 5860 Py_DECREF(func);
5824 PyErr_Format(PyExc_TypeError, 5861 PyErr_Format(PyExc_TypeError,
5825 "argument of type '%.200s' is not a container", 5862 "'%.200s' object is not a container",
5826 Py_TYPE(self)->tp_name); 5863 Py_TYPE(self)->tp_name);
5827 return -1; 5864 return -1;
5828 } 5865 }
5829 if (func != NULL) { 5866 if (func != NULL) {
5830 args = PyTuple_Pack(1, value); 5867 args = PyTuple_Pack(1, value);
5831 if (args == NULL) 5868 if (args == NULL)
5832 res = NULL; 5869 res = NULL;
5833 else { 5870 else {
5834 res = PyObject_Call(func, args, NULL); 5871 res = PyObject_Call(func, args, NULL);
5835 Py_DECREF(args); 5872 Py_DECREF(args);
(...skipping 1095 matching lines...) Expand 10 before | Expand all | Expand 10 after
6931 { 6968 {
6932 slotdef *p; 6969 slotdef *p;
6933 6970
6934 init_slotdefs(); 6971 init_slotdefs();
6935 for (p = slotdefs; p->name; p++) { 6972 for (p = slotdefs; p->name; p++) {
6936 /* update_slot returns int but can't actually fail */ 6973 /* update_slot returns int but can't actually fail */
6937 update_slot(type, p->name_strobj); 6974 update_slot(type, p->name_strobj);
6938 } 6975 }
6939 } 6976 }
6940 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
6941 /* recurse_down_subclasses() and update_subclasses() are mutually 7026 /* recurse_down_subclasses() and update_subclasses() are mutually
6942 recursive functions to call a callback for all subclasses, 7027 recursive functions to call a callback for all subclasses,
6943 but refraining from recursing into subclasses that define 'name'. */ 7028 but refraining from recursing into subclasses that define 'name'. */
6944 7029
6945 static int 7030 static int
6946 update_subclasses(PyTypeObject *type, PyObject *name, 7031 update_subclasses(PyTypeObject *type, PyObject *name,
6947 update_callback callback, void *data) 7032 update_callback callback, void *data)
6948 { 7033 {
6949 if (callback(type, data) < 0) 7034 if (callback(type, data) < 0)
6950 return -1; 7035 return -1;
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
7033 /* Classes may prevent the inheritance of the tp_hash 7118 /* Classes may prevent the inheritance of the tp_hash
7034 slot by storing PyObject_HashNotImplemented in it. Make it 7119 slot by storing PyObject_HashNotImplemented in it. Make it
7035 visible as a None value for the __hash__ attribute. */ 7120 visible as a None value for the __hash__ attribute. */
7036 if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0) 7121 if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0)
7037 return -1; 7122 return -1;
7038 } 7123 }
7039 else { 7124 else {
7040 descr = PyDescr_NewWrapper(type, p, *ptr); 7125 descr = PyDescr_NewWrapper(type, p, *ptr);
7041 if (descr == NULL) 7126 if (descr == NULL)
7042 return -1; 7127 return -1;
7043 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) 7128 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) {
7129 Py_DECREF(descr);
7044 return -1; 7130 return -1;
7131 }
7045 Py_DECREF(descr); 7132 Py_DECREF(descr);
7046 } 7133 }
7047 } 7134 }
7048 if (type->tp_new != NULL) { 7135 if (type->tp_new != NULL) {
7049 if (add_tp_new_wrapper(type) < 0) 7136 if (add_tp_new_wrapper(type) < 0)
7050 return -1; 7137 return -1;
7051 } 7138 }
7052 return 0; 7139 return 0;
7053 } 7140 }
7054 7141
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after
7361 7448
7362 if (obj == Py_None) 7449 if (obj == Py_None)
7363 obj = NULL; 7450 obj = NULL;
7364 if (obj != NULL) { 7451 if (obj != NULL) {
7365 obj_type = supercheck(type, obj); 7452 obj_type = supercheck(type, obj);
7366 if (obj_type == NULL) 7453 if (obj_type == NULL)
7367 return -1; 7454 return -1;
7368 Py_INCREF(obj); 7455 Py_INCREF(obj);
7369 } 7456 }
7370 Py_INCREF(type); 7457 Py_INCREF(type);
7371 su->type = type; 7458 Py_XSETREF(su->type, type);
7372 su->obj = obj; 7459 Py_XSETREF(su->obj, obj);
7373 su->obj_type = obj_type; 7460 Py_XSETREF(su->obj_type, obj_type);
7374 return 0; 7461 return 0;
7375 } 7462 }
7376 7463
7377 PyDoc_STRVAR(super_doc, 7464 PyDoc_STRVAR(super_doc,
7378 "super() -> same as super(__class__, <first argument>)\n" 7465 "super() -> same as super(__class__, <first argument>)\n"
7379 "super(type) -> unbound super object\n" 7466 "super(type) -> unbound super object\n"
7380 "super(type, obj) -> bound super object; requires isinstance(obj, type)\n" 7467 "super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
7381 "super(type, type2) -> bound super object; requires issubclass(type2, type)\n" 7468 "super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
7382 "Typical use to call a cooperative superclass method:\n" 7469 "Typical use to call a cooperative superclass method:\n"
7383 "class C(B):\n" 7470 "class C(B):\n"
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
7437 0, /* tp_base */ 7524 0, /* tp_base */
7438 0, /* tp_dict */ 7525 0, /* tp_dict */
7439 super_descr_get, /* tp_descr_get */ 7526 super_descr_get, /* tp_descr_get */
7440 0, /* tp_descr_set */ 7527 0, /* tp_descr_set */
7441 0, /* tp_dictoffset */ 7528 0, /* tp_dictoffset */
7442 super_init, /* tp_init */ 7529 super_init, /* tp_init */
7443 PyType_GenericAlloc, /* tp_alloc */ 7530 PyType_GenericAlloc, /* tp_alloc */
7444 PyType_GenericNew, /* tp_new */ 7531 PyType_GenericNew, /* tp_new */
7445 PyObject_GC_Del, /* tp_free */ 7532 PyObject_GC_Del, /* tp_free */
7446 }; 7533 };
LEFTRIGHT

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