diff -r 55bea11d892e Lib/test/test_descr.py --- a/Lib/test/test_descr.py Thu Mar 10 08:44:08 2011 -0500 +++ b/Lib/test/test_descr.py Thu Mar 10 19:46:49 2011 +0100 @@ -4235,6 +4235,19 @@ with self.assertRaises(AttributeError): del X.__abstractmethods__ + def test_issue11455(self): + # issue11455: issue a warning when the + # __dict__ of a class contains non-string keys + with self.assertWarnsRegex(RuntimeWarning, 'MyClass'): + MyClass = type('MyClass', (), {1: 2}) + + class meta(type): + def __new__(mcls, name, bases, ns): + ns[1] = 2 + return super().__new__(mcls, name, bases, ns) + with self.assertWarnsRegex(RuntimeWarning, 'MyClass'): + MyClass = meta('MyClass', (), {}) + class DictProxyTests(unittest.TestCase): def setUp(self): diff -r 55bea11d892e Objects/typeobject.c --- a/Objects/typeobject.c Thu Mar 10 08:44:08 2011 -0500 +++ b/Objects/typeobject.c Thu Mar 10 19:46:49 2011 +0100 @@ -2316,6 +2316,19 @@ /* Put the proper slots in place */ fixup_slot_dispatchers(type); + if (!_PyDict_HasOnlyStringKeys(type->tp_dict)) { + PyObject *name_of_class = type_name(type, NULL); + if (name_of_class == NULL) { + Py_DECREF(type); + return NULL; + } + + if (PyErr_WarnFormat(NULL, 1, "non-string key in the __dict__ of class %U", name_of_class) == -1) { + Py_DECREF(type); + return NULL; + } + } + return (PyObject *)type; }