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

Side by Side Diff: Objects/typeobject.c

Issue 23722: During metaclass.__init__, super() of the constructed class does not work
Patch Set: Created 3 years, 5 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:
View unified diff | Download patch
OLDNEW
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 2252 matching lines...) Expand 10 before | Expand all | Expand 10 after
2263 return NULL; 2263 return NULL;
2264 } 2264 }
2265 return winner; 2265 return winner;
2266 } 2266 }
2267 2267
2268 static PyObject * 2268 static PyObject *
2269 type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) 2269 type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
2270 { 2270 {
2271 PyObject *name, *bases = NULL, *orig_dict, *dict = NULL; 2271 PyObject *name, *bases = NULL, *orig_dict, *dict = NULL;
2272 static char *kwlist[] = {"name", "bases", "dict", 0}; 2272 static char *kwlist[] = {"name", "bases", "dict", 0};
2273 PyObject *qualname, *slots = NULL, *tmp, *newslots; 2273 PyObject *qualname, *slots = NULL, *tmp, *newslots, *cell;
2274 PyTypeObject *type = NULL, *base, *tmptype, *winner; 2274 PyTypeObject *type = NULL, *base, *tmptype, *winner;
2275 PyHeapTypeObject *et; 2275 PyHeapTypeObject *et;
2276 PyMemberDef *mp; 2276 PyMemberDef *mp;
2277 Py_ssize_t i, nbases, nslots, slotoffset, name_size; 2277 Py_ssize_t i, nbases, nslots, slotoffset, name_size;
2278 int j, may_add_dict, may_add_weak, add_dict, add_weak; 2278 int j, may_add_dict, may_add_weak, add_dict, add_weak;
2279 _Py_IDENTIFIER(__qualname__); 2279 _Py_IDENTIFIER(__qualname__);
2280 _Py_IDENTIFIER(__slots__); 2280 _Py_IDENTIFIER(__slots__);
2281 _Py_IDENTIFIER(__classcell__);
2281 2282
2282 assert(args != NULL && PyTuple_Check(args)); 2283 assert(args != NULL && PyTuple_Check(args));
2283 assert(kwds == NULL || PyDict_Check(kwds)); 2284 assert(kwds == NULL || PyDict_Check(kwds));
2284 2285
2285 /* Special case: type(x) should return x->ob_type */ 2286 /* Special case: type(x) should return x->ob_type */
2286 { 2287 {
2287 const Py_ssize_t nargs = PyTuple_GET_SIZE(args); 2288 const Py_ssize_t nargs = PyTuple_GET_SIZE(args);
2288 const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds); 2289 const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
2289 2290
2290 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) { 2291 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
2537 if (qualname != NULL) { 2538 if (qualname != NULL) {
2538 if (!PyUnicode_Check(qualname)) { 2539 if (!PyUnicode_Check(qualname)) {
2539 PyErr_Format(PyExc_TypeError, 2540 PyErr_Format(PyExc_TypeError,
2540 "type __qualname__ must be a str, not %s", 2541 "type __qualname__ must be a str, not %s",
2541 Py_TYPE(qualname)->tp_name); 2542 Py_TYPE(qualname)->tp_name);
2542 goto error; 2543 goto error;
2543 } 2544 }
2544 } 2545 }
2545 et->ht_qualname = qualname ? qualname : et->ht_name; 2546 et->ht_qualname = qualname ? qualname : et->ht_name;
2546 Py_INCREF(et->ht_qualname); 2547 Py_INCREF(et->ht_qualname);
2547 if (qualname != NULL && PyDict_DelItem(dict, PyId___qualname__.object) < 0) 2548 if (qualname != NULL && _PyDict_DelItemId(dict, &PyId___qualname__) < 0)
2548 goto error; 2549 goto error;
2549 2550
2550 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there 2551 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
2551 and is a string. The __doc__ accessor will first look for tp_doc; 2552 and is a string. The __doc__ accessor will first look for tp_doc;
2552 if that fails, it will still look into __dict__. 2553 if that fails, it will still look into __dict__.
2553 */ 2554 */
2554 { 2555 {
2555 PyObject *doc = _PyDict_GetItemId(dict, &PyId___doc__); 2556 PyObject *doc = _PyDict_GetItemId(dict, &PyId___doc__);
2556 if (doc != NULL && PyUnicode_Check(doc)) { 2557 if (doc != NULL && PyUnicode_Check(doc)) {
2557 Py_ssize_t len; 2558 Py_ssize_t len;
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
2648 2649
2649 /* Always override allocation strategy to use regular heap */ 2650 /* Always override allocation strategy to use regular heap */
2650 type->tp_alloc = PyType_GenericAlloc; 2651 type->tp_alloc = PyType_GenericAlloc;
2651 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) { 2652 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
2652 type->tp_free = PyObject_GC_Del; 2653 type->tp_free = PyObject_GC_Del;
2653 type->tp_traverse = subtype_traverse; 2654 type->tp_traverse = subtype_traverse;
2654 type->tp_clear = subtype_clear; 2655 type->tp_clear = subtype_clear;
2655 } 2656 }
2656 else 2657 else
2657 type->tp_free = PyObject_Del; 2658 type->tp_free = PyObject_Del;
2659
2660 /* store type in class' cell */
2661 cell = _PyDict_GetItemId(dict, &PyId___classcell__);
tecki 2016/07/17 18:49:21 Here, at the end of type.__new__, the __classcell_
2662 if (cell != NULL && PyCell_Check(cell)) {
2663 PyCell_Set(cell, (PyObject *) type);
2664 _PyDict_DelItemId(dict, &PyId___classcell__);
2665 PyErr_Clear();
2666 }
2658 2667
2659 /* Initialize the rest */ 2668 /* Initialize the rest */
2660 if (PyType_Ready(type) < 0) 2669 if (PyType_Ready(type) < 0)
2661 goto error; 2670 goto error;
2662 2671
2663 /* Put the proper slots in place */ 2672 /* Put the proper slots in place */
2664 fixup_slot_dispatchers(type); 2673 fixup_slot_dispatchers(type);
2665 2674
2666 if (type->tp_dictoffset) { 2675 if (type->tp_dictoffset) {
2667 et->ht_cached_keys = _PyDict_NewKeysForClass(); 2676 et->ht_cached_keys = _PyDict_NewKeysForClass();
(...skipping 4758 matching lines...) Expand 10 before | Expand all | Expand 10 after
7426 0, /* tp_base */ 7435 0, /* tp_base */
7427 0, /* tp_dict */ 7436 0, /* tp_dict */
7428 super_descr_get, /* tp_descr_get */ 7437 super_descr_get, /* tp_descr_get */
7429 0, /* tp_descr_set */ 7438 0, /* tp_descr_set */
7430 0, /* tp_dictoffset */ 7439 0, /* tp_dictoffset */
7431 super_init, /* tp_init */ 7440 super_init, /* tp_init */
7432 PyType_GenericAlloc, /* tp_alloc */ 7441 PyType_GenericAlloc, /* tp_alloc */
7433 PyType_GenericNew, /* tp_new */ 7442 PyType_GenericNew, /* tp_new */
7434 PyObject_GC_Del, /* tp_free */ 7443 PyObject_GC_Del, /* tp_free */
7435 }; 7444 };
OLDNEW
« no previous file with comments | « Lib/test/test_super.py ('k') | Python/bltinmodule.c » ('j') | Python/bltinmodule.c » ('J')

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