| 1 |
import array |
| 2 |
import ctypes |
| 3 |
import gc |
| 4 |
import sys |
| 5 |
import unittest |
| 6 |
from ctypes import (CDLL, CFUNCTYPE, Structure, |
| 7 |
POINTER, pointer, _Pointer, |
| 8 |
byref, sizeof, |
| 9 |
c_void_p, c_char_p, |
| 10 |
c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, |
| 11 |
c_long, c_ulong, c_longlong, c_ulonglong, |
| 12 |
c_float, c_double) |
| 13 |
from ctypes import _pointer_type_cache, _pointer_type_cache_fallback |
| 14 |
from test.support import import_helper |
| 15 |
from weakref import WeakSet |
| 16 |
_ctypes_test = import_helper.import_module("_ctypes_test") |
| 17 |
from ._support import (_CData, PyCPointerType, Py_TPFLAGS_DISALLOW_INSTANTIATION, |
| 18 |
Py_TPFLAGS_IMMUTABLETYPE) |
| 19 |
|
| 20 |
|
| 21 |
ctype_types = [c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, |
| 22 |
c_long, c_ulong, c_longlong, c_ulonglong, c_double, c_float] |
| 23 |
python_types = [int, int, int, int, int, int, |
| 24 |
int, int, int, int, float, float] |
| 25 |
|
| 26 |
|
| 27 |
class PointersTestCase(unittest.TestCase): |
| 28 |
def tearDown(self): |
| 29 |
_pointer_type_cache_fallback.clear() |
| 30 |
|
| 31 |
def test_inheritance_hierarchy(self): |
| 32 |
self.assertEqual(_Pointer.mro(), [_Pointer, _CData, object]) |
| 33 |
|
| 34 |
self.assertEqual(PyCPointerType.__name__, "PyCPointerType") |
| 35 |
self.assertEqual(type(PyCPointerType), type) |
| 36 |
|
| 37 |
def test_type_flags(self): |
| 38 |
for cls in _Pointer, PyCPointerType: |
| 39 |
with self.subTest(cls=cls): |
| 40 |
self.assertTrue(_Pointer.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) |
| 41 |
self.assertFalse(_Pointer.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) |
| 42 |
|
| 43 |
def test_metaclass_details(self): |
| 44 |
# Cannot call the metaclass __init__ more than once |
| 45 |
with self.assertRaisesRegex(SystemError, "already initialized"): |
| 46 |
PyCPointerType.__init__(POINTER(c_byte), 'ptr', (), {}) |
| 47 |
|
| 48 |
def test_pointer_crash(self): |
| 49 |
|
| 50 |
class A(POINTER(c_ulong)): |
| 51 |
pass |
| 52 |
|
| 53 |
POINTER(c_ulong)(c_ulong(22)) |
| 54 |
# Pointer can't set contents: has no _type_ |
| 55 |
self.assertRaises(TypeError, A, c_ulong(33)) |
| 56 |
|
| 57 |
def test_pass_pointers(self): |
| 58 |
dll = CDLL(_ctypes_test.__file__) |
| 59 |
func = dll._testfunc_p_p |
| 60 |
if sizeof(c_longlong) == sizeof(c_void_p): |
| 61 |
func.restype = c_longlong |
| 62 |
else: |
| 63 |
func.restype = c_long |
| 64 |
|
| 65 |
i = c_int(12345678) |
| 66 |
address = func(byref(i)) |
| 67 |
self.assertEqual(c_int.from_address(address).value, 12345678) |
| 68 |
|
| 69 |
func.restype = POINTER(c_int) |
| 70 |
res = func(pointer(i)) |
| 71 |
self.assertEqual(res.contents.value, 12345678) |
| 72 |
self.assertEqual(res[0], 12345678) |
| 73 |
|
| 74 |
def test_change_pointers(self): |
| 75 |
dll = CDLL(_ctypes_test.__file__) |
| 76 |
func = dll._testfunc_p_p |
| 77 |
|
| 78 |
i = c_int(87654) |
| 79 |
func.restype = POINTER(c_int) |
| 80 |
func.argtypes = (POINTER(c_int),) |
| 81 |
|
| 82 |
res = func(pointer(i)) |
| 83 |
self.assertEqual(res[0], 87654) |
| 84 |
self.assertEqual(res.contents.value, 87654) |
| 85 |
|
| 86 |
# C code: *res = 54345 |
| 87 |
res[0] = 54345 |
| 88 |
self.assertEqual(i.value, 54345) |
| 89 |
|
| 90 |
# C code: |
| 91 |
# int x = 12321; |
| 92 |
# res = &x |
| 93 |
x = c_int(12321) |
| 94 |
res.contents = x |
| 95 |
self.assertEqual(i.value, 54345) |
| 96 |
|
| 97 |
x.value = -99 |
| 98 |
self.assertEqual(res.contents.value, -99) |
| 99 |
|
| 100 |
def test_callbacks_with_pointers(self): |
| 101 |
# a function type receiving a pointer |
| 102 |
PROTOTYPE = CFUNCTYPE(c_int, POINTER(c_int)) |
| 103 |
|
| 104 |
self.result = [] |
| 105 |
|
| 106 |
def func(arg): |
| 107 |
for i in range(10): |
| 108 |
self.result.append(arg[i]) |
| 109 |
return 0 |
| 110 |
callback = PROTOTYPE(func) |
| 111 |
|
| 112 |
dll = CDLL(_ctypes_test.__file__) |
| 113 |
# This function expects a function pointer, |
| 114 |
# and calls this with an integer pointer as parameter. |
| 115 |
# The int pointer points to a table containing the numbers 1..10 |
| 116 |
doit = dll._testfunc_callback_with_pointer |
| 117 |
|
| 118 |
doit(callback) |
| 119 |
doit(callback) |
| 120 |
|
| 121 |
def test_basics(self): |
| 122 |
for ct, pt in zip(ctype_types, python_types): |
| 123 |
i = ct(42) |
| 124 |
p = pointer(i) |
| 125 |
self.assertIs(type(p.contents), ct) |
| 126 |
# p.contents is the same as p[0] |
| 127 |
|
| 128 |
with self.assertRaises(TypeError): |
| 129 |
del p[0] |
| 130 |
|
| 131 |
def test_from_address(self): |
| 132 |
a = array.array('i', [100, 200, 300, 400, 500]) |
| 133 |
addr = a.buffer_info()[0] |
| 134 |
p = POINTER(POINTER(c_int)) |
| 135 |
|
| 136 |
def test_pointer_from_pointer(self): |
| 137 |
p1 = POINTER(c_int) |
| 138 |
p2 = POINTER(p1) |
| 139 |
|
| 140 |
self.assertIsNot(p1, p2) |
| 141 |
self.assertIs(p1.__pointer_type__, p2) |
| 142 |
self.assertIs(p2._type_, p1) |
| 143 |
|
| 144 |
def test_other(self): |
| 145 |
class Table(Structure): |
| 146 |
_fields_ = [("a", c_int), |
| 147 |
("b", c_int), |
| 148 |
("c", c_int)] |
| 149 |
|
| 150 |
pt = pointer(Table(1, 2, 3)) |
| 151 |
|
| 152 |
self.assertEqual(pt.contents.a, 1) |
| 153 |
self.assertEqual(pt.contents.b, 2) |
| 154 |
self.assertEqual(pt.contents.c, 3) |
| 155 |
|
| 156 |
pt.contents.c = 33 |
| 157 |
|
| 158 |
def test_basic(self): |
| 159 |
p = pointer(c_int(42)) |
| 160 |
# Although a pointer can be indexed, it has no length |
| 161 |
self.assertRaises(TypeError, len, p) |
| 162 |
self.assertEqual(p[0], 42) |
| 163 |
self.assertEqual(p[0:1], [42]) |
| 164 |
self.assertEqual(p.contents.value, 42) |
| 165 |
|
| 166 |
def test_charpp(self): |
| 167 |
"""Test that a character pointer-to-pointer is correctly passed""" |
| 168 |
dll = CDLL(_ctypes_test.__file__) |
| 169 |
func = dll._testfunc_c_p_p |
| 170 |
func.restype = c_char_p |
| 171 |
argv = (c_char_p * 2)() |
| 172 |
argc = c_int( 2 ) |
| 173 |
argv[0] = b'hello' |
| 174 |
argv[1] = b'world' |
| 175 |
result = func( byref(argc), argv ) |
| 176 |
self.assertEqual(result, b'world') |
| 177 |
|
| 178 |
def test_bug_1467852(self): |
| 179 |
# http://sourceforge.net/tracker/?func=detail&atid=532154&aid=1467852&group_id=71702 |
| 180 |
x = c_int(5) |
| 181 |
dummy = [] |
| 182 |
for i in range(32000): |
| 183 |
dummy.append(c_int(i)) |
| 184 |
y = c_int(6) |
| 185 |
p = pointer(x) |
| 186 |
pp = pointer(p) |
| 187 |
q = pointer(y) |
| 188 |
pp[0] = q # <== |
| 189 |
self.assertEqual(p[0], 6) |
| 190 |
|
| 191 |
def test_c_void_p(self): |
| 192 |
# http://sourceforge.net/tracker/?func=detail&aid=1518190&group_id=5470&atid=105470 |
| 193 |
if sizeof(c_void_p) == 4: |
| 194 |
self.assertEqual(c_void_p(0xFFFFFFFF).value, |
| 195 |
c_void_p(-1).value) |
| 196 |
self.assertEqual(c_void_p(0xFFFFFFFFFFFFFFFF).value, |
| 197 |
c_void_p(-1).value) |
| 198 |
elif sizeof(c_void_p) == 8: |
| 199 |
self.assertEqual(c_void_p(0xFFFFFFFF).value, |
| 200 |
0xFFFFFFFF) |
| 201 |
self.assertEqual(c_void_p(0xFFFFFFFFFFFFFFFF).value, |
| 202 |
c_void_p(-1).value) |
| 203 |
self.assertEqual(c_void_p(0xFFFFFFFFFFFFFFFFFFFFFFFF).value, |
| 204 |
c_void_p(-1).value) |
| 205 |
|
| 206 |
self.assertRaises(TypeError, c_void_p, 3.14) # make sure floats are NOT accepted |
| 207 |
self.assertRaises(TypeError, c_void_p, object()) # nor other objects |
| 208 |
|
| 209 |
def test_read_null_pointer(self): |
| 210 |
null_ptr = POINTER(c_int)() |
| 211 |
with self.assertRaisesRegex(ValueError, "NULL pointer access"): |
| 212 |
null_ptr[0] |
| 213 |
|
| 214 |
def test_write_null_pointer(self): |
| 215 |
null_ptr = POINTER(c_int)() |
| 216 |
with self.assertRaisesRegex(ValueError, "NULL pointer access"): |
| 217 |
null_ptr[0] = 1 |
| 218 |
|
| 219 |
def test_set_pointer_to_null_and_read(self): |
| 220 |
class Bar(Structure): |
| 221 |
_fields_ = [("values", POINTER(c_int))] |
| 222 |
|
| 223 |
bar = Bar() |
| 224 |
bar.values = (c_int * 3)(1, 2, 3) |
| 225 |
|
| 226 |
values = [bar.values[0], bar.values[1], bar.values[2]] |
| 227 |
self.assertEqual(values, [1, 2, 3]) |
| 228 |
|
| 229 |
bar.values = None |
| 230 |
with self.assertRaisesRegex(ValueError, "NULL pointer access"): |
| 231 |
bar.values[0] |
| 232 |
|
| 233 |
def test_pointers_bool(self): |
| 234 |
# NULL pointers have a boolean False value, non-NULL pointers True. |
| 235 |
self.assertEqual(bool(POINTER(c_int)()), False) |
| 236 |
self.assertEqual(bool(pointer(c_int())), True) |
| 237 |
|
| 238 |
self.assertEqual(bool(CFUNCTYPE(None)(0)), False) |
| 239 |
self.assertEqual(bool(CFUNCTYPE(None)(42)), True) |
| 240 |
|
| 241 |
# COM methods are boolean True: |
| 242 |
if sys.platform == "win32": |
| 243 |
mth = ctypes.WINFUNCTYPE(None)(42, "name", (), None) |
| 244 |
self.assertEqual(bool(mth), True) |
| 245 |
|
| 246 |
def test_pointer_type_name(self): |
| 247 |
LargeNamedType = type('T' * 2 ** 25, (Structure,), {}) |
| 248 |
self.assertTrue(POINTER(LargeNamedType)) |
| 249 |
|
| 250 |
def test_pointer_type_str_name(self): |
| 251 |
large_string = 'T' * 2 ** 25 |
| 252 |
with self.assertWarns(DeprecationWarning): |
| 253 |
P = POINTER(large_string) |
| 254 |
self.assertTrue(P) |
| 255 |
|
| 256 |
def test_abstract(self): |
| 257 |
self.assertRaises(TypeError, _Pointer.set_type, 42) |
| 258 |
|
| 259 |
def test_pointer_types_equal(self): |
| 260 |
t1 = POINTER(c_int) |
| 261 |
t2 = POINTER(c_int) |
| 262 |
|
| 263 |
self.assertIs(t1, t2) |
| 264 |
|
| 265 |
p1 = t1(c_int(1)) |
| 266 |
p2 = pointer(c_int(1)) |
| 267 |
|
| 268 |
self.assertIsInstance(p1, t1) |
| 269 |
self.assertIsInstance(p2, t1) |
| 270 |
|
| 271 |
self.assertIs(type(p1), t1) |
| 272 |
self.assertIs(type(p2), t1) |
| 273 |
|
| 274 |
def test_incomplete_pointer_types_still_equal(self): |
| 275 |
with self.assertWarns(DeprecationWarning): |
| 276 |
t1 = POINTER("LP_C") |
| 277 |
with self.assertWarns(DeprecationWarning): |
| 278 |
t2 = POINTER("LP_C") |
| 279 |
|
| 280 |
self.assertIs(t1, t2) |
| 281 |
|
| 282 |
def test_incomplete_pointer_types_cannot_instantiate(self): |
| 283 |
with self.assertWarns(DeprecationWarning): |
| 284 |
t1 = POINTER("LP_C") |
| 285 |
with self.assertRaisesRegex(TypeError, "has no _type_"): |
| 286 |
t1() |
| 287 |
|
| 288 |
def test_pointer_set_type_twice(self): |
| 289 |
t1 = POINTER(c_int) |
| 290 |
self.assertIs(c_int.__pointer_type__, t1) |
| 291 |
self.assertIs(t1._type_, c_int) |
| 292 |
|
| 293 |
t1.set_type(c_int) |
| 294 |
self.assertIs(c_int.__pointer_type__, t1) |
| 295 |
self.assertIs(t1._type_, c_int) |
| 296 |
|
| 297 |
def test_pointer_set_wrong_type(self): |
| 298 |
int_ptr = POINTER(c_int) |
| 299 |
float_ptr = POINTER(c_float) |
| 300 |
try: |
| 301 |
class C(c_int): |
| 302 |
pass |
| 303 |
|
| 304 |
t1 = POINTER(c_int) |
| 305 |
t2 = POINTER(c_float) |
| 306 |
t1.set_type(c_float) |
| 307 |
self.assertEqual(t1(c_float(1.5))[0], 1.5) |
| 308 |
self.assertIs(t1._type_, c_float) |
| 309 |
self.assertIs(c_int.__pointer_type__, t1) |
| 310 |
self.assertIs(c_float.__pointer_type__, float_ptr) |
| 311 |
|
| 312 |
t1.set_type(C) |
| 313 |
self.assertEqual(t1(C(123))[0].value, 123) |
| 314 |
self.assertIs(c_int.__pointer_type__, t1) |
| 315 |
self.assertIs(c_float.__pointer_type__, float_ptr) |
| 316 |
finally: |
| 317 |
POINTER(c_int).set_type(c_int) |
| 318 |
self.assertIs(POINTER(c_int), int_ptr) |
| 319 |
self.assertIs(POINTER(c_int)._type_, c_int) |
| 320 |
self.assertIs(c_int.__pointer_type__, int_ptr) |
| 321 |
|
| 322 |
def test_pointer_not_ctypes_type(self): |
| 323 |
with self.assertRaisesRegex(TypeError, "must have storage info"): |
| 324 |
POINTER(int) |
| 325 |
|
| 326 |
with self.assertRaisesRegex(TypeError, "must have storage info"): |
| 327 |
pointer(int) |
| 328 |
|
| 329 |
with self.assertRaisesRegex(TypeError, "must have storage info"): |
| 330 |
pointer(int(1)) |
| 331 |
|
| 332 |
def test_pointer_set_python_type(self): |
| 333 |
p1 = POINTER(c_int) |
| 334 |
with self.assertRaisesRegex(TypeError, "must have storage info"): |
| 335 |
p1.set_type(int) |
| 336 |
|
| 337 |
def test_pointer_type_attribute_is_none(self): |
| 338 |
class Cls(Structure): |
| 339 |
_fields_ = ( |
| 340 |
('a', c_int), |
| 341 |
('b', c_float), |
| 342 |
) |
| 343 |
|
| 344 |
with self.assertRaisesRegex(AttributeError, ".Cls'> has no attribute '__pointer_type__'"): |
| 345 |
Cls.__pointer_type__ |
| 346 |
|
| 347 |
p = POINTER(Cls) |
| 348 |
self.assertIs(Cls.__pointer_type__, p) |
| 349 |
|
| 350 |
def test_arbitrary_pointer_type_attribute(self): |
| 351 |
class Cls(Structure): |
| 352 |
_fields_ = ( |
| 353 |
('a', c_int), |
| 354 |
('b', c_float), |
| 355 |
) |
| 356 |
|
| 357 |
garbage = 'garbage' |
| 358 |
|
| 359 |
P = POINTER(Cls) |
| 360 |
self.assertIs(Cls.__pointer_type__, P) |
| 361 |
Cls.__pointer_type__ = garbage |
| 362 |
self.assertIs(Cls.__pointer_type__, garbage) |
| 363 |
self.assertIs(POINTER(Cls), garbage) |
| 364 |
self.assertIs(P._type_, Cls) |
| 365 |
|
| 366 |
instance = Cls(1, 2.0) |
| 367 |
pointer = P(instance) |
| 368 |
self.assertEqual(pointer[0].a, 1) |
| 369 |
self.assertEqual(pointer[0].b, 2) |
| 370 |
|
| 371 |
del Cls.__pointer_type__ |
| 372 |
|
| 373 |
NewP = POINTER(Cls) |
| 374 |
self.assertIsNot(NewP, P) |
| 375 |
self.assertIs(Cls.__pointer_type__, NewP) |
| 376 |
self.assertIs(P._type_, Cls) |
| 377 |
|
| 378 |
def test_pointer_types_factory(self): |
| 379 |
"""Shouldn't leak""" |
| 380 |
def factory(): |
| 381 |
class Cls(Structure): |
| 382 |
_fields_ = ( |
| 383 |
('a', c_int), |
| 384 |
('b', c_float), |
| 385 |
) |
| 386 |
|
| 387 |
return Cls |
| 388 |
|
| 389 |
ws_typ = WeakSet() |
| 390 |
ws_ptr = WeakSet() |
| 391 |
for _ in range(10): |
| 392 |
typ = factory() |
| 393 |
ptr = POINTER(typ) |
| 394 |
|
| 395 |
ws_typ.add(typ) |
| 396 |
ws_ptr.add(ptr) |
| 397 |
|
| 398 |
typ = None |
| 399 |
ptr = None |
| 400 |
|
| 401 |
gc.collect() |
| 402 |
|
| 403 |
self.assertEqual(len(ws_typ), 0, ws_typ) |
| 404 |
self.assertEqual(len(ws_ptr), 0, ws_ptr) |
| 405 |
|
| 406 |
def test_pointer_proto_missing_argtypes_error(self): |
| 407 |
class BadType(ctypes._Pointer): |
| 408 |
# _type_ is intentionally missing |
| 409 |
pass |
| 410 |
|
| 411 |
func = ctypes.pythonapi.Py_GetVersion |
| 412 |
func.argtypes = (BadType,) |
| 413 |
|
| 414 |
with self.assertRaises(ctypes.ArgumentError): |
| 415 |
func(object()) |
| 416 |
|
| 417 |
class PointerTypeCacheTestCase(unittest.TestCase): |
| 418 |
# dummy tests to check warnings and base behavior |
| 419 |
def tearDown(self): |
| 420 |
_pointer_type_cache_fallback.clear() |
| 421 |
|
| 422 |
def test_deprecated_cache_with_not_ctypes_type(self): |
| 423 |
class C: |
| 424 |
pass |
| 425 |
|
| 426 |
with self.assertWarns(DeprecationWarning): |
| 427 |
P = POINTER("C") |
| 428 |
|
| 429 |
with self.assertWarns(DeprecationWarning): |
| 430 |
self.assertIs(_pointer_type_cache["C"], P) |
| 431 |
|
| 432 |
with self.assertWarns(DeprecationWarning): |
| 433 |
_pointer_type_cache[C] = P |
| 434 |
self.assertIs(C.__pointer_type__, P) |
| 435 |
with self.assertWarns(DeprecationWarning): |
| 436 |
self.assertIs(_pointer_type_cache[C], P) |
| 437 |
|
| 438 |
def test_deprecated_cache_with_ints(self): |
| 439 |
with self.assertWarns(DeprecationWarning): |
| 440 |
_pointer_type_cache[123] = 456 |
| 441 |
|
| 442 |
with self.assertWarns(DeprecationWarning): |
| 443 |
self.assertEqual(_pointer_type_cache[123], 456) |
| 444 |
|
| 445 |
def test_deprecated_cache_with_ctypes_type(self): |
| 446 |
class C(Structure): |
| 447 |
_fields_ = [("a", c_int), |
| 448 |
("b", c_int), |
| 449 |
("c", c_int)] |
| 450 |
|
| 451 |
P1 = POINTER(C) |
| 452 |
with self.assertWarns(DeprecationWarning): |
| 453 |
P2 = POINTER("C") |
| 454 |
|
| 455 |
with self.assertWarns(DeprecationWarning): |
| 456 |
_pointer_type_cache[C] = P2 |
| 457 |
|
| 458 |
self.assertIs(C.__pointer_type__, P2) |
| 459 |
self.assertIsNot(C.__pointer_type__, P1) |
| 460 |
|
| 461 |
with self.assertWarns(DeprecationWarning): |
| 462 |
self.assertIs(_pointer_type_cache[C], P2) |
| 463 |
|
| 464 |
with self.assertWarns(DeprecationWarning): |
| 465 |
self.assertIs(_pointer_type_cache.get(C), P2) |
| 466 |
|
| 467 |
def test_get_not_registered(self): |
| 468 |
with self.assertWarns(DeprecationWarning): |
| 469 |
self.assertIsNone(_pointer_type_cache.get(str)) |
| 470 |
|
| 471 |
with self.assertWarns(DeprecationWarning): |
| 472 |
self.assertIsNone(_pointer_type_cache.get(str, None)) |
| 473 |
|
| 474 |
def test_repeated_set_type(self): |
| 475 |
# Regression test for gh-133290 |
| 476 |
class C(Structure): |
| 477 |
_fields_ = [('a', c_int)] |
| 478 |
ptr = POINTER(C) |
| 479 |
# Read _type_ several times to warm up cache |
| 480 |
for i in range(5): |
| 481 |
self.assertIs(ptr._type_, C) |
| 482 |
ptr.set_type(c_int) |
| 483 |
self.assertIs(ptr._type_, c_int) |
| 484 |
|
| 485 |
|
| 486 |
if __name__ == '__main__': |
| 487 |
unittest.main() |