summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2016-12-05 06:47:55 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2016-12-05 06:47:55 (GMT)
commit19d246745d9d013c12e9560dd020d778381780fb (patch)
treea296697991d1f411c3ee76690c6549985744e85a /Objects
parent71c62e14aa27d73623427a0a626b1f20df309e43 (diff)
downloadcpython-19d246745d9d013c12e9560dd020d778381780fb.zip
cpython-19d246745d9d013c12e9560dd020d778381780fb.tar.gz
cpython-19d246745d9d013c12e9560dd020d778381780fb.tar.bz2
Issue #23722: improve __classcell__ compatibility
Handling zero-argument super() in __init_subclass__ and __set_name__ involved moving __class__ initialisation to type.__new__. This requires cooperation from custom metaclasses to ensure that the new __classcell__ entry is passed along appropriately. The initial implementation of that change resulted in abruptly broken zero-argument super() support in metaclasses that didn't adhere to the new requirements (such as Django's metaclass for Model definitions). The updated approach adopted here instead emits a deprecation warning for those cases, and makes them work the same way they did in Python 3.5. This patch also improves the related class machinery documentation to cover these details and to include more reader-friendly cross-references and index entries.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/typeobject.c11
1 files changed, 9 insertions, 2 deletions
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 04da32b..329261b 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -2687,9 +2687,16 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
else
type->tp_free = PyObject_Del;
- /* store type in class' cell */
+ /* store type in class' cell if one is supplied */
cell = _PyDict_GetItemId(dict, &PyId___classcell__);
- if (cell != NULL && PyCell_Check(cell)) {
+ if (cell != NULL) {
+ /* At least one method requires a reference to its defining class */
+ if (!PyCell_Check(cell)) {
+ PyErr_Format(PyExc_TypeError,
+ "__classcell__ must be a nonlocal cell, not %.200R",
+ Py_TYPE(cell));
+ goto error;
+ }
PyCell_Set(cell, (PyObject *) type);
_PyDict_DelItemId(dict, &PyId___classcell__);
PyErr_Clear();