(original) (raw)
changeset: 103991:1a2b8398f045 branch: 3.6 parent: 103987:3ab4a374b5f0 user: Serhiy Storchaka storchaka@gmail.com date: Wed Sep 21 15:54:59 2016 +0300 files: Lib/test/test_subclassinit.py Misc/NEWS Objects/typeobject.c description: Issue #28214: Now __set_name__ is looked up on the class instead of the instance. diff -r 3ab4a374b5f0 -r 1a2b8398f045 Lib/test/test_subclassinit.py --- a/Lib/test/test_subclassinit.py Wed Sep 21 14:36:44 2016 +0200 +++ b/Lib/test/test_subclassinit.py Wed Sep 21 15:54:59 2016 +0300 @@ -148,6 +148,18 @@ 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 -r 3ab4a374b5f0 -r 1a2b8398f045 Misc/NEWS --- a/Misc/NEWS Wed Sep 21 14:36:44 2016 +0200 +++ b/Misc/NEWS Wed Sep 21 15:54:59 2016 +0300 @@ -10,6 +10,9 @@ 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 -r 3ab4a374b5f0 -r 1a2b8398f045 Objects/typeobject.c --- a/Objects/typeobject.c Wed Sep 21 14:36:44 2016 +0200 +++ b/Objects/typeobject.c Wed Sep 21 15:54:59 2016 +0300 @@ -6990,19 +6990,21 @@ 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; /storchaka@gmail.com