diff options
author | Guido van Rossum <guido@dropbox.com> | 2015-09-05 03:54:07 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@dropbox.com> | 2015-09-05 03:54:07 (GMT) |
commit | 7d293ee97dbe27e2b6a43f900cf988572108c18e (patch) | |
tree | 99fb70d44ddf1d18cdadfa56a39995e4f906b4d0 /Lib | |
parent | 1b6691053701e92490c6b68171d6d6a645f8e708 (diff) | |
download | cpython-7d293ee97dbe27e2b6a43f900cf988572108c18e.zip cpython-7d293ee97dbe27e2b6a43f900cf988572108c18e.tar.gz cpython-7d293ee97dbe27e2b6a43f900cf988572108c18e.tar.bz2 |
Issue #24912: Prevent __class__ assignment to immutable built-in objects.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_descr.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 0ef1a31..c74ebae 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -1036,6 +1036,51 @@ order (MRO) for bases """ self.assertTrue(m.__class__ is types.ModuleType) self.assertFalse(hasattr(m, "a")) + # Make sure that builtin immutable objects don't support __class__ + # assignment, because the object instances may be interned. + # We set __slots__ = () to ensure that the subclasses are + # memory-layout compatible, and thus otherwise reasonable candidates + # for __class__ assignment. + + # The following types have immutable instances, but are not + # subclassable and thus don't need to be checked: + # NoneType, bool + + class MyInt(int): + __slots__ = () + with self.assertRaises(TypeError): + (1).__class__ = MyInt + + class MyFloat(float): + __slots__ = () + with self.assertRaises(TypeError): + (1.0).__class__ = MyFloat + + class MyComplex(complex): + __slots__ = () + with self.assertRaises(TypeError): + (1 + 2j).__class__ = MyComplex + + class MyStr(str): + __slots__ = () + with self.assertRaises(TypeError): + "a".__class__ = MyStr + + class MyBytes(bytes): + __slots__ = () + with self.assertRaises(TypeError): + b"a".__class__ = MyBytes + + class MyTuple(tuple): + __slots__ = () + with self.assertRaises(TypeError): + ().__class__ = MyTuple + + class MyFrozenSet(frozenset): + __slots__ = () + with self.assertRaises(TypeError): + frozenset().__class__ = MyFrozenSet + def test_slots(self): # Testing __slots__... class C0(object): |