with self.assertRaises(AttributeError):
del X.__abstractmethods__
+ def test_gh55664(self):
+ # gh-55664: 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', (), {})
+
def test_proxy_call(self):
class FakeStr:
__class__ = str
mykey = 'from Base2'
mykey2 = 'from Base2'
- X = type('X', (Base,), {MyKey(): 5})
+ with self.assertWarnsRegex(RuntimeWarning, 'X'):
+ X = type('X', (Base,), {MyKey(): 5})
# mykey is read from Base
self.assertEqual(X.mykey, 'from Base')
# mykey2 is read from Base2 because MyKey.__eq__ has set __bases__
// Put the proper slots in place
fixup_slot_dispatchers(type);
+ if (!_PyDict_HasOnlyStringKeys(type->tp_dict)) {
+ if (PyErr_WarnFormat(
+ PyExc_RuntimeWarning,
+ 1,
+ "non-string key in the __dict__ of class %.200s",
+ type->tp_name) == -1)
+ {
+ goto error;
+ }
+ }
+
if (type_new_set_names(type) < 0) {
goto error;
}