Skip to content

gh-103092: Test _ctypes type hierarchy and features #113727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Lib/test/test_ctypes/support.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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)

# type flags
PY_TPFLAGS_DISALLOW_INSTANTIATION = 1 << 7
PY_TPFLAGS_IMMUTABLETYPE = 1 << 8
15 changes: 15 additions & 0 deletions Lib/test/test_ctypes/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +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)


formats = "bBhHiIlLqQfd"
Expand All @@ -23,6 +25,19 @@ def ARRAY(*args):


class ArrayTestCase(unittest.TestCase):
def test_inheritance_hierarchy(self):
self.assertEqual(Array.mro(), [Array, _CData, object])

self.assertEqual(PyCArrayType.__name__, "PyCArrayType")
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(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
# various properties.
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_ctypes/test_funcptr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +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)


try:
Expand All @@ -15,6 +17,19 @@


class CFuncPtrTestCase(unittest.TestCase):
def test_inheritance_hierarchy(self):
self.assertEqual(_CFuncPtr.mro(), [_CFuncPtr, _CData, object])

self.assertEqual(PyCFuncPtrType.__name__, "PyCFuncPtrType")
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(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)

Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_ctypes/test_pointers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -19,6 +21,19 @@


class PointersTestCase(unittest.TestCase):
def test_inheritance_hierarchy(self):
self.assertEqual(_Pointer.mro(), [_Pointer, _CData, object])

self.assertEqual(PyCPointerType.__name__, "PyCPointerType")
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(PyCPointerType.__flags__ & PY_TPFLAGS_IMMUTABLETYPE)
self.assertFalse(PyCPointerType.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION)

def test_pointer_crash(self):

class A(POINTER(c_ulong)):
Expand Down
18 changes: 17 additions & 1 deletion Lib/test/test_ctypes/test_simplesubclasses.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import unittest
from ctypes import Structure, CFUNCTYPE, c_int
from ctypes import Structure, CFUNCTYPE, c_int, _SimpleCData
from .support import (_CData, PyCSimpleType, PY_TPFLAGS_DISALLOW_INSTANTIATION,
PY_TPFLAGS_IMMUTABLETYPE)


class MyInt(c_int):
Expand All @@ -10,6 +12,20 @@ def __eq__(self, other):


class Test(unittest.TestCase):
def test_inheritance_hierarchy(self):
self.assertEqual(_SimpleCData.mro(), [_SimpleCData, _CData, object])

self.assertEqual(PyCSimpleType.__name__, "PyCSimpleType")
self.assertEqual(type(PyCSimpleType), type)

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(PyCSimpleType.__flags__ & PY_TPFLAGS_IMMUTABLETYPE)
self.assertFalse(PyCSimpleType.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION)

def test_compare(self):
self.assertEqual(MyInt(3), MyInt(3))
Expand Down
16 changes: 12 additions & 4 deletions Lib/test/test_ctypes/test_struct_fields.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -56,10 +60,14 @@ class X(Structure):
self.assertEqual(bytes(x), b'a\x00###')

def test_6(self):
class X(Structure):
_fields_ = [("x", c_int)]
CField = type(X.x)
self.assertRaises(TypeError, CField)
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)

def test_cfield_inheritance_hierarchy(self):
self.assertEqual(self.CField.mro(), [self.CField, object])

def test_gh99275(self):
class BrokenStructure(Structure):
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_ctypes/test_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +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)


class SubclassesTest(unittest.TestCase):
Expand Down Expand Up @@ -70,6 +72,20 @@ class StructureTestCase(unittest.TestCase):
"d": c_double,
}

def test_inheritance_hierarchy(self):
self.assertEqual(Structure.mro(), [Structure, _CData, object])

self.assertEqual(PyCStructType.__name__, "PyCStructType")
self.assertEqual(type(PyCStructType), type)


def test_type_flags(self):
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)

def test_simple_structs(self):
for code, tp in self.formats.items():
class X(Structure):
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_ctypes/test_unions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import unittest
from ctypes import Union
from .support import (_CData, UnionType, PY_TPFLAGS_DISALLOW_INSTANTIATION,
PY_TPFLAGS_IMMUTABLETYPE)


class ArrayTestCase(unittest.TestCase):
def test_inheritance_hierarchy(self):
self.assertEqual(Union.mro(), [Union, _CData, object])

self.assertEqual(UnionType.__name__, "UnionType")
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(UnionType.__flags__ & PY_TPFLAGS_IMMUTABLETYPE)
self.assertFalse(UnionType.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION)
6 changes: 6 additions & 0 deletions Lib/test/test_ctypes/test_win32.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -73,6 +74,11 @@ def test_COMError(self):
self.assertEqual(ex.text, "text")
self.assertEqual(ex.details, ("details",))

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')
class TestWinError(unittest.TestCase):
Expand Down