From 1d95bbcf95489cb3db0cac0018f3cbb3db024372 Mon Sep 17 00:00:00 2001 From: AN Long Date: Fri, 5 Jan 2024 01:04:53 +0800 Subject: [PATCH 01/16] add inheritance hierarchy and other tests for ctypes base types --- Lib/test/test_ctypes/test_arrays.py | 23 +++++++++++++ Lib/test/test_ctypes/test_pointers.py | 23 +++++++++++++ Lib/test/test_ctypes/test_simplesubclasses.py | 28 +++++++++++++++- Lib/test/test_ctypes/test_structures.py | 23 +++++++++++++ Lib/test/test_ctypes/test_unions.py | 32 +++++++++++++++++++ 5 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 Lib/test/test_ctypes/test_unions.py diff --git a/Lib/test/test_ctypes/test_arrays.py b/Lib/test/test_ctypes/test_arrays.py index 6b6cebd3e20285..6c5687da4916e7 100644 --- a/Lib/test/test_ctypes/test_arrays.py +++ b/Lib/test/test_ctypes/test_arrays.py @@ -14,6 +14,10 @@ formats = c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, \ c_long, c_ulonglong, c_float, c_double, c_longdouble +ArrayType = type(Array) +# _CData is not exported to Python, so we have to access it from __base__. +_CData = Structure.__base__ + def ARRAY(*args): # ignore DeprecationWarning in tests @@ -23,6 +27,25 @@ def ARRAY(*args): class ArrayTestCase(unittest.TestCase): + def test_inheritance_hierarchy(self): + self.assertEqual(_CData.__name__, "_CData") + self.assertEqual(Array.mro(), [Array, _CData, object]) + + self.assertEqual(ArrayType.__name__, "PyCArrayType") + self.assertEqual(type(ArrayType), type) + + def test_instantiation(self): + with self.assertRaisesRegex(TypeError, "abstract class"): + Array() + + ArrayType("Foo", (Array,), {"_length_": 1, "_type_": c_int}) + + def test_immutability(self): + for t in [Array, ArrayType]: + msg = "cannot set 'foo' attribute of immutable type" + with self.assertRaisesRegex(TypeError, msg): + t.foo = "bar" + def test_simple(self): # create classes holding simple numeric types, and check # various properties. diff --git a/Lib/test/test_ctypes/test_pointers.py b/Lib/test/test_ctypes/test_pointers.py index 8410174358c19d..5aaeb59787c554 100644 --- a/Lib/test/test_ctypes/test_pointers.py +++ b/Lib/test/test_ctypes/test_pointers.py @@ -17,8 +17,31 @@ python_types = [int, int, int, int, int, int, int, int, int, int, float, float] +PointerType = type(_Pointer) +# _CData is not exported to Python, so we have to access it from __base__. +_CData = Structure.__base__ + class PointersTestCase(unittest.TestCase): + def test_inheritance_hierarchy(self): + self.assertEqual(_CData.__name__, "_CData") + self.assertEqual(_Pointer.mro(), [_Pointer, _CData, object]) + + self.assertEqual(PointerType.__name__, "PyCPointerType") + self.assertEqual(type(PointerType), type) + + def test_abstract_class(self): + with self.assertRaisesRegex(TypeError, "Cannot create instance: has no _type"): + _Pointer() + + PointerType("Foo", (_Pointer,), {}) + + def test_immutability(self): + for t in [_Pointer, PointerType]: + msg = "cannot set 'foo' attribute of immutable type" + with self.assertRaisesRegex(TypeError, msg): + t.foo = "bar" + def test_pointer_crash(self): class A(POINTER(c_ulong)): diff --git a/Lib/test/test_ctypes/test_simplesubclasses.py b/Lib/test/test_ctypes/test_simplesubclasses.py index 6072b62de5d53a..9c49e2dfecc97e 100644 --- a/Lib/test/test_ctypes/test_simplesubclasses.py +++ b/Lib/test/test_ctypes/test_simplesubclasses.py @@ -1,5 +1,10 @@ import unittest -from ctypes import Structure, CFUNCTYPE, c_int +from ctypes import Structure, CFUNCTYPE, c_int, _SimpleCData + + +SimpleType = type(_SimpleCData) +# _CData is not exported to Python, so we have to access it from __base__. +_CData = Structure.__base__ class MyInt(c_int): @@ -10,6 +15,27 @@ def __eq__(self, other): class Test(unittest.TestCase): + def test_inheritance_hierarchy(self): + self.assertEqual(_CData.__name__, "_CData") + self.assertEqual(_SimpleCData.mro(), [_SimpleCData, _CData, object]) + + self.assertEqual(SimpleType.__name__, "PyCSimpleType") + self.assertEqual(type(SimpleType), type) + + self.assertEqual(c_int.mro(), [c_int, _SimpleCData, _CData, object]) + + def test_abstract_class(self): + with self.assertRaisesRegex(TypeError, "abstract class"): + _SimpleCData() + + SimpleType("Foo", (_SimpleCData,), {"_type_": "i"}) + + def test_immutability(self): + for t in [_SimpleCData, SimpleType]: + msg = "cannot set 'foo' attribute of immutable type" + with self.assertRaisesRegex(TypeError, msg): + t.foo = "bar" + def test_compare(self): self.assertEqual(MyInt(3), MyInt(3)) diff --git a/Lib/test/test_ctypes/test_structures.py b/Lib/test/test_ctypes/test_structures.py index 3eafc77ca70aea..21ee8466d008b9 100644 --- a/Lib/test/test_ctypes/test_structures.py +++ b/Lib/test/test_ctypes/test_structures.py @@ -13,6 +13,10 @@ from collections import namedtuple from test import support +StructureType = type(Structure) +# _CData is not exported to Python, so we have to access it from __base__. +_CData = Array.__base__ + class SubclassesTest(unittest.TestCase): def test_subclass(self): @@ -70,6 +74,25 @@ class StructureTestCase(unittest.TestCase): "d": c_double, } + def test_inheritance_hierarchy(self): + self.assertEqual(_CData.__name__, "_CData") + self.assertEqual(Structure.mro(), [Structure, _CData, object]) + + self.assertEqual(StructureType.__name__, "PyCStructType") + self.assertEqual(type(StructureType), type) + + def test_instantiation(self): + with self.assertRaisesRegex(TypeError, "abstract class"): + Structure() + + StructureType("Foo", (Structure,), {}) + + def test_immutability(self): + for t in [Structure, StructureType]: + msg = "cannot set 'foo' attribute of immutable type" + with self.assertRaisesRegex(TypeError, msg): + t.foo = "bar" + def test_simple_structs(self): for code, tp in self.formats.items(): class X(Structure): diff --git a/Lib/test/test_ctypes/test_unions.py b/Lib/test/test_ctypes/test_unions.py new file mode 100644 index 00000000000000..2176dd9ea7f8eb --- /dev/null +++ b/Lib/test/test_ctypes/test_unions.py @@ -0,0 +1,32 @@ +import ctypes +import sys +import unittest +import warnings +from ctypes import Union + + +UnionType = type(Union) +# _CData is not exported to Python, so we have to access it from __base__. +_CData = Union.__base__ + + +class ArrayTestCase(unittest.TestCase): + def test_inheritance_hierarchy(self): + self.assertEqual(_CData.__name__, "_CData") + self.assertEqual(Union.mro(), [Union, _CData, object]) + + self.assertEqual(UnionType.__name__, "UnionType") + self.assertEqual(type(UnionType), type) + + def test_instantiation(self): + with self.assertRaisesRegex(TypeError, "abstract class"): + Union() + + UnionType("Foo", (Union,), {}) + + def test_immutability(self): + Union.foo = "bar" + + msg = "cannot set 'foo' attribute of immutable type" + with self.assertRaisesRegex(TypeError, msg): + UnionType.foo = "bar" From 6df089cb1e48bd438f907b429da43d3d8bc625dc Mon Sep 17 00:00:00 2001 From: AN Long Date: Fri, 5 Jan 2024 13:29:04 +0800 Subject: [PATCH 02/16] add tests for cfuncptr --- Lib/test/test_ctypes/test_funcptr.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Lib/test/test_ctypes/test_funcptr.py b/Lib/test/test_ctypes/test_funcptr.py index 2ad40647e0cfbb..b650ab98d90a3a 100644 --- a/Lib/test/test_ctypes/test_funcptr.py +++ b/Lib/test/test_ctypes/test_funcptr.py @@ -11,10 +11,34 @@ # fake to enable this test on Linux WINFUNCTYPE = CFUNCTYPE + +FuncPtrType = type(_CFuncPtr) +# _CData is not exported to Python, so we have to access it from __base__. +_CData = Structure.__base__ + lib = CDLL(_ctypes_test.__file__) class CFuncPtrTestCase(unittest.TestCase): + def test_inheritance_hierarchy(self): + self.assertEqual(_CData.__name__, "_CData") + self.assertEqual(_CFuncPtr.mro(), [_CFuncPtr, _CData, object]) + + self.assertEqual(FuncPtrType.__name__, "PyCFuncPtrType") + self.assertEqual(type(FuncPtrType), type) + + def test_abstract_class(self): + with self.assertRaisesRegex(TypeError, "abstract class"): + _CFuncPtr() + + FuncPtrType("Foo", (_CFuncPtr,), {"_flags_": 0}) + + def test_immutability(self): + for t in [_CFuncPtr, FuncPtrType]: + msg = "cannot set 'foo' attribute of immutable type" + with self.assertRaisesRegex(TypeError, msg): + t.foo = "bar" + def test_basic(self): X = WINFUNCTYPE(c_int, c_int, c_int) From 81802ae18087d4420cc471863d01c9b67c25f2f0 Mon Sep 17 00:00:00 2001 From: AN Long Date: Fri, 5 Jan 2024 14:05:45 +0800 Subject: [PATCH 03/16] add tests for CField --- Lib/test/test_ctypes/test_struct_fields.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_ctypes/test_struct_fields.py b/Lib/test/test_ctypes/test_struct_fields.py index f60dfe5b42ef65..b32a5a9b4dc546 100644 --- a/Lib/test/test_ctypes/test_struct_fields.py +++ b/Lib/test/test_ctypes/test_struct_fields.py @@ -55,11 +55,21 @@ class X(Structure): x.char = b'a\0b\0' self.assertEqual(bytes(x), b'a\x00###') - def test_6(self): + def test_cfield_instantiation(self): class X(Structure): _fields_ = [("x", c_int)] CField = type(X.x) - self.assertRaises(TypeError, CField) + self.assertRaisesRegex(TypeError, + "cannot create '_ctypes.CField' instances", + CField) + + def test_cfield_immutability(self): + class X(Structure): + _fields_ = [("x", c_int)] + CField = type(X.x) + msg = "cannot set 'foo' attribute of immutable type '_ctypes.CField'" + with self.assertRaisesRegex(TypeError, msg): + CField.foo = "bar" def test_gh99275(self): class BrokenStructure(Structure): From a8b4d327473a60b5e02f397bdf08b9bbd7cbf977 Mon Sep 17 00:00:00 2001 From: AN Long Date: Sat, 6 Jan 2024 22:36:48 +0800 Subject: [PATCH 04/16] add test for COMError --- Lib/test/test_ctypes/test_win32.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/test/test_ctypes/test_win32.py b/Lib/test/test_ctypes/test_win32.py index 01e624f76f0685..628d801f5119d6 100644 --- a/Lib/test/test_ctypes/test_win32.py +++ b/Lib/test/test_ctypes/test_win32.py @@ -73,6 +73,10 @@ def test_COMError(self): self.assertEqual(ex.text, "text") self.assertEqual(ex.details, ("details",)) + msg = "cannot set 'foo' attribute of immutable type '_ctypes.COMError'" + with self.assertRaisesRegex(TypeError, msg): + COMError.foo = "bar" + @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test') class TestWinError(unittest.TestCase): From e48efc3046aaf050ca58e358873447727abfffd1 Mon Sep 17 00:00:00 2001 From: AN Long Date: Tue, 9 Jan 2024 00:17:21 +0800 Subject: [PATCH 05/16] add support.py to export ctypes hidden types --- Lib/test/test_ctypes/support.py | 15 +++++++++++++++ Lib/test/test_ctypes/test_arrays.py | 14 +++++--------- Lib/test/test_ctypes/test_funcptr.py | 15 +++++---------- Lib/test/test_ctypes/test_simplesubclasses.py | 15 +++++---------- Lib/test/test_ctypes/test_structures.py | 14 +++++--------- Lib/test/test_ctypes/test_unions.py | 7 +------ 6 files changed, 36 insertions(+), 44 deletions(-) create mode 100644 Lib/test/test_ctypes/support.py diff --git a/Lib/test/test_ctypes/support.py b/Lib/test/test_ctypes/support.py new file mode 100644 index 00000000000000..34fe2b5052a2e4 --- /dev/null +++ b/Lib/test/test_ctypes/support.py @@ -0,0 +1,15 @@ +# Some classes and types are not export to _ctypes module directly. + +from _ctypes import Structure, Union, _Pointer, Array, _SimpleCData, CFuncPtr + + +_CData = Structure.__base__ +assert _CData.__name__, "_CData" + +# metaclasses +PyCStructType = type(Structure) +UnionType = type(Union) +PyCPointerType = type(_Pointer) +PyCArrayType = type(Array) +PyCSimpleType = type(_SimpleCData) +PyCFuncPtrType = type(CFuncPtr) diff --git a/Lib/test/test_ctypes/test_arrays.py b/Lib/test/test_ctypes/test_arrays.py index 6c5687da4916e7..3613286658de99 100644 --- a/Lib/test/test_ctypes/test_arrays.py +++ b/Lib/test/test_ctypes/test_arrays.py @@ -7,6 +7,7 @@ c_char, c_wchar, c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, c_long, c_ulonglong, c_float, c_double, c_longdouble) from test.support import bigmemtest, _2G +from .support import _CData, PyCArrayType formats = "bBhHiIlLqQfd" @@ -14,10 +15,6 @@ formats = c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, \ c_long, c_ulonglong, c_float, c_double, c_longdouble -ArrayType = type(Array) -# _CData is not exported to Python, so we have to access it from __base__. -_CData = Structure.__base__ - def ARRAY(*args): # ignore DeprecationWarning in tests @@ -28,20 +25,19 @@ def ARRAY(*args): class ArrayTestCase(unittest.TestCase): def test_inheritance_hierarchy(self): - self.assertEqual(_CData.__name__, "_CData") self.assertEqual(Array.mro(), [Array, _CData, object]) - self.assertEqual(ArrayType.__name__, "PyCArrayType") - self.assertEqual(type(ArrayType), type) + self.assertEqual(PyCArrayType.__name__, "PyCArrayType") + self.assertEqual(type(PyCArrayType), type) def test_instantiation(self): with self.assertRaisesRegex(TypeError, "abstract class"): Array() - ArrayType("Foo", (Array,), {"_length_": 1, "_type_": c_int}) + PyCArrayType("Foo", (Array,), {"_length_": 1, "_type_": c_int}) def test_immutability(self): - for t in [Array, ArrayType]: + for t in [Array, PyCArrayType]: msg = "cannot set 'foo' attribute of immutable type" with self.assertRaisesRegex(TypeError, msg): t.foo = "bar" diff --git a/Lib/test/test_ctypes/test_funcptr.py b/Lib/test/test_ctypes/test_funcptr.py index b650ab98d90a3a..284c4c19fa2462 100644 --- a/Lib/test/test_ctypes/test_funcptr.py +++ b/Lib/test/test_ctypes/test_funcptr.py @@ -3,6 +3,7 @@ import unittest from ctypes import (CDLL, Structure, CFUNCTYPE, sizeof, _CFuncPtr, c_void_p, c_char_p, c_char, c_int, c_uint, c_long) +from .support import _CData, PyCFuncPtrType try: @@ -11,30 +12,24 @@ # fake to enable this test on Linux WINFUNCTYPE = CFUNCTYPE - -FuncPtrType = type(_CFuncPtr) -# _CData is not exported to Python, so we have to access it from __base__. -_CData = Structure.__base__ - lib = CDLL(_ctypes_test.__file__) class CFuncPtrTestCase(unittest.TestCase): def test_inheritance_hierarchy(self): - self.assertEqual(_CData.__name__, "_CData") self.assertEqual(_CFuncPtr.mro(), [_CFuncPtr, _CData, object]) - self.assertEqual(FuncPtrType.__name__, "PyCFuncPtrType") - self.assertEqual(type(FuncPtrType), type) + self.assertEqual(PyCFuncPtrType.__name__, "PyCFuncPtrType") + self.assertEqual(type(PyCFuncPtrType), type) def test_abstract_class(self): with self.assertRaisesRegex(TypeError, "abstract class"): _CFuncPtr() - FuncPtrType("Foo", (_CFuncPtr,), {"_flags_": 0}) + PyCFuncPtrType("Foo", (_CFuncPtr,), {"_flags_": 0}) def test_immutability(self): - for t in [_CFuncPtr, FuncPtrType]: + for t in [_CFuncPtr, PyCFuncPtrType]: msg = "cannot set 'foo' attribute of immutable type" with self.assertRaisesRegex(TypeError, msg): t.foo = "bar" diff --git a/Lib/test/test_ctypes/test_simplesubclasses.py b/Lib/test/test_ctypes/test_simplesubclasses.py index 9c49e2dfecc97e..510d1e73e3ea9e 100644 --- a/Lib/test/test_ctypes/test_simplesubclasses.py +++ b/Lib/test/test_ctypes/test_simplesubclasses.py @@ -1,10 +1,6 @@ import unittest from ctypes import Structure, CFUNCTYPE, c_int, _SimpleCData - - -SimpleType = type(_SimpleCData) -# _CData is not exported to Python, so we have to access it from __base__. -_CData = Structure.__base__ +from .support import _CData, PyCSimpleType class MyInt(c_int): @@ -16,11 +12,10 @@ def __eq__(self, other): class Test(unittest.TestCase): def test_inheritance_hierarchy(self): - self.assertEqual(_CData.__name__, "_CData") self.assertEqual(_SimpleCData.mro(), [_SimpleCData, _CData, object]) - self.assertEqual(SimpleType.__name__, "PyCSimpleType") - self.assertEqual(type(SimpleType), type) + self.assertEqual(PyCSimpleType.__name__, "PyCSimpleType") + self.assertEqual(type(PyCSimpleType), type) self.assertEqual(c_int.mro(), [c_int, _SimpleCData, _CData, object]) @@ -28,10 +23,10 @@ def test_abstract_class(self): with self.assertRaisesRegex(TypeError, "abstract class"): _SimpleCData() - SimpleType("Foo", (_SimpleCData,), {"_type_": "i"}) + PyCSimpleType("Foo", (_SimpleCData,), {"_type_": "i"}) def test_immutability(self): - for t in [_SimpleCData, SimpleType]: + for t in [_SimpleCData, PyCSimpleType]: msg = "cannot set 'foo' attribute of immutable type" with self.assertRaisesRegex(TypeError, msg): t.foo = "bar" diff --git a/Lib/test/test_ctypes/test_structures.py b/Lib/test/test_ctypes/test_structures.py index 21ee8466d008b9..645f8c7e889227 100644 --- a/Lib/test/test_ctypes/test_structures.py +++ b/Lib/test/test_ctypes/test_structures.py @@ -12,10 +12,7 @@ from struct import calcsize from collections import namedtuple from test import support - -StructureType = type(Structure) -# _CData is not exported to Python, so we have to access it from __base__. -_CData = Array.__base__ +from .support import _CData, PyCStructType class SubclassesTest(unittest.TestCase): @@ -75,20 +72,19 @@ class StructureTestCase(unittest.TestCase): } def test_inheritance_hierarchy(self): - self.assertEqual(_CData.__name__, "_CData") self.assertEqual(Structure.mro(), [Structure, _CData, object]) - self.assertEqual(StructureType.__name__, "PyCStructType") - self.assertEqual(type(StructureType), type) + self.assertEqual(PyCStructType.__name__, "PyCStructType") + self.assertEqual(type(PyCStructType), type) def test_instantiation(self): with self.assertRaisesRegex(TypeError, "abstract class"): Structure() - StructureType("Foo", (Structure,), {}) + PyCStructType("Foo", (Structure,), {}) def test_immutability(self): - for t in [Structure, StructureType]: + for t in [Structure, PyCStructType]: msg = "cannot set 'foo' attribute of immutable type" with self.assertRaisesRegex(TypeError, msg): t.foo = "bar" diff --git a/Lib/test/test_ctypes/test_unions.py b/Lib/test/test_ctypes/test_unions.py index 2176dd9ea7f8eb..3aa4d5e16d10d1 100644 --- a/Lib/test/test_ctypes/test_unions.py +++ b/Lib/test/test_ctypes/test_unions.py @@ -3,16 +3,11 @@ import unittest import warnings from ctypes import Union - - -UnionType = type(Union) -# _CData is not exported to Python, so we have to access it from __base__. -_CData = Union.__base__ +from .support import _CData, UnionType class ArrayTestCase(unittest.TestCase): def test_inheritance_hierarchy(self): - self.assertEqual(_CData.__name__, "_CData") self.assertEqual(Union.mro(), [Union, _CData, object]) self.assertEqual(UnionType.__name__, "UnionType") From 0cd6f8813141da21fb79c35247d8202ac30942ef Mon Sep 17 00:00:00 2001 From: AN Long Date: Tue, 9 Jan 2024 00:56:18 +0800 Subject: [PATCH 06/16] change to check the type's mutablility by type.__flags__ --- Lib/test/test_ctypes/support.py | 4 ++++ Lib/test/test_ctypes/test_arrays.py | 18 ++++++--------- Lib/test/test_ctypes/test_funcptr.py | 18 ++++++--------- Lib/test/test_ctypes/test_simplesubclasses.py | 19 ++++++---------- Lib/test/test_ctypes/test_structures.py | 17 ++++++-------- Lib/test/test_ctypes/test_unions.py | 22 ++++++------------- 6 files changed, 39 insertions(+), 59 deletions(-) diff --git a/Lib/test/test_ctypes/support.py b/Lib/test/test_ctypes/support.py index 34fe2b5052a2e4..4834e13fd6518e 100644 --- a/Lib/test/test_ctypes/support.py +++ b/Lib/test/test_ctypes/support.py @@ -13,3 +13,7 @@ PyCArrayType = type(Array) PyCSimpleType = type(_SimpleCData) PyCFuncPtrType = type(CFuncPtr) + +# type flags +PY_TPFLAGS_DISALLOW_INSTANTIATION = 1 << 7 +PY_TPFLAGS_IMMUTABLETYPE = 1 << 8 diff --git a/Lib/test/test_ctypes/test_arrays.py b/Lib/test/test_ctypes/test_arrays.py index 3613286658de99..5948325eef6070 100644 --- a/Lib/test/test_ctypes/test_arrays.py +++ b/Lib/test/test_ctypes/test_arrays.py @@ -7,7 +7,8 @@ c_char, c_wchar, c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, c_long, c_ulonglong, c_float, c_double, c_longdouble) from test.support import bigmemtest, _2G -from .support import _CData, PyCArrayType +from .support import (_CData, PyCArrayType, PY_TPFLAGS_DISALLOW_INSTANTIATION, + PY_TPFLAGS_IMMUTABLETYPE) formats = "bBhHiIlLqQfd" @@ -30,17 +31,12 @@ def test_inheritance_hierarchy(self): self.assertEqual(PyCArrayType.__name__, "PyCArrayType") self.assertEqual(type(PyCArrayType), type) - def test_instantiation(self): - with self.assertRaisesRegex(TypeError, "abstract class"): - Array() + def test_type_flags(self): + self.assertTrue(Array.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(Array.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) - PyCArrayType("Foo", (Array,), {"_length_": 1, "_type_": c_int}) - - def test_immutability(self): - for t in [Array, PyCArrayType]: - msg = "cannot set 'foo' attribute of immutable type" - with self.assertRaisesRegex(TypeError, msg): - t.foo = "bar" + self.assertTrue(PyCArrayType.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(PyCArrayType.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) def test_simple(self): # create classes holding simple numeric types, and check diff --git a/Lib/test/test_ctypes/test_funcptr.py b/Lib/test/test_ctypes/test_funcptr.py index 284c4c19fa2462..ec79498a20d107 100644 --- a/Lib/test/test_ctypes/test_funcptr.py +++ b/Lib/test/test_ctypes/test_funcptr.py @@ -3,7 +3,8 @@ import unittest from ctypes import (CDLL, Structure, CFUNCTYPE, sizeof, _CFuncPtr, c_void_p, c_char_p, c_char, c_int, c_uint, c_long) -from .support import _CData, PyCFuncPtrType +from .support import (_CData, PyCFuncPtrType, PY_TPFLAGS_DISALLOW_INSTANTIATION, + PY_TPFLAGS_IMMUTABLETYPE) try: @@ -22,17 +23,12 @@ def test_inheritance_hierarchy(self): self.assertEqual(PyCFuncPtrType.__name__, "PyCFuncPtrType") self.assertEqual(type(PyCFuncPtrType), type) - def test_abstract_class(self): - with self.assertRaisesRegex(TypeError, "abstract class"): - _CFuncPtr() + def test_type_flags(self): + self.assertTrue(_CFuncPtr.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(_CFuncPtr.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) - PyCFuncPtrType("Foo", (_CFuncPtr,), {"_flags_": 0}) - - def test_immutability(self): - for t in [_CFuncPtr, PyCFuncPtrType]: - msg = "cannot set 'foo' attribute of immutable type" - with self.assertRaisesRegex(TypeError, msg): - t.foo = "bar" + self.assertTrue(PyCFuncPtrType.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(PyCFuncPtrType.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) def test_basic(self): X = WINFUNCTYPE(c_int, c_int, c_int) diff --git a/Lib/test/test_ctypes/test_simplesubclasses.py b/Lib/test/test_ctypes/test_simplesubclasses.py index 510d1e73e3ea9e..cfaacc97abece8 100644 --- a/Lib/test/test_ctypes/test_simplesubclasses.py +++ b/Lib/test/test_ctypes/test_simplesubclasses.py @@ -1,6 +1,7 @@ import unittest from ctypes import Structure, CFUNCTYPE, c_int, _SimpleCData -from .support import _CData, PyCSimpleType +from .support import (_CData, PyCSimpleType, PY_TPFLAGS_DISALLOW_INSTANTIATION, + PY_TPFLAGS_IMMUTABLETYPE) class MyInt(c_int): @@ -19,18 +20,12 @@ def test_inheritance_hierarchy(self): self.assertEqual(c_int.mro(), [c_int, _SimpleCData, _CData, object]) - def test_abstract_class(self): - with self.assertRaisesRegex(TypeError, "abstract class"): - _SimpleCData() - - PyCSimpleType("Foo", (_SimpleCData,), {"_type_": "i"}) - - def test_immutability(self): - for t in [_SimpleCData, PyCSimpleType]: - msg = "cannot set 'foo' attribute of immutable type" - with self.assertRaisesRegex(TypeError, msg): - t.foo = "bar" + def test_type_flags(self): + self.assertTrue(_SimpleCData.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(_SimpleCData.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) + self.assertTrue(PyCSimpleType.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(PyCSimpleType.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) def test_compare(self): self.assertEqual(MyInt(3), MyInt(3)) diff --git a/Lib/test/test_ctypes/test_structures.py b/Lib/test/test_ctypes/test_structures.py index 645f8c7e889227..5b36da39b788b5 100644 --- a/Lib/test/test_ctypes/test_structures.py +++ b/Lib/test/test_ctypes/test_structures.py @@ -12,7 +12,8 @@ from struct import calcsize from collections import namedtuple from test import support -from .support import _CData, PyCStructType +from .support import (_CData, PyCStructType, PY_TPFLAGS_DISALLOW_INSTANTIATION, + PY_TPFLAGS_IMMUTABLETYPE) class SubclassesTest(unittest.TestCase): @@ -77,17 +78,13 @@ def test_inheritance_hierarchy(self): self.assertEqual(PyCStructType.__name__, "PyCStructType") self.assertEqual(type(PyCStructType), type) - def test_instantiation(self): - with self.assertRaisesRegex(TypeError, "abstract class"): - Structure() - PyCStructType("Foo", (Structure,), {}) + def test_type_flags(self): + self.assertTrue(Structure.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(Structure.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) - def test_immutability(self): - for t in [Structure, PyCStructType]: - msg = "cannot set 'foo' attribute of immutable type" - with self.assertRaisesRegex(TypeError, msg): - t.foo = "bar" + self.assertTrue(PyCStructType.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(PyCStructType.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) def test_simple_structs(self): for code, tp in self.formats.items(): diff --git a/Lib/test/test_ctypes/test_unions.py b/Lib/test/test_ctypes/test_unions.py index 3aa4d5e16d10d1..a2de55caa988fe 100644 --- a/Lib/test/test_ctypes/test_unions.py +++ b/Lib/test/test_ctypes/test_unions.py @@ -1,9 +1,7 @@ -import ctypes -import sys import unittest -import warnings from ctypes import Union -from .support import _CData, UnionType +from .support import (_CData, UnionType, PY_TPFLAGS_DISALLOW_INSTANTIATION, + PY_TPFLAGS_IMMUTABLETYPE) class ArrayTestCase(unittest.TestCase): @@ -13,15 +11,9 @@ def test_inheritance_hierarchy(self): self.assertEqual(UnionType.__name__, "UnionType") self.assertEqual(type(UnionType), type) - def test_instantiation(self): - with self.assertRaisesRegex(TypeError, "abstract class"): - Union() + def test_type_flags(self): + self.assertTrue(Union.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(Union.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) - UnionType("Foo", (Union,), {}) - - def test_immutability(self): - Union.foo = "bar" - - msg = "cannot set 'foo' attribute of immutable type" - with self.assertRaisesRegex(TypeError, msg): - UnionType.foo = "bar" + self.assertTrue(UnionType.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(UnionType.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) From b031cc9e3d5f82dee125fc750bc1f471fafaf169 Mon Sep 17 00:00:00 2001 From: AN Long Date: Tue, 9 Jan 2024 01:00:13 +0800 Subject: [PATCH 07/16] fix assert in support.py --- Lib/test/test_ctypes/support.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_ctypes/support.py b/Lib/test/test_ctypes/support.py index 4834e13fd6518e..a2a57e06621ea1 100644 --- a/Lib/test/test_ctypes/support.py +++ b/Lib/test/test_ctypes/support.py @@ -4,7 +4,7 @@ _CData = Structure.__base__ -assert _CData.__name__, "_CData" +assert _CData.__name__ == "_CData" # metaclasses PyCStructType = type(Structure) From 1e7c0642cffabdd85dc2b8541b1da83ecef9d361 Mon Sep 17 00:00:00 2001 From: AN Long Date: Tue, 9 Jan 2024 01:15:11 +0800 Subject: [PATCH 08/16] change more test to check the type.__flags__ --- Lib/test/test_ctypes/test_pointers.py | 26 ++++++++-------------- Lib/test/test_ctypes/test_struct_fields.py | 26 ++++++++++------------ Lib/test/test_ctypes/test_win32.py | 8 ++++--- 3 files changed, 26 insertions(+), 34 deletions(-) diff --git a/Lib/test/test_ctypes/test_pointers.py b/Lib/test/test_ctypes/test_pointers.py index 5aaeb59787c554..5645cade0fac51 100644 --- a/Lib/test/test_ctypes/test_pointers.py +++ b/Lib/test/test_ctypes/test_pointers.py @@ -10,6 +10,8 @@ c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong, c_float, c_double) +from .support import (_CData, PyCPointerType, PY_TPFLAGS_DISALLOW_INSTANTIATION, + PY_TPFLAGS_IMMUTABLETYPE) ctype_types = [c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, @@ -17,30 +19,20 @@ python_types = [int, int, int, int, int, int, int, int, int, int, float, float] -PointerType = type(_Pointer) -# _CData is not exported to Python, so we have to access it from __base__. -_CData = Structure.__base__ - class PointersTestCase(unittest.TestCase): def test_inheritance_hierarchy(self): - self.assertEqual(_CData.__name__, "_CData") self.assertEqual(_Pointer.mro(), [_Pointer, _CData, object]) - self.assertEqual(PointerType.__name__, "PyCPointerType") - self.assertEqual(type(PointerType), type) - - def test_abstract_class(self): - with self.assertRaisesRegex(TypeError, "Cannot create instance: has no _type"): - _Pointer() + self.assertEqual(PyCPointerType.__name__, "PyCPointerType") + self.assertEqual(type(PyCPointerType), type) - PointerType("Foo", (_Pointer,), {}) + def test_type_flags(self): + self.assertTrue(_Pointer.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(_Pointer.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) - def test_immutability(self): - for t in [_Pointer, PointerType]: - msg = "cannot set 'foo' attribute of immutable type" - with self.assertRaisesRegex(TypeError, msg): - t.foo = "bar" + self.assertTrue(PyCPointerType.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(PyCPointerType.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) def test_pointer_crash(self): diff --git a/Lib/test/test_ctypes/test_struct_fields.py b/Lib/test/test_ctypes/test_struct_fields.py index b32a5a9b4dc546..67e3ae74fca357 100644 --- a/Lib/test/test_ctypes/test_struct_fields.py +++ b/Lib/test/test_ctypes/test_struct_fields.py @@ -1,5 +1,6 @@ import unittest from ctypes import Structure, Union, sizeof, c_char, c_int +from .support import PY_TPFLAGS_DISALLOW_INSTANTIATION, PY_TPFLAGS_IMMUTABLETYPE class StructFieldsTestCase(unittest.TestCase): @@ -12,6 +13,9 @@ class StructFieldsTestCase(unittest.TestCase): # 4. The type is subclassed # # When they are finalized, assigning _fields_ is no longer allowed. + class X(Structure): + _fields_ = [("x", c_int)] + CField = type(X.x) def test_1_A(self): class X(Structure): @@ -55,21 +59,15 @@ class X(Structure): x.char = b'a\0b\0' self.assertEqual(bytes(x), b'a\x00###') - def test_cfield_instantiation(self): - class X(Structure): - _fields_ = [("x", c_int)] - CField = type(X.x) - self.assertRaisesRegex(TypeError, - "cannot create '_ctypes.CField' instances", - CField) + def test_6(self): + self.assertRaises(TypeError, self.CField) - def test_cfield_immutability(self): - class X(Structure): - _fields_ = [("x", c_int)] - CField = type(X.x) - msg = "cannot set 'foo' attribute of immutable type '_ctypes.CField'" - with self.assertRaisesRegex(TypeError, msg): - CField.foo = "bar" + def test_cfield_type_flags(self): + self.assertTrue(self.CField.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) + self.assertTrue(self.CField.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) + + def test_cfield_inheritance_hierarchy(self): + self.assertEqual(self.CField.mro(), [self.CField, object]) def test_gh99275(self): class BrokenStructure(Structure): diff --git a/Lib/test/test_ctypes/test_win32.py b/Lib/test/test_ctypes/test_win32.py index 628d801f5119d6..e4ab8d41741d3d 100644 --- a/Lib/test/test_ctypes/test_win32.py +++ b/Lib/test/test_ctypes/test_win32.py @@ -9,6 +9,7 @@ _pointer_type_cache, c_void_p, c_char, c_int, c_long) from test import support +from .support import PY_TPFLAGS_DISALLOW_INSTANTIATION, PY_TPFLAGS_IMMUTABLETYPE @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test') @@ -73,9 +74,10 @@ def test_COMError(self): self.assertEqual(ex.text, "text") self.assertEqual(ex.details, ("details",)) - msg = "cannot set 'foo' attribute of immutable type '_ctypes.COMError'" - with self.assertRaisesRegex(TypeError, msg): - COMError.foo = "bar" + self.assertEqual(COMError.mro(), + [COMError, Exception, BaseException, object]) + self.assertFalse(COMError.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) + self.assertTrue(COMError.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test') From d8437637764d4eac6b2fb9ec65d197dbd43f4a58 Mon Sep 17 00:00:00 2001 From: AN Long Date: Tue, 9 Jan 2024 20:23:04 +0800 Subject: [PATCH 09/16] Update Lib/test/test_ctypes/support.py Co-authored-by: Erlend E. Aasland --- Lib/test/test_ctypes/support.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_ctypes/support.py b/Lib/test/test_ctypes/support.py index a2a57e06621ea1..fe7c9e6b25baf9 100644 --- a/Lib/test/test_ctypes/support.py +++ b/Lib/test/test_ctypes/support.py @@ -15,5 +15,5 @@ PyCFuncPtrType = type(CFuncPtr) # type flags -PY_TPFLAGS_DISALLOW_INSTANTIATION = 1 << 7 -PY_TPFLAGS_IMMUTABLETYPE = 1 << 8 +Py_TPFLAGS_DISALLOW_INSTANTIATION = 1 << 7 +Py_TPFLAGS_IMMUTABLETYPE = 1 << 8 From 8636250fba84c06119ee9e6374c44e090dd14b5f Mon Sep 17 00:00:00 2001 From: AN Long Date: Tue, 9 Jan 2024 20:32:47 +0800 Subject: [PATCH 10/16] rename type flag symbol name --- Lib/test/test_ctypes/test_arrays.py | 12 ++++++------ Lib/test/test_ctypes/test_funcptr.py | 12 ++++++------ Lib/test/test_ctypes/test_pointers.py | 12 ++++++------ Lib/test/test_ctypes/test_simplesubclasses.py | 12 ++++++------ Lib/test/test_ctypes/test_struct_fields.py | 6 +++--- Lib/test/test_ctypes/test_structures.py | 12 ++++++------ Lib/test/test_ctypes/test_unions.py | 12 ++++++------ Lib/test/test_ctypes/test_win32.py | 6 +++--- 8 files changed, 42 insertions(+), 42 deletions(-) diff --git a/Lib/test/test_ctypes/test_arrays.py b/Lib/test/test_ctypes/test_arrays.py index 5948325eef6070..f32e912149e9f6 100644 --- a/Lib/test/test_ctypes/test_arrays.py +++ b/Lib/test/test_ctypes/test_arrays.py @@ -7,8 +7,8 @@ c_char, c_wchar, c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, c_long, c_ulonglong, c_float, c_double, c_longdouble) from test.support import bigmemtest, _2G -from .support import (_CData, PyCArrayType, PY_TPFLAGS_DISALLOW_INSTANTIATION, - PY_TPFLAGS_IMMUTABLETYPE) +from .support import (_CData, PyCArrayType, Py_TPFLAGS_DISALLOW_INSTANTIATION, + Py_TPFLAGS_IMMUTABLETYPE) formats = "bBhHiIlLqQfd" @@ -32,11 +32,11 @@ def test_inheritance_hierarchy(self): self.assertEqual(type(PyCArrayType), type) def test_type_flags(self): - self.assertTrue(Array.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(Array.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) + self.assertTrue(Array.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(Array.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) - self.assertTrue(PyCArrayType.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(PyCArrayType.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) + self.assertTrue(PyCArrayType.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(PyCArrayType.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) def test_simple(self): # create classes holding simple numeric types, and check diff --git a/Lib/test/test_ctypes/test_funcptr.py b/Lib/test/test_ctypes/test_funcptr.py index ec79498a20d107..1745f224b82963 100644 --- a/Lib/test/test_ctypes/test_funcptr.py +++ b/Lib/test/test_ctypes/test_funcptr.py @@ -3,8 +3,8 @@ import unittest from ctypes import (CDLL, Structure, CFUNCTYPE, sizeof, _CFuncPtr, c_void_p, c_char_p, c_char, c_int, c_uint, c_long) -from .support import (_CData, PyCFuncPtrType, PY_TPFLAGS_DISALLOW_INSTANTIATION, - PY_TPFLAGS_IMMUTABLETYPE) +from .support import (_CData, PyCFuncPtrType, Py_TPFLAGS_DISALLOW_INSTANTIATION, + Py_TPFLAGS_IMMUTABLETYPE) try: @@ -24,11 +24,11 @@ def test_inheritance_hierarchy(self): self.assertEqual(type(PyCFuncPtrType), type) def test_type_flags(self): - self.assertTrue(_CFuncPtr.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(_CFuncPtr.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) + self.assertTrue(_CFuncPtr.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(_CFuncPtr.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) - self.assertTrue(PyCFuncPtrType.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(PyCFuncPtrType.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) + self.assertTrue(PyCFuncPtrType.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(PyCFuncPtrType.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) def test_basic(self): X = WINFUNCTYPE(c_int, c_int, c_int) diff --git a/Lib/test/test_ctypes/test_pointers.py b/Lib/test/test_ctypes/test_pointers.py index 5645cade0fac51..864de2a49b5111 100644 --- a/Lib/test/test_ctypes/test_pointers.py +++ b/Lib/test/test_ctypes/test_pointers.py @@ -10,8 +10,8 @@ c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong, c_float, c_double) -from .support import (_CData, PyCPointerType, PY_TPFLAGS_DISALLOW_INSTANTIATION, - PY_TPFLAGS_IMMUTABLETYPE) +from .support import (_CData, PyCPointerType, Py_TPFLAGS_DISALLOW_INSTANTIATION, + Py_TPFLAGS_IMMUTABLETYPE) ctype_types = [c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, @@ -28,11 +28,11 @@ def test_inheritance_hierarchy(self): self.assertEqual(type(PyCPointerType), type) def test_type_flags(self): - self.assertTrue(_Pointer.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(_Pointer.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) + self.assertTrue(_Pointer.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(_Pointer.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) - self.assertTrue(PyCPointerType.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(PyCPointerType.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) + self.assertTrue(PyCPointerType.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(PyCPointerType.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) def test_pointer_crash(self): diff --git a/Lib/test/test_ctypes/test_simplesubclasses.py b/Lib/test/test_ctypes/test_simplesubclasses.py index cfaacc97abece8..b75f4f09052d74 100644 --- a/Lib/test/test_ctypes/test_simplesubclasses.py +++ b/Lib/test/test_ctypes/test_simplesubclasses.py @@ -1,7 +1,7 @@ import unittest from ctypes import Structure, CFUNCTYPE, c_int, _SimpleCData -from .support import (_CData, PyCSimpleType, PY_TPFLAGS_DISALLOW_INSTANTIATION, - PY_TPFLAGS_IMMUTABLETYPE) +from .support import (_CData, PyCSimpleType, Py_TPFLAGS_DISALLOW_INSTANTIATION, + Py_TPFLAGS_IMMUTABLETYPE) class MyInt(c_int): @@ -21,11 +21,11 @@ def test_inheritance_hierarchy(self): self.assertEqual(c_int.mro(), [c_int, _SimpleCData, _CData, object]) def test_type_flags(self): - self.assertTrue(_SimpleCData.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(_SimpleCData.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) + self.assertTrue(_SimpleCData.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(_SimpleCData.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) - self.assertTrue(PyCSimpleType.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(PyCSimpleType.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) + self.assertTrue(PyCSimpleType.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(PyCSimpleType.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) def test_compare(self): self.assertEqual(MyInt(3), MyInt(3)) diff --git a/Lib/test/test_ctypes/test_struct_fields.py b/Lib/test/test_ctypes/test_struct_fields.py index 67e3ae74fca357..45e06e2e2e5fdf 100644 --- a/Lib/test/test_ctypes/test_struct_fields.py +++ b/Lib/test/test_ctypes/test_struct_fields.py @@ -1,6 +1,6 @@ import unittest from ctypes import Structure, Union, sizeof, c_char, c_int -from .support import PY_TPFLAGS_DISALLOW_INSTANTIATION, PY_TPFLAGS_IMMUTABLETYPE +from .support import Py_TPFLAGS_DISALLOW_INSTANTIATION, Py_TPFLAGS_IMMUTABLETYPE class StructFieldsTestCase(unittest.TestCase): @@ -63,8 +63,8 @@ def test_6(self): self.assertRaises(TypeError, self.CField) def test_cfield_type_flags(self): - self.assertTrue(self.CField.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) - self.assertTrue(self.CField.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) + self.assertTrue(self.CField.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) + self.assertTrue(self.CField.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) def test_cfield_inheritance_hierarchy(self): self.assertEqual(self.CField.mro(), [self.CField, object]) diff --git a/Lib/test/test_ctypes/test_structures.py b/Lib/test/test_ctypes/test_structures.py index 5b36da39b788b5..38cda833176178 100644 --- a/Lib/test/test_ctypes/test_structures.py +++ b/Lib/test/test_ctypes/test_structures.py @@ -12,8 +12,8 @@ from struct import calcsize from collections import namedtuple from test import support -from .support import (_CData, PyCStructType, PY_TPFLAGS_DISALLOW_INSTANTIATION, - PY_TPFLAGS_IMMUTABLETYPE) +from .support import (_CData, PyCStructType, Py_TPFLAGS_DISALLOW_INSTANTIATION, + Py_TPFLAGS_IMMUTABLETYPE) class SubclassesTest(unittest.TestCase): @@ -80,11 +80,11 @@ def test_inheritance_hierarchy(self): def test_type_flags(self): - self.assertTrue(Structure.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(Structure.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) + self.assertTrue(Structure.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(Structure.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) - self.assertTrue(PyCStructType.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(PyCStructType.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) + self.assertTrue(PyCStructType.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(PyCStructType.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) def test_simple_structs(self): for code, tp in self.formats.items(): diff --git a/Lib/test/test_ctypes/test_unions.py b/Lib/test/test_ctypes/test_unions.py index a2de55caa988fe..20b1b65f3fffaa 100644 --- a/Lib/test/test_ctypes/test_unions.py +++ b/Lib/test/test_ctypes/test_unions.py @@ -1,7 +1,7 @@ import unittest from ctypes import Union -from .support import (_CData, UnionType, PY_TPFLAGS_DISALLOW_INSTANTIATION, - PY_TPFLAGS_IMMUTABLETYPE) +from .support import (_CData, UnionType, Py_TPFLAGS_DISALLOW_INSTANTIATION, + Py_TPFLAGS_IMMUTABLETYPE) class ArrayTestCase(unittest.TestCase): @@ -12,8 +12,8 @@ def test_inheritance_hierarchy(self): self.assertEqual(type(UnionType), type) def test_type_flags(self): - self.assertTrue(Union.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(Union.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) + self.assertTrue(Union.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(Union.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) - self.assertTrue(UnionType.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(UnionType.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) + self.assertTrue(UnionType.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(UnionType.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) diff --git a/Lib/test/test_ctypes/test_win32.py b/Lib/test/test_ctypes/test_win32.py index e4ab8d41741d3d..b9fb3097f50db8 100644 --- a/Lib/test/test_ctypes/test_win32.py +++ b/Lib/test/test_ctypes/test_win32.py @@ -9,7 +9,7 @@ _pointer_type_cache, c_void_p, c_char, c_int, c_long) from test import support -from .support import PY_TPFLAGS_DISALLOW_INSTANTIATION, PY_TPFLAGS_IMMUTABLETYPE +from .support import Py_TPFLAGS_DISALLOW_INSTANTIATION, Py_TPFLAGS_IMMUTABLETYPE @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test') @@ -76,8 +76,8 @@ def test_COMError(self): self.assertEqual(COMError.mro(), [COMError, Exception, BaseException, object]) - self.assertFalse(COMError.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION) - self.assertTrue(COMError.__flags__ & PY_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(COMError.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) + self.assertTrue(COMError.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test') From 6a0676c273c486e5a43f5f836c67d2f40d66ed25 Mon Sep 17 00:00:00 2001 From: AN Long Date: Tue, 9 Jan 2024 20:39:38 +0800 Subject: [PATCH 11/16] move CField to support.py --- Lib/test/test_ctypes/support.py | 5 +++++ Lib/test/test_ctypes/test_struct_fields.py | 15 ++++++--------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Lib/test/test_ctypes/support.py b/Lib/test/test_ctypes/support.py index fe7c9e6b25baf9..e4c2b33825ae8f 100644 --- a/Lib/test/test_ctypes/support.py +++ b/Lib/test/test_ctypes/support.py @@ -1,11 +1,16 @@ # Some classes and types are not export to _ctypes module directly. +import ctypes from _ctypes import Structure, Union, _Pointer, Array, _SimpleCData, CFuncPtr _CData = Structure.__base__ assert _CData.__name__ == "_CData" +class _X(Structure): + _fields_ = [("x", ctypes.c_int)] +CField = type(_X.x) + # metaclasses PyCStructType = type(Structure) UnionType = type(Union) diff --git a/Lib/test/test_ctypes/test_struct_fields.py b/Lib/test/test_ctypes/test_struct_fields.py index 45e06e2e2e5fdf..987b9be9ea72a1 100644 --- a/Lib/test/test_ctypes/test_struct_fields.py +++ b/Lib/test/test_ctypes/test_struct_fields.py @@ -1,6 +1,7 @@ import unittest from ctypes import Structure, Union, sizeof, c_char, c_int -from .support import Py_TPFLAGS_DISALLOW_INSTANTIATION, Py_TPFLAGS_IMMUTABLETYPE +from .support import (CField, Py_TPFLAGS_DISALLOW_INSTANTIATION, + Py_TPFLAGS_IMMUTABLETYPE) class StructFieldsTestCase(unittest.TestCase): @@ -13,10 +14,6 @@ class StructFieldsTestCase(unittest.TestCase): # 4. The type is subclassed # # When they are finalized, assigning _fields_ is no longer allowed. - class X(Structure): - _fields_ = [("x", c_int)] - CField = type(X.x) - def test_1_A(self): class X(Structure): pass @@ -60,14 +57,14 @@ class X(Structure): self.assertEqual(bytes(x), b'a\x00###') def test_6(self): - self.assertRaises(TypeError, self.CField) + self.assertRaises(TypeError, CField) def test_cfield_type_flags(self): - self.assertTrue(self.CField.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) - self.assertTrue(self.CField.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertTrue(CField.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) + self.assertTrue(CField.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) def test_cfield_inheritance_hierarchy(self): - self.assertEqual(self.CField.mro(), [self.CField, object]) + self.assertEqual(CField.mro(), [CField, object]) def test_gh99275(self): class BrokenStructure(Structure): From 37aecd4853e6ffba68c616c6583fa3ecb7554ac9 Mon Sep 17 00:00:00 2001 From: AN Long Date: Tue, 9 Jan 2024 20:59:58 +0800 Subject: [PATCH 12/16] test the type flags with sub test --- Lib/test/test_ctypes/test_arrays.py | 10 ++++++---- Lib/test/test_ctypes/test_funcptr.py | 10 ++++++---- Lib/test/test_ctypes/test_pointers.py | 10 ++++++---- Lib/test/test_ctypes/test_simplesubclasses.py | 10 ++++++---- Lib/test/test_ctypes/test_structures.py | 10 ++++++---- Lib/test/test_ctypes/test_unions.py | 10 ++++++---- 6 files changed, 36 insertions(+), 24 deletions(-) diff --git a/Lib/test/test_ctypes/test_arrays.py b/Lib/test/test_ctypes/test_arrays.py index f32e912149e9f6..1a0789f3bffc2f 100644 --- a/Lib/test/test_ctypes/test_arrays.py +++ b/Lib/test/test_ctypes/test_arrays.py @@ -32,11 +32,13 @@ def test_inheritance_hierarchy(self): self.assertEqual(type(PyCArrayType), type) def test_type_flags(self): - self.assertTrue(Array.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(Array.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) + with self.subTest(cls=Array): + self.assertTrue(Array.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(Array.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) - self.assertTrue(PyCArrayType.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(PyCArrayType.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) + with self.subTest(cls=PyCArrayType): + self.assertTrue(PyCArrayType.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(PyCArrayType.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) def test_simple(self): # create classes holding simple numeric types, and check diff --git a/Lib/test/test_ctypes/test_funcptr.py b/Lib/test/test_ctypes/test_funcptr.py index 1745f224b82963..af41d85ab50e63 100644 --- a/Lib/test/test_ctypes/test_funcptr.py +++ b/Lib/test/test_ctypes/test_funcptr.py @@ -24,11 +24,13 @@ def test_inheritance_hierarchy(self): self.assertEqual(type(PyCFuncPtrType), type) def test_type_flags(self): - self.assertTrue(_CFuncPtr.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(_CFuncPtr.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) + with self.subTest(cls=_CFuncPtr): + self.assertTrue(_CFuncPtr.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(_CFuncPtr.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) - self.assertTrue(PyCFuncPtrType.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(PyCFuncPtrType.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) + with self.subTest(cls=PyCFuncPtrType): + self.assertTrue(PyCFuncPtrType.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(PyCFuncPtrType.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) def test_basic(self): X = WINFUNCTYPE(c_int, c_int, c_int) diff --git a/Lib/test/test_ctypes/test_pointers.py b/Lib/test/test_ctypes/test_pointers.py index 864de2a49b5111..bb7867af80cdaf 100644 --- a/Lib/test/test_ctypes/test_pointers.py +++ b/Lib/test/test_ctypes/test_pointers.py @@ -28,11 +28,13 @@ def test_inheritance_hierarchy(self): self.assertEqual(type(PyCPointerType), type) def test_type_flags(self): - self.assertTrue(_Pointer.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(_Pointer.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) + with self.subTest(cls=_Pointer): + self.assertTrue(_Pointer.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(_Pointer.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) - self.assertTrue(PyCPointerType.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(PyCPointerType.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) + with self.subTest(cls=PyCPointerType): + self.assertTrue(PyCPointerType.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(PyCPointerType.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) def test_pointer_crash(self): diff --git a/Lib/test/test_ctypes/test_simplesubclasses.py b/Lib/test/test_ctypes/test_simplesubclasses.py index b75f4f09052d74..90865e9c7ce007 100644 --- a/Lib/test/test_ctypes/test_simplesubclasses.py +++ b/Lib/test/test_ctypes/test_simplesubclasses.py @@ -21,11 +21,13 @@ def test_inheritance_hierarchy(self): self.assertEqual(c_int.mro(), [c_int, _SimpleCData, _CData, object]) def test_type_flags(self): - self.assertTrue(_SimpleCData.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(_SimpleCData.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) + with self.subTest(cls=_SimpleCData): + self.assertTrue(_SimpleCData.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(_SimpleCData.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) - self.assertTrue(PyCSimpleType.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(PyCSimpleType.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) + with self.subTest(cls=PyCSimpleType): + self.assertTrue(PyCSimpleType.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(PyCSimpleType.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) def test_compare(self): self.assertEqual(MyInt(3), MyInt(3)) diff --git a/Lib/test/test_ctypes/test_structures.py b/Lib/test/test_ctypes/test_structures.py index 38cda833176178..ee61925b56ed92 100644 --- a/Lib/test/test_ctypes/test_structures.py +++ b/Lib/test/test_ctypes/test_structures.py @@ -80,11 +80,13 @@ def test_inheritance_hierarchy(self): def test_type_flags(self): - self.assertTrue(Structure.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(Structure.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) + with self.subTest(cls=Structure): + self.assertTrue(Structure.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(Structure.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) - self.assertTrue(PyCStructType.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(PyCStructType.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) + with self.subTest(cls=PyCStructType): + self.assertTrue(PyCStructType.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(PyCStructType.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) def test_simple_structs(self): for code, tp in self.formats.items(): diff --git a/Lib/test/test_ctypes/test_unions.py b/Lib/test/test_ctypes/test_unions.py index 20b1b65f3fffaa..7fac0abb71acaf 100644 --- a/Lib/test/test_ctypes/test_unions.py +++ b/Lib/test/test_ctypes/test_unions.py @@ -12,8 +12,10 @@ def test_inheritance_hierarchy(self): self.assertEqual(type(UnionType), type) def test_type_flags(self): - self.assertTrue(Union.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(Union.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) + with self.subTest(cls=Union): + self.assertTrue(Union.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(Union.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) - self.assertTrue(UnionType.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(UnionType.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) + with self.subTest(cls=UnionType): + self.assertTrue(UnionType.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(UnionType.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) From 3ccb9648c02ea7daa1fbebd58a04097b6a5ab42f Mon Sep 17 00:00:00 2001 From: AN Long Date: Tue, 9 Jan 2024 21:08:20 +0800 Subject: [PATCH 13/16] rename support.py to _support.py to avoid ambiguous when import --- Lib/test/test_ctypes/{support.py => _support.py} | 0 Lib/test/test_ctypes/test_arrays.py | 2 +- Lib/test/test_ctypes/test_funcptr.py | 2 +- Lib/test/test_ctypes/test_pointers.py | 2 +- Lib/test/test_ctypes/test_simplesubclasses.py | 2 +- Lib/test/test_ctypes/test_struct_fields.py | 2 +- Lib/test/test_ctypes/test_structures.py | 2 +- Lib/test/test_ctypes/test_unions.py | 2 +- Lib/test/test_ctypes/test_win32.py | 2 +- 9 files changed, 8 insertions(+), 8 deletions(-) rename Lib/test/test_ctypes/{support.py => _support.py} (100%) diff --git a/Lib/test/test_ctypes/support.py b/Lib/test/test_ctypes/_support.py similarity index 100% rename from Lib/test/test_ctypes/support.py rename to Lib/test/test_ctypes/_support.py diff --git a/Lib/test/test_ctypes/test_arrays.py b/Lib/test/test_ctypes/test_arrays.py index 1a0789f3bffc2f..d7eca48e1c6260 100644 --- a/Lib/test/test_ctypes/test_arrays.py +++ b/Lib/test/test_ctypes/test_arrays.py @@ -7,7 +7,7 @@ c_char, c_wchar, c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, c_long, c_ulonglong, c_float, c_double, c_longdouble) from test.support import bigmemtest, _2G -from .support import (_CData, PyCArrayType, Py_TPFLAGS_DISALLOW_INSTANTIATION, +from ._support import (_CData, PyCArrayType, Py_TPFLAGS_DISALLOW_INSTANTIATION, Py_TPFLAGS_IMMUTABLETYPE) diff --git a/Lib/test/test_ctypes/test_funcptr.py b/Lib/test/test_ctypes/test_funcptr.py index af41d85ab50e63..654ed8d6d1ed33 100644 --- a/Lib/test/test_ctypes/test_funcptr.py +++ b/Lib/test/test_ctypes/test_funcptr.py @@ -3,7 +3,7 @@ import unittest from ctypes import (CDLL, Structure, CFUNCTYPE, sizeof, _CFuncPtr, c_void_p, c_char_p, c_char, c_int, c_uint, c_long) -from .support import (_CData, PyCFuncPtrType, Py_TPFLAGS_DISALLOW_INSTANTIATION, +from ._support import (_CData, PyCFuncPtrType, Py_TPFLAGS_DISALLOW_INSTANTIATION, Py_TPFLAGS_IMMUTABLETYPE) diff --git a/Lib/test/test_ctypes/test_pointers.py b/Lib/test/test_ctypes/test_pointers.py index bb7867af80cdaf..e8baf81eb2fe41 100644 --- a/Lib/test/test_ctypes/test_pointers.py +++ b/Lib/test/test_ctypes/test_pointers.py @@ -10,7 +10,7 @@ c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong, c_float, c_double) -from .support import (_CData, PyCPointerType, Py_TPFLAGS_DISALLOW_INSTANTIATION, +from ._support import (_CData, PyCPointerType, Py_TPFLAGS_DISALLOW_INSTANTIATION, Py_TPFLAGS_IMMUTABLETYPE) diff --git a/Lib/test/test_ctypes/test_simplesubclasses.py b/Lib/test/test_ctypes/test_simplesubclasses.py index 90865e9c7ce007..19c4f090251de2 100644 --- a/Lib/test/test_ctypes/test_simplesubclasses.py +++ b/Lib/test/test_ctypes/test_simplesubclasses.py @@ -1,6 +1,6 @@ import unittest from ctypes import Structure, CFUNCTYPE, c_int, _SimpleCData -from .support import (_CData, PyCSimpleType, Py_TPFLAGS_DISALLOW_INSTANTIATION, +from ._support import (_CData, PyCSimpleType, Py_TPFLAGS_DISALLOW_INSTANTIATION, Py_TPFLAGS_IMMUTABLETYPE) diff --git a/Lib/test/test_ctypes/test_struct_fields.py b/Lib/test/test_ctypes/test_struct_fields.py index 987b9be9ea72a1..29dc04a0beec4a 100644 --- a/Lib/test/test_ctypes/test_struct_fields.py +++ b/Lib/test/test_ctypes/test_struct_fields.py @@ -1,6 +1,6 @@ import unittest from ctypes import Structure, Union, sizeof, c_char, c_int -from .support import (CField, Py_TPFLAGS_DISALLOW_INSTANTIATION, +from ._support import (CField, Py_TPFLAGS_DISALLOW_INSTANTIATION, Py_TPFLAGS_IMMUTABLETYPE) diff --git a/Lib/test/test_ctypes/test_structures.py b/Lib/test/test_ctypes/test_structures.py index ee61925b56ed92..20f6365929adaf 100644 --- a/Lib/test/test_ctypes/test_structures.py +++ b/Lib/test/test_ctypes/test_structures.py @@ -12,7 +12,7 @@ from struct import calcsize from collections import namedtuple from test import support -from .support import (_CData, PyCStructType, Py_TPFLAGS_DISALLOW_INSTANTIATION, +from ._support import (_CData, PyCStructType, Py_TPFLAGS_DISALLOW_INSTANTIATION, Py_TPFLAGS_IMMUTABLETYPE) diff --git a/Lib/test/test_ctypes/test_unions.py b/Lib/test/test_ctypes/test_unions.py index 7fac0abb71acaf..2f5446e13d8a05 100644 --- a/Lib/test/test_ctypes/test_unions.py +++ b/Lib/test/test_ctypes/test_unions.py @@ -1,6 +1,6 @@ import unittest from ctypes import Union -from .support import (_CData, UnionType, Py_TPFLAGS_DISALLOW_INSTANTIATION, +from ._support import (_CData, UnionType, Py_TPFLAGS_DISALLOW_INSTANTIATION, Py_TPFLAGS_IMMUTABLETYPE) diff --git a/Lib/test/test_ctypes/test_win32.py b/Lib/test/test_ctypes/test_win32.py index b9fb3097f50db8..4aaecd8d38f98f 100644 --- a/Lib/test/test_ctypes/test_win32.py +++ b/Lib/test/test_ctypes/test_win32.py @@ -9,7 +9,7 @@ _pointer_type_cache, c_void_p, c_char, c_int, c_long) from test import support -from .support import Py_TPFLAGS_DISALLOW_INSTANTIATION, Py_TPFLAGS_IMMUTABLETYPE +from ._support import Py_TPFLAGS_DISALLOW_INSTANTIATION, Py_TPFLAGS_IMMUTABLETYPE @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test') From 0ac92f023913aba7896a59be118e9f44228519d7 Mon Sep 17 00:00:00 2001 From: AN Long Date: Tue, 9 Jan 2024 21:11:29 +0800 Subject: [PATCH 14/16] align the import statement --- Lib/test/test_ctypes/test_arrays.py | 2 +- Lib/test/test_ctypes/test_funcptr.py | 2 +- Lib/test/test_ctypes/test_pointers.py | 2 +- Lib/test/test_ctypes/test_simplesubclasses.py | 2 +- Lib/test/test_ctypes/test_struct_fields.py | 2 +- Lib/test/test_ctypes/test_structures.py | 2 +- Lib/test/test_ctypes/test_unions.py | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_ctypes/test_arrays.py b/Lib/test/test_ctypes/test_arrays.py index d7eca48e1c6260..a83699a4b4b94c 100644 --- a/Lib/test/test_ctypes/test_arrays.py +++ b/Lib/test/test_ctypes/test_arrays.py @@ -8,7 +8,7 @@ c_long, c_ulonglong, c_float, c_double, c_longdouble) from test.support import bigmemtest, _2G from ._support import (_CData, PyCArrayType, Py_TPFLAGS_DISALLOW_INSTANTIATION, - Py_TPFLAGS_IMMUTABLETYPE) + Py_TPFLAGS_IMMUTABLETYPE) formats = "bBhHiIlLqQfd" diff --git a/Lib/test/test_ctypes/test_funcptr.py b/Lib/test/test_ctypes/test_funcptr.py index 654ed8d6d1ed33..8f21e7ebf7a6be 100644 --- a/Lib/test/test_ctypes/test_funcptr.py +++ b/Lib/test/test_ctypes/test_funcptr.py @@ -4,7 +4,7 @@ from ctypes import (CDLL, Structure, CFUNCTYPE, sizeof, _CFuncPtr, c_void_p, c_char_p, c_char, c_int, c_uint, c_long) from ._support import (_CData, PyCFuncPtrType, Py_TPFLAGS_DISALLOW_INSTANTIATION, - Py_TPFLAGS_IMMUTABLETYPE) + Py_TPFLAGS_IMMUTABLETYPE) try: diff --git a/Lib/test/test_ctypes/test_pointers.py b/Lib/test/test_ctypes/test_pointers.py index e8baf81eb2fe41..5474375c0da0fc 100644 --- a/Lib/test/test_ctypes/test_pointers.py +++ b/Lib/test/test_ctypes/test_pointers.py @@ -11,7 +11,7 @@ c_long, c_ulong, c_longlong, c_ulonglong, c_float, c_double) from ._support import (_CData, PyCPointerType, Py_TPFLAGS_DISALLOW_INSTANTIATION, - Py_TPFLAGS_IMMUTABLETYPE) + Py_TPFLAGS_IMMUTABLETYPE) ctype_types = [c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, diff --git a/Lib/test/test_ctypes/test_simplesubclasses.py b/Lib/test/test_ctypes/test_simplesubclasses.py index 19c4f090251de2..cbaef5dd4cd139 100644 --- a/Lib/test/test_ctypes/test_simplesubclasses.py +++ b/Lib/test/test_ctypes/test_simplesubclasses.py @@ -1,7 +1,7 @@ import unittest from ctypes import Structure, CFUNCTYPE, c_int, _SimpleCData from ._support import (_CData, PyCSimpleType, Py_TPFLAGS_DISALLOW_INSTANTIATION, - Py_TPFLAGS_IMMUTABLETYPE) + Py_TPFLAGS_IMMUTABLETYPE) class MyInt(c_int): diff --git a/Lib/test/test_ctypes/test_struct_fields.py b/Lib/test/test_ctypes/test_struct_fields.py index 29dc04a0beec4a..f474a02fa8db06 100644 --- a/Lib/test/test_ctypes/test_struct_fields.py +++ b/Lib/test/test_ctypes/test_struct_fields.py @@ -1,7 +1,7 @@ import unittest from ctypes import Structure, Union, sizeof, c_char, c_int from ._support import (CField, Py_TPFLAGS_DISALLOW_INSTANTIATION, - Py_TPFLAGS_IMMUTABLETYPE) + Py_TPFLAGS_IMMUTABLETYPE) class StructFieldsTestCase(unittest.TestCase): diff --git a/Lib/test/test_ctypes/test_structures.py b/Lib/test/test_ctypes/test_structures.py index 20f6365929adaf..cf19823d36d3ab 100644 --- a/Lib/test/test_ctypes/test_structures.py +++ b/Lib/test/test_ctypes/test_structures.py @@ -13,7 +13,7 @@ from collections import namedtuple from test import support from ._support import (_CData, PyCStructType, Py_TPFLAGS_DISALLOW_INSTANTIATION, - Py_TPFLAGS_IMMUTABLETYPE) + Py_TPFLAGS_IMMUTABLETYPE) class SubclassesTest(unittest.TestCase): diff --git a/Lib/test/test_ctypes/test_unions.py b/Lib/test/test_ctypes/test_unions.py index 2f5446e13d8a05..6d420627e908c2 100644 --- a/Lib/test/test_ctypes/test_unions.py +++ b/Lib/test/test_ctypes/test_unions.py @@ -1,7 +1,7 @@ import unittest from ctypes import Union from ._support import (_CData, UnionType, Py_TPFLAGS_DISALLOW_INSTANTIATION, - Py_TPFLAGS_IMMUTABLETYPE) + Py_TPFLAGS_IMMUTABLETYPE) class ArrayTestCase(unittest.TestCase): From efd0d9fa7b01fdaf19fe1f2bc16a4147a6d63957 Mon Sep 17 00:00:00 2001 From: AN Long Date: Tue, 9 Jan 2024 22:48:51 +0800 Subject: [PATCH 15/16] Update Lib/test/test_ctypes/test_arrays.py Co-authored-by: Erlend E. Aasland --- Lib/test/test_ctypes/test_arrays.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_ctypes/test_arrays.py b/Lib/test/test_ctypes/test_arrays.py index a83699a4b4b94c..774316e227ff73 100644 --- a/Lib/test/test_ctypes/test_arrays.py +++ b/Lib/test/test_ctypes/test_arrays.py @@ -32,13 +32,10 @@ def test_inheritance_hierarchy(self): self.assertEqual(type(PyCArrayType), type) def test_type_flags(self): - with self.subTest(cls=Array): - self.assertTrue(Array.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(Array.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) - - with self.subTest(cls=PyCArrayType): - self.assertTrue(PyCArrayType.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(PyCArrayType.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) + for cls in Array, PyCArrayType: + with self.subTest(cls=cls): + self.assertTrue(cls.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(cls.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) def test_simple(self): # create classes holding simple numeric types, and check From 2660e86f688e97587db494ab2d1779b3c1162d99 Mon Sep 17 00:00:00 2001 From: AN Long Date: Tue, 9 Jan 2024 22:57:03 +0800 Subject: [PATCH 16/16] refactor the type flags check --- Lib/test/test_ctypes/test_funcptr.py | 11 ++++------- Lib/test/test_ctypes/test_pointers.py | 11 ++++------- Lib/test/test_ctypes/test_simplesubclasses.py | 11 ++++------- Lib/test/test_ctypes/test_structures.py | 11 ++++------- Lib/test/test_ctypes/test_unions.py | 11 ++++------- 5 files changed, 20 insertions(+), 35 deletions(-) diff --git a/Lib/test/test_ctypes/test_funcptr.py b/Lib/test/test_ctypes/test_funcptr.py index 8f21e7ebf7a6be..0eed39484fb39e 100644 --- a/Lib/test/test_ctypes/test_funcptr.py +++ b/Lib/test/test_ctypes/test_funcptr.py @@ -24,13 +24,10 @@ def test_inheritance_hierarchy(self): self.assertEqual(type(PyCFuncPtrType), type) def test_type_flags(self): - with self.subTest(cls=_CFuncPtr): - self.assertTrue(_CFuncPtr.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(_CFuncPtr.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) - - with self.subTest(cls=PyCFuncPtrType): - self.assertTrue(PyCFuncPtrType.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(PyCFuncPtrType.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) + for cls in _CFuncPtr, PyCFuncPtrType: + with self.subTest(cls=cls): + self.assertTrue(_CFuncPtr.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(_CFuncPtr.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) def test_basic(self): X = WINFUNCTYPE(c_int, c_int, c_int) diff --git a/Lib/test/test_ctypes/test_pointers.py b/Lib/test/test_ctypes/test_pointers.py index 5474375c0da0fc..8cf2114c282cab 100644 --- a/Lib/test/test_ctypes/test_pointers.py +++ b/Lib/test/test_ctypes/test_pointers.py @@ -28,13 +28,10 @@ def test_inheritance_hierarchy(self): self.assertEqual(type(PyCPointerType), type) def test_type_flags(self): - with self.subTest(cls=_Pointer): - self.assertTrue(_Pointer.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(_Pointer.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) - - with self.subTest(cls=PyCPointerType): - self.assertTrue(PyCPointerType.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(PyCPointerType.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) + for cls in _Pointer, PyCPointerType: + with self.subTest(cls=cls): + self.assertTrue(_Pointer.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(_Pointer.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) def test_pointer_crash(self): diff --git a/Lib/test/test_ctypes/test_simplesubclasses.py b/Lib/test/test_ctypes/test_simplesubclasses.py index cbaef5dd4cd139..c96798e67f23f7 100644 --- a/Lib/test/test_ctypes/test_simplesubclasses.py +++ b/Lib/test/test_ctypes/test_simplesubclasses.py @@ -21,13 +21,10 @@ def test_inheritance_hierarchy(self): self.assertEqual(c_int.mro(), [c_int, _SimpleCData, _CData, object]) def test_type_flags(self): - with self.subTest(cls=_SimpleCData): - self.assertTrue(_SimpleCData.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(_SimpleCData.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) - - with self.subTest(cls=PyCSimpleType): - self.assertTrue(PyCSimpleType.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(PyCSimpleType.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) + for cls in _SimpleCData, PyCSimpleType: + with self.subTest(cls=cls): + self.assertTrue(_SimpleCData.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(_SimpleCData.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) def test_compare(self): self.assertEqual(MyInt(3), MyInt(3)) diff --git a/Lib/test/test_ctypes/test_structures.py b/Lib/test/test_ctypes/test_structures.py index cf19823d36d3ab..98bc4bdcac9306 100644 --- a/Lib/test/test_ctypes/test_structures.py +++ b/Lib/test/test_ctypes/test_structures.py @@ -80,13 +80,10 @@ def test_inheritance_hierarchy(self): def test_type_flags(self): - with self.subTest(cls=Structure): - self.assertTrue(Structure.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(Structure.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) - - with self.subTest(cls=PyCStructType): - self.assertTrue(PyCStructType.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(PyCStructType.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) + for cls in Structure, PyCStructType: + with self.subTest(cls=cls): + self.assertTrue(Structure.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(Structure.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) def test_simple_structs(self): for code, tp in self.formats.items(): diff --git a/Lib/test/test_ctypes/test_unions.py b/Lib/test/test_ctypes/test_unions.py index 6d420627e908c2..cf5344bdf19165 100644 --- a/Lib/test/test_ctypes/test_unions.py +++ b/Lib/test/test_ctypes/test_unions.py @@ -12,10 +12,7 @@ def test_inheritance_hierarchy(self): self.assertEqual(type(UnionType), type) def test_type_flags(self): - with self.subTest(cls=Union): - self.assertTrue(Union.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(Union.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) - - with self.subTest(cls=UnionType): - self.assertTrue(UnionType.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) - self.assertFalse(UnionType.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) + for cls in Union, UnionType: + with self.subTest(cls=Union): + self.assertTrue(Union.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) + self.assertFalse(Union.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION)