diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 1f2039e8113..3f68ee9bd1d 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -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): diff --git a/Misc/NEWS b/Misc/NEWS index 038dc906687..4cf9dda5488 100644 --- 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. diff --git a/Objects/typeobject.c b/Objects/typeobject.c index bb4622f7380..3c1d3a1681f 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -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);