forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pythongh-103092: Test _ctypes type hierarchy and features (python#113727
) Test the following features for _ctypes types: - disallow instantiation - inheritance (MRO) - immutability - type name The following _ctypes types are tested: - Array - CField - COMError - PyCArrayType - PyCFuncPtrType - PyCPointerType - PyCSimpleType - PyCStructType - Structure - Union - UnionType - _CFuncPtr - _Pointer - _SimpleCData Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
- Loading branch information
Showing
9 changed files
with
130 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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,24 @@ | ||
# 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) | ||
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 |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,18 @@ | ||
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): | ||
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) |
This file contains 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