]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
complain when a class variable shadows a name in __slots__ (closes #12766)
authorBenjamin Peterson <benjamin@python.org>
Tue, 16 Aug 2011 23:53:26 +0000 (18:53 -0500)
committerBenjamin Peterson <benjamin@python.org>
Tue, 16 Aug 2011 23:53:26 +0000 (18:53 -0500)
Lib/test/test_descr.py
Misc/NEWS
Objects/typeobject.c

index 1f2039e8113cdd43074496a7bc570e476fd32cd4..3f68ee9bd1d333aefab931b3f5c7de2084629d83 100644 (file)
@@ -4253,6 +4253,14 @@ order (MRO) for bases """
         foo = Foo()
         str(foo)
 
+    def test_slot_shadows_class(self):
+        with self.assertRaises(ValueError) as cm:
+            class X:
+                __slots__ = ["foo"]
+                foo = None
+        m = str(cm.exception)
+        self.assertEqual("'foo' in __slots__ conflicts with class variable", m)
+
 class DictProxyTests(unittest.TestCase):
     def setUp(self):
         class C(object):
index 038dc9066872732f4e750ccb3fcd60800321dffe..4cf9dda5488677c164a4dd605f6001c96e822f65 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.3 Alpha 1?
 Core and Builtins
 -----------------
 
+- Issue #12766: Raise an ValueError when creating a class with a class variable
+  that conflicts with a name in __slots__.
+
 - Issue #12266: Fix str.capitalize() to correctly uppercase/lowercase
   titlecased and cased non-letter characters.
 
index bb4622f73805709e34d4697c48874e0c2d067675..3c1d3a1681f4cdb5329fccfaa0f68a63688c63ec 100644 (file)
@@ -2094,6 +2094,12 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
             if (!tmp)
                 goto bad_slots;
             PyList_SET_ITEM(newslots, j, tmp);
+            if (PyDict_GetItem(dict, tmp)) {
+                PyErr_Format(PyExc_ValueError,
+                             "%R in __slots__ conflicts with class variable",
+                             tmp);
+                goto bad_slots;
+            }
             j++;
         }
         assert(j == nslots - add_dict - add_weak);