]> granicus.if.org Git - python/commitdiff
Issue #28797: Modifying the class __dict__ inside the __set_name__ method of
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 29 Nov 2016 07:54:17 +0000 (09:54 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Tue, 29 Nov 2016 07:54:17 +0000 (09:54 +0200)
 a descriptor that is used inside that class no longer prevents calling the
 __set_name__ method of other descriptors.

Lib/test/test_subclassinit.py
Misc/NEWS
Objects/typeobject.c

index 6a12fb1ee0f83d7aa24e9342ab00376225e64ffd..3c331bb98fc9368f7d95a1198102a1fd8b7c9b17 100644 (file)
@@ -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
index c950383ce5c1b253e3f1ea25d425c6e89769916c..35777356e055dc2c1295bb6514d0ccf20a128c28 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@ What's New in Python 3.6.0 release candidate 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 #28782: Fix a bug in the implementation ``yield from`` when checking
   if the next instruction is YIELD_FROM. Regression introduced by WORDCODE
   (issue #26647).
index 606e08e449e0d8528cf736a718c3db63af71f15c..04da32bdfad58c1517c5fe2df1cf35926ceb3d5d 100644 (file)
@@ -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;
 }