diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2016-11-29 07:56:07 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2016-11-29 07:56:07 (GMT) |
commit | baa7223bcd64bc092c51548caaef1fb52782ae52 (patch) | |
tree | 30929588a468110b5959468d94babd257b2e79d4 | |
parent | 3d85fae91f82f27d239b51098fa90eee9d812ae4 (diff) | |
parent | 9ec07721f459d14131d6453ccb6d2a1a0b0c1959 (diff) | |
download | cpython-baa7223bcd64bc092c51548caaef1fb52782ae52.zip cpython-baa7223bcd64bc092c51548caaef1fb52782ae52.tar.gz cpython-baa7223bcd64bc092c51548caaef1fb52782ae52.tar.bz2 |
Issue #28797: Modifying the class __dict__ inside the __set_name__ method of
a descriptor that is used inside that class no longer prevents calling the
__set_name__ method of other descriptors.
-rw-r--r-- | Lib/test/test_subclassinit.py | 16 | ||||
-rw-r--r-- | Misc/NEWS | 4 | ||||
-rw-r--r-- | Objects/typeobject.c | 14 |
3 files changed, 31 insertions, 3 deletions
diff --git a/Lib/test/test_subclassinit.py b/Lib/test/test_subclassinit.py index 6a12fb1..3c331bb9 100644 --- a/Lib/test/test_subclassinit.py +++ b/Lib/test/test_subclassinit.py @@ -198,6 +198,22 @@ class Test(unittest.TestCase): self.assertIs(B.meta_owner, B) self.assertEqual(B.name, 'd') + def test_set_name_modifying_dict(self): + notified = [] + class Descriptor: + def __set_name__(self, owner, name): + setattr(owner, name + 'x', None) + notified.append(name) + + class A: + a = Descriptor() + b = Descriptor() + c = Descriptor() + d = Descriptor() + e = Descriptor() + + self.assertCountEqual(notified, ['a', 'b', 'c', 'd', 'e']) + def test_errors(self): class MyMeta(type): pass @@ -10,6 +10,10 @@ What's New in Python 3.7.0 alpha 1 Core and Builtins ----------------- +- Issue #28797: Modifying the class __dict__ inside the __set_name__ method of + a descriptor that is used inside that class no longer prevents calling the + __set_name__ method of other descriptors. + - Issue #28799: Remove the ``PyEval_GetCallStats()`` function and deprecate the untested and undocumented ``sys.callstats()`` function. Remove the ``CALL_PROFILE`` special build: use the :func:`sys.setprofile` function, diff --git a/Objects/typeobject.c b/Objects/typeobject.c index af09271..186c570 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -7004,10 +7004,14 @@ update_all_slots(PyTypeObject* type) static int set_names(PyTypeObject *type) { - PyObject *key, *value, *set_name, *tmp; + PyObject *names_to_set, *key, *value, *set_name, *tmp; Py_ssize_t i = 0; - while (PyDict_Next(type->tp_dict, &i, &key, &value)) { + names_to_set = PyDict_Copy(type->tp_dict); + if (names_to_set == NULL) + return -1; + + while (PyDict_Next(names_to_set, &i, &key, &value)) { set_name = lookup_maybe(value, &PyId___set_name__); if (set_name != NULL) { tmp = PyObject_CallFunctionObjArgs(set_name, type, key, NULL); @@ -7017,15 +7021,19 @@ set_names(PyTypeObject *type) "Error calling __set_name__ on '%.100s' instance %R " "in '%.100s'", value->ob_type->tp_name, key, type->tp_name); + Py_DECREF(names_to_set); return -1; } else Py_DECREF(tmp); } - else if (PyErr_Occurred()) + else if (PyErr_Occurred()) { + Py_DECREF(names_to_set); return -1; + } } + Py_DECREF(names_to_set); return 0; } |