| 1 |
import array |
| 2 |
import ctypes |
| 3 |
import struct |
| 4 |
import sys |
| 5 |
import unittest |
| 6 |
from itertools import combinations |
| 7 |
from operator import truth |
| 8 |
from ctypes import (byref, sizeof, alignment, |
| 9 |
c_char, c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, |
| 10 |
c_long, c_ulong, c_longlong, c_ulonglong, |
| 11 |
c_float, c_double, c_longdouble, c_bool) |
| 12 |
from test.support.testcase import ComplexesAreIdenticalMixin |
| 13 |
|
| 14 |
|
| 15 |
def valid_ranges(*types): |
| 16 |
# given a sequence of numeric types, collect their _type_ |
| 17 |
# attribute, which is a single format character compatible with |
| 18 |
# the struct module, use the struct module to calculate the |
| 19 |
# minimum and maximum value allowed for this format. |
| 20 |
# Returns a list of (min, max) values. |
| 21 |
result = [] |
| 22 |
for t in types: |
| 23 |
fmt = t._type_ |
| 24 |
size = struct.calcsize(fmt) |
| 25 |
a = struct.unpack(fmt, (b"\x00"*32)[:size])[0] |
| 26 |
b = struct.unpack(fmt, (b"\xFF"*32)[:size])[0] |
| 27 |
c = struct.unpack(fmt, (b"\x7F"+b"\x00"*32)[:size])[0] |
| 28 |
d = struct.unpack(fmt, (b"\x80"+b"\xFF"*32)[:size])[0] |
| 29 |
result.append((min(a, b, c, d), max(a, b, c, d))) |
| 30 |
return result |
| 31 |
|
| 32 |
|
| 33 |
ArgType = type(byref(c_int(0))) |
| 34 |
|
| 35 |
unsigned_types = [c_ubyte, c_ushort, c_uint, c_ulong, c_ulonglong] |
| 36 |
signed_types = [c_byte, c_short, c_int, c_long, c_longlong] |
| 37 |
bool_types = [c_bool] |
| 38 |
float_types = [c_double, c_float] |
| 39 |
|
| 40 |
unsigned_ranges = valid_ranges(*unsigned_types) |
| 41 |
signed_ranges = valid_ranges(*signed_types) |
| 42 |
bool_values = [True, False, 0, 1, -1, 5000, 'test', [], [1]] |
| 43 |
|
| 44 |
class IntLike: |
| 45 |
def __int__(self): |
| 46 |
return 2 |
| 47 |
|
| 48 |
class IndexLike: |
| 49 |
def __index__(self): |
| 50 |
return 2 |
| 51 |
|
| 52 |
class FloatLike: |
| 53 |
def __float__(self): |
| 54 |
return 2.0 |
| 55 |
|
| 56 |
class ComplexLike: |
| 57 |
def __complex__(self): |
| 58 |
return 1+1j |
| 59 |
|
| 60 |
|
| 61 |
INF = float("inf") |
| 62 |
NAN = float("nan") |
| 63 |
|
| 64 |
|
| 65 |
class NumberTestCase(unittest.TestCase, ComplexesAreIdenticalMixin): |
| 66 |
|
| 67 |
def test_default_init(self): |
| 68 |
# default values are set to zero |
| 69 |
for t in signed_types + unsigned_types + float_types: |
| 70 |
self.assertEqual(t().value, 0) |
| 71 |
|
| 72 |
def test_unsigned_values(self): |
| 73 |
# the value given to the constructor is available |
| 74 |
# as the 'value' attribute |
| 75 |
for t, (l, h) in zip(unsigned_types, unsigned_ranges): |
| 76 |
self.assertEqual(t(l).value, l) |
| 77 |
self.assertEqual(t(h).value, h) |
| 78 |
|
| 79 |
def test_signed_values(self): |
| 80 |
# see above |
| 81 |
for t, (l, h) in zip(signed_types, signed_ranges): |
| 82 |
self.assertEqual(t(l).value, l) |
| 83 |
self.assertEqual(t(h).value, h) |
| 84 |
|
| 85 |
def test_bool_values(self): |
| 86 |
for t, v in zip(bool_types, bool_values): |
| 87 |
self.assertEqual(t(v).value, truth(v)) |
| 88 |
|
| 89 |
def test_typeerror(self): |
| 90 |
# Only numbers are allowed in the constructor, |
| 91 |
# otherwise TypeError is raised |
| 92 |
for t in signed_types + unsigned_types + float_types: |
| 93 |
self.assertRaises(TypeError, t, "") |
| 94 |
self.assertRaises(TypeError, t, None) |
| 95 |
|
| 96 |
def test_from_param(self): |
| 97 |
# the from_param class method attribute always |
| 98 |
# returns PyCArgObject instances |
| 99 |
for t in signed_types + unsigned_types + float_types: |
| 100 |
self.assertEqual(ArgType, type(t.from_param(0))) |
| 101 |
|
| 102 |
def test_byref(self): |
| 103 |
# calling byref returns also a PyCArgObject instance |
| 104 |
for t in signed_types + unsigned_types + float_types + bool_types: |
| 105 |
parm = byref(t()) |
| 106 |
self.assertEqual(ArgType, type(parm)) |
| 107 |
|
| 108 |
|
| 109 |
def test_floats(self): |
| 110 |
# c_float and c_double can be created from |
| 111 |
# Python int and float |
| 112 |
f = FloatLike() |
| 113 |
for t in float_types: |
| 114 |
self.assertEqual(t(2.0).value, 2.0) |
| 115 |
self.assertEqual(t(2).value, 2.0) |
| 116 |
self.assertEqual(t(2).value, 2.0) |
| 117 |
self.assertEqual(t(f).value, 2.0) |
| 118 |
|
| 119 |
@unittest.skipUnless(hasattr(ctypes, "c_double_complex"), |
| 120 |
"requires C11 complex type") |
| 121 |
def test_complex(self): |
| 122 |
for t in [ctypes.c_double_complex, ctypes.c_float_complex, |
| 123 |
ctypes.c_longdouble_complex]: |
| 124 |
self.assertEqual(t(1).value, 1+0j) |
| 125 |
self.assertEqual(t(1.0).value, 1+0j) |
| 126 |
self.assertEqual(t(1+0.125j).value, 1+0.125j) |
| 127 |
self.assertEqual(t(IndexLike()).value, 2+0j) |
| 128 |
self.assertEqual(t(FloatLike()).value, 2+0j) |
| 129 |
self.assertEqual(t(ComplexLike()).value, 1+1j) |
| 130 |
|
| 131 |
@unittest.skipUnless(hasattr(ctypes, "c_double_complex"), |
| 132 |
"requires C11 complex type") |
| 133 |
def test_complex_round_trip(self): |
| 134 |
# Ensure complexes transformed exactly. The CMPLX macro should |
| 135 |
# preserve special components (like inf/nan or signed zero). |
| 136 |
values = [complex(*_) for _ in combinations([1, -1, 0.0, -0.0, 2, |
| 137 |
-3, INF, -INF, NAN], 2)] |
| 138 |
for z in values: |
| 139 |
for t in [ctypes.c_double_complex, ctypes.c_float_complex, |
| 140 |
ctypes.c_longdouble_complex]: |
| 141 |
with self.subTest(z=z, type=t): |
| 142 |
self.assertComplexesAreIdentical(z, t(z).value) |
| 143 |
|
| 144 |
def test_integers(self): |
| 145 |
f = FloatLike() |
| 146 |
d = IntLike() |
| 147 |
i = IndexLike() |
| 148 |
# integers cannot be constructed from floats, |
| 149 |
# but from integer-like objects |
| 150 |
for t in signed_types + unsigned_types: |
| 151 |
self.assertRaises(TypeError, t, 3.14) |
| 152 |
self.assertRaises(TypeError, t, f) |
| 153 |
self.assertRaises(TypeError, t, d) |
| 154 |
self.assertEqual(t(i).value, 2) |
| 155 |
|
| 156 |
def test_sizes(self): |
| 157 |
for t in signed_types + unsigned_types + float_types + bool_types: |
| 158 |
try: |
| 159 |
size = struct.calcsize(t._type_) |
| 160 |
except struct.error: |
| 161 |
continue |
| 162 |
# sizeof of the type... |
| 163 |
self.assertEqual(sizeof(t), size) |
| 164 |
# and sizeof of an instance |
| 165 |
self.assertEqual(sizeof(t()), size) |
| 166 |
|
| 167 |
def test_alignments(self): |
| 168 |
for t in signed_types + unsigned_types + float_types: |
| 169 |
code = t._type_ # the typecode |
| 170 |
align = struct.calcsize("c%c" % code) - struct.calcsize(code) |
| 171 |
|
| 172 |
# alignment of the type... |
| 173 |
self.assertEqual((code, alignment(t)), |
| 174 |
(code, align)) |
| 175 |
# and alignment of an instance |
| 176 |
self.assertEqual((code, alignment(t())), |
| 177 |
(code, align)) |
| 178 |
|
| 179 |
def test_int_from_address(self): |
| 180 |
for t in signed_types + unsigned_types: |
| 181 |
# the array module doesn't support all format codes |
| 182 |
# (no 'q' or 'Q') |
| 183 |
try: |
| 184 |
array.array(t._type_) |
| 185 |
except ValueError: |
| 186 |
continue |
| 187 |
a = array.array(t._type_, [100]) |
| 188 |
|
| 189 |
# v now is an integer at an 'external' memory location |
| 190 |
v = t.from_address(a.buffer_info()[0]) |
| 191 |
self.assertEqual(v.value, a[0]) |
| 192 |
self.assertEqual(type(v), t) |
| 193 |
|
| 194 |
# changing the value at the memory location changes v's value also |
| 195 |
a[0] = 42 |
| 196 |
self.assertEqual(v.value, a[0]) |
| 197 |
|
| 198 |
|
| 199 |
def test_float_from_address(self): |
| 200 |
for t in float_types: |
| 201 |
a = array.array(t._type_, [3.14]) |
| 202 |
v = t.from_address(a.buffer_info()[0]) |
| 203 |
self.assertEqual(v.value, a[0]) |
| 204 |
self.assertIs(type(v), t) |
| 205 |
a[0] = 2.3456e17 |
| 206 |
self.assertEqual(v.value, a[0]) |
| 207 |
self.assertIs(type(v), t) |
| 208 |
|
| 209 |
def test_char_from_address(self): |
| 210 |
a = array.array('b', [0]) |
| 211 |
a[0] = ord('x') |
| 212 |
v = c_char.from_address(a.buffer_info()[0]) |
| 213 |
self.assertEqual(v.value, b'x') |
| 214 |
self.assertIs(type(v), c_char) |
| 215 |
|
| 216 |
a[0] = ord('?') |
| 217 |
self.assertEqual(v.value, b'?') |
| 218 |
|
| 219 |
def test_init(self): |
| 220 |
# c_int() can be initialized from Python's int, and c_int. |
| 221 |
# Not from c_long or so, which seems strange, abc should |
| 222 |
# probably be changed: |
| 223 |
self.assertRaises(TypeError, c_int, c_long(42)) |
| 224 |
|
| 225 |
def test_float_overflow(self): |
| 226 |
big_int = int(sys.float_info.max) * 2 |
| 227 |
for t in float_types + [c_longdouble]: |
| 228 |
self.assertRaises(OverflowError, t, big_int) |
| 229 |
if (hasattr(t, "__ctype_be__")): |
| 230 |
self.assertRaises(OverflowError, t.__ctype_be__, big_int) |
| 231 |
if (hasattr(t, "__ctype_le__")): |
| 232 |
self.assertRaises(OverflowError, t.__ctype_le__, big_int) |
| 233 |
|
| 234 |
|
| 235 |
if __name__ == '__main__': |
| 236 |
unittest.main() |