Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit baa7223

Browse files
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.
2 parents 3d85fae + 9ec0772 commit baa7223

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

Lib/test/test_subclassinit.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,22 @@ class B(A, metaclass=Meta):
198198
self.assertIs(B.meta_owner, B)
199199
self.assertEqual(B.name, 'd')
200200

201+
def test_set_name_modifying_dict(self):
202+
notified = []
203+
class Descriptor:
204+
def __set_name__(self, owner, name):
205+
setattr(owner, name + 'x', None)
206+
notified.append(name)
207+
208+
class A:
209+
a = Descriptor()
210+
b = Descriptor()
211+
c = Descriptor()
212+
d = Descriptor()
213+
e = Descriptor()
214+
215+
self.assertCountEqual(notified, ['a', 'b', 'c', 'd', 'e'])
216+
201217
def test_errors(self):
202218
class MyMeta(type):
203219
pass

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ What's New in Python 3.7.0 alpha 1
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #28797: Modifying the class __dict__ inside the __set_name__ method of
14+
a descriptor that is used inside that class no longer prevents calling the
15+
__set_name__ method of other descriptors.
16+
1317
- Issue #28799: Remove the ``PyEval_GetCallStats()`` function and deprecate
1418
the untested and undocumented ``sys.callstats()`` function. Remove the
1519
``CALL_PROFILE`` special build: use the :func:`sys.setprofile` function,

Objects/typeobject.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7004,10 +7004,14 @@ update_all_slots(PyTypeObject* type)
70047004
static int
70057005
set_names(PyTypeObject *type)
70067006
{
7007-
PyObject *key, *value, *set_name, *tmp;
7007+
PyObject *names_to_set, *key, *value, *set_name, *tmp;
70087008
Py_ssize_t i = 0;
70097009

7010-
while (PyDict_Next(type->tp_dict, &i, &key, &value)) {
7010+
names_to_set = PyDict_Copy(type->tp_dict);
7011+
if (names_to_set == NULL)
7012+
return -1;
7013+
7014+
while (PyDict_Next(names_to_set, &i, &key, &value)) {
70117015
set_name = lookup_maybe(value, &PyId___set_name__);
70127016
if (set_name != NULL) {
70137017
tmp = PyObject_CallFunctionObjArgs(set_name, type, key, NULL);
@@ -7017,15 +7021,19 @@ set_names(PyTypeObject *type)
70177021
"Error calling __set_name__ on '%.100s' instance %R "
70187022
"in '%.100s'",
70197023
value->ob_type->tp_name, key, type->tp_name);
7024+
Py_DECREF(names_to_set);
70207025
return -1;
70217026
}
70227027
else
70237028
Py_DECREF(tmp);
70247029
}
7025-
else if (PyErr_Occurred())
7030+
else if (PyErr_Occurred()) {
7031+
Py_DECREF(names_to_set);
70267032
return -1;
7033+
}
70277034
}
70287035

7036+
Py_DECREF(names_to_set);
70297037
return 0;
70307038
}
70317039

0 commit comments

Comments
 (0)