mirror of https://github.com/python/cpython
Issue #28214: Now __set_name__ is looked up on the class instead of the
instance.
This commit is contained in:
commit
cf4fb40b9d
|
@ -148,6 +148,18 @@ class Test(unittest.TestCase):
|
||||||
class A:
|
class A:
|
||||||
d = Descriptor()
|
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):
|
def test_set_name_init_subclass(self):
|
||||||
class Descriptor:
|
class Descriptor:
|
||||||
def __set_name__(self, owner, name):
|
def __set_name__(self, owner, name):
|
||||||
|
|
|
@ -10,6 +10,9 @@ What's New in Python 3.7.0 alpha 1
|
||||||
Core and Builtins
|
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()
|
- Issue #27955: Fallback on reading /dev/urandom device when the getrandom()
|
||||||
syscall fails with EPERM, for example when blocked by SECCOMP.
|
syscall fails with EPERM, for example when blocked by SECCOMP.
|
||||||
|
|
||||||
|
|
|
@ -6990,19 +6990,21 @@ update_all_slots(PyTypeObject* type)
|
||||||
static int
|
static int
|
||||||
set_names(PyTypeObject *type)
|
set_names(PyTypeObject *type)
|
||||||
{
|
{
|
||||||
PyObject *key, *value, *tmp;
|
PyObject *key, *value, *set_name, *tmp;
|
||||||
Py_ssize_t i = 0;
|
Py_ssize_t i = 0;
|
||||||
|
|
||||||
while (PyDict_Next(type->tp_dict, &i, &key, &value)) {
|
while (PyDict_Next(type->tp_dict, &i, &key, &value)) {
|
||||||
if (PyObject_HasAttr(value, _PyUnicode_FromId(&PyId___set_name__))) {
|
set_name = lookup_maybe(value, &PyId___set_name__);
|
||||||
tmp = PyObject_CallMethodObjArgs(
|
if (set_name != NULL) {
|
||||||
value, _PyUnicode_FromId(&PyId___set_name__),
|
tmp = PyObject_CallFunctionObjArgs(set_name, type, key, NULL);
|
||||||
type, key, NULL);
|
Py_DECREF(set_name);
|
||||||
if (tmp == NULL)
|
if (tmp == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
else
|
else
|
||||||
Py_DECREF(tmp);
|
Py_DECREF(tmp);
|
||||||
}
|
}
|
||||||
|
else if (PyErr_Occurred())
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in New Issue