8000 gh-103092: Test `_ctypes` type hierarchy and features by aisk · Pull Request #113727 · python/cpython · GitHub
[go: up one dir, main page]

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 1 commit
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
Prev Previous commit
Next Next commit
change more test to check the type.__flags__
  • Loading branch information
aisk committed Jan 8, 2024
commit 1e7c0642cffabdd85dc2b8541b1da83ecef9d361
26 changes: 9 additions & 17 deletions Lib/test/test_ctypes/test_pointers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,29 @@
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,
c_long, c_ulong, c_longlong, c_ulonglong, c_double, c_float]
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):

Expand Down
26 changes: 12 additions & 14 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 @@ -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):
Expand Down
8 changes: 5 additions & 3 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,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')
Expand Down
0