summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-09-21 12:54:59 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2016-09-21 12:54:59 (GMT)
commitafd02a439f864c8380a8554ae6cb9726b996fd5e (patch)
tree0fc8e637e3f24b4ad1de6436b5568882f411c932
parentb00e00c339cec1438a3764001b994c0a274a49c1 (diff)
downloadcpython-afd02a439f864c8380a8554ae6cb9726b996fd5e.zip
cpython-afd02a439f864c8380a8554ae6cb9726b996fd5e.tar.gz
cpython-afd02a439f864c8380a8554ae6cb9726b996fd5e.tar.bz2
Issue #28214: Now __set_name__ is looked up on the class instead of the
instance.
-rw-r--r--Lib/test/test_subclassinit.py12
-rw-r--r--Misc/NEWS3
-rw-r--r--Objects/typeobject.c12
3 files changed, 22 insertions, 5 deletions
diff --git a/Lib/test/test_subclassinit.py b/Lib/test/test_subclassinit.py
index ea6de75..0267e41 100644
--- a/Lib/test/test_subclassinit.py
+++ b/Lib/test/test_subclassinit.py
@@ -148,6 +148,18 @@ class Test(unittest.TestCase):
class A:
d = Descriptor()
+ def test_set_name_lookup(self):
+ resolved = []
+ class NonDescriptor:
+ def __getattr__(self, name):
+ resolved.append(name)
+
+ class A:
+ d = NonDescriptor()
+
+ self.assertNotIn('__set_name__', resolved,
+ '__set_name__ is looked up in instance dict')
+
def test_set_name_init_subclass(self):
class Descriptor:
def __set_name__(self, owner, name):
diff --git a/Misc/NEWS b/Misc/NEWS
index bda0576..248da26 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.6.0 beta 2
Core and Builtins
-----------------
+- Issue #28214: Now __set_name__ is looked up on the class instead of the
+ instance.
+
- Issue #27955: Fallback on reading /dev/urandom device when the getrandom()
syscall fails with EPERM, for example when blocked by SECCOMP.
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 5028304..e799a57 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -6990,19 +6990,21 @@ update_all_slots(PyTypeObject* type)
static int
set_names(PyTypeObject *type)
{
- PyObject *key, *value, *tmp;
+ PyObject *key, *value, *set_name, *tmp;
Py_ssize_t i = 0;
while (PyDict_Next(type->tp_dict, &i, &key, &value)) {
- if (PyObject_HasAttr(value, _PyUnicode_FromId(&PyId___set_name__))) {
- tmp = PyObject_CallMethodObjArgs(
- value, _PyUnicode_FromId(&PyId___set_name__),
- type, key, NULL);
+ set_name = lookup_maybe(value, &PyId___set_name__);
+ if (set_name != NULL) {
+ tmp = PyObject_CallFunctionObjArgs(set_name, type, key, NULL);
+ Py_DECREF(set_name);
if (tmp == NULL)
return -1;
else
Py_DECREF(tmp);
}
+ else if (PyErr_Occurred())
+ return -1;
}
return 0;