FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-103092: Test `_ctypes` type hierarchy and features by aisk · Pull Request #113727 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension .py  (5) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
Next Next commit
add inheritance hierarchy and other tests for ctypes base types
  • Loading branch information
aisk committed Jan 4, 2024
commit 1d95bbcf95489cb3db0cac0018f3cbb3db024372
23 changes: 23 additions & 0 deletions Lib/test/test_ctypes/test_arrays.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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__

Comment thread
erlend-aasland marked this conversation as resolved.
Outdated

def ARRAY(*args):
# ignore DeprecationWarning in tests
Expand All @@ -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})
Comment thread
erlend-aasland marked this conversation as resolved.
Outdated

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.
Expand Down
23 changes: 23 additions & 0 deletions Lib/test/test_ctypes/test_pointers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down
28 changes: 27 additions & 1 deletion Lib/test/test_ctypes/test_simplesubclasses.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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))
Expand Down
23 changes: 23 additions & 0 deletions Lib/test/test_ctypes/test_structures.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
32 changes: 32 additions & 0 deletions Lib/test/test_ctypes/test_unions.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -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"
Comment thread
erlend-aasland marked this conversation as resolved.
Outdated

msg = "cannot set 'foo' attribute of immutable type"
with self.assertRaisesRegex(TypeError, msg):
UnionType.foo = "bar"

Back | FazBrowse Home | New Git URL