| 1 |
import unittest |
| 2 |
from ctypes import Structure, CFUNCTYPE, c_int, _SimpleCData |
| 3 |
from ._support import (_CData, PyCSimpleType, Py_TPFLAGS_DISALLOW_INSTANTIATION, |
| 4 |
Py_TPFLAGS_IMMUTABLETYPE) |
| 5 |
|
| 6 |
|
| 7 |
class MyInt(c_int): |
| 8 |
def __eq__(self, other): |
| 9 |
if type(other) != MyInt: |
| 10 |
return NotImplementedError |
| 11 |
return self.value == other.value |
| 12 |
|
| 13 |
|
| 14 |
class Test(unittest.TestCase): |
| 15 |
def test_inheritance_hierarchy(self): |
| 16 |
self.assertEqual(_SimpleCData.mro(), [_SimpleCData, _CData, object]) |
| 17 |
|
| 18 |
self.assertEqual(PyCSimpleType.__name__, "PyCSimpleType") |
| 19 |
self.assertEqual(type(PyCSimpleType), type) |
| 20 |
|
| 21 |
self.assertEqual(c_int.mro(), [c_int, _SimpleCData, _CData, object]) |
| 22 |
|
| 23 |
def test_type_flags(self): |
| 24 |
for cls in _SimpleCData, PyCSimpleType: |
| 25 |
with self.subTest(cls=cls): |
| 26 |
self.assertTrue(_SimpleCData.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) |
| 27 |
self.assertFalse(_SimpleCData.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) |
| 28 |
|
| 29 |
def test_metaclass_details(self): |
| 30 |
# Abstract classes (whose metaclass __init__ was not called) can't be |
| 31 |
# instantiated directly |
| 32 |
NewT = PyCSimpleType.__new__(PyCSimpleType, 'NewT', (_SimpleCData,), {}) |
| 33 |
for cls in _SimpleCData, NewT: |
| 34 |
with self.subTest(cls=cls): |
| 35 |
with self.assertRaisesRegex(TypeError, "abstract class"): |
| 36 |
obj = cls() |
| 37 |
|
| 38 |
# Cannot call the metaclass __init__ more than once |
| 39 |
class T(_SimpleCData): |
| 40 |
_type_ = "i" |
| 41 |
with self.assertRaisesRegex(SystemError, "already initialized"): |
| 42 |
PyCSimpleType.__init__(T, 'ptr', (), {}) |
| 43 |
|
| 44 |
def test_swapped_type_creation(self): |
| 45 |
cls = PyCSimpleType.__new__(PyCSimpleType, '', (), {'_type_': 'i'}) |
| 46 |
with self.assertRaises(TypeError): |
| 47 |
PyCSimpleType.__init__(cls) |
| 48 |
PyCSimpleType.__init__(cls, '', (), {'_type_': 'i'}) |
| 49 |
self.assertEqual(cls.__ctype_le__.__dict__.get('_type_'), 'i') |
| 50 |
self.assertEqual(cls.__ctype_be__.__dict__.get('_type_'), 'i') |
| 51 |
|
| 52 |
def test_compare(self): |
| 53 |
self.assertEqual(MyInt(3), MyInt(3)) |
| 54 |
self.assertNotEqual(MyInt(42), MyInt(43)) |
| 55 |
|
| 56 |
def test_ignore_retval(self): |
| 57 |
# Test if the return value of a callback is ignored |
| 58 |
# if restype is None |
| 59 |
proto = CFUNCTYPE(None) |
| 60 |
def func(): |
| 61 |
return (1, "abc", None) |
| 62 |
|
| 63 |
cb = proto(func) |
| 64 |
self.assertEqual(None, cb()) |
| 65 |
|
| 66 |
|
| 67 |
def test_int_callback(self): |
| 68 |
args = [] |
| 69 |
def func(arg): |
| 70 |
args.append(arg) |
| 71 |
return arg |
| 72 |
|
| 73 |
cb = CFUNCTYPE(None, MyInt)(func) |
| 74 |
|
| 75 |
self.assertEqual(None, cb(42)) |
| 76 |
self.assertEqual(type(args[-1]), MyInt) |
| 77 |
|
| 78 |
cb = CFUNCTYPE(c_int, c_int)(func) |
| 79 |
|
| 80 |
self.assertEqual(42, cb(42)) |
| 81 |
self.assertEqual(type(args[-1]), int) |
| 82 |
|
| 83 |
def test_int_struct(self): |
| 84 |
class X(Structure): |
| 85 |
_fields_ = [("x", MyInt)] |
| 86 |
|
| 87 |
self.assertEqual(X().x, MyInt()) |
| 88 |
|
| 89 |
s = X() |
| 90 |
s.x = MyInt(42) |
| 91 |
|
| 92 |
self.assertEqual(s.x, MyInt(42)) |
| 93 |
|
| 94 |
|
| 95 |
if __name__ == "__main__": |
| 96 |
unittest.main() |