| 1 |
import pytest |
| 2 |
|
| 3 |
from ctypes import * |
| 4 |
|
| 5 |
def test_cast_functype(dll): |
| 6 |
|
| 7 |
my_sqrt = dll.my_sqrt |
| 8 |
saved_objects = my_sqrt._objects.copy() |
| 9 |
sqrt = cast(cast(my_sqrt, c_void_p), CFUNCTYPE(c_double, c_double)) |
| 10 |
assert sqrt(4.0) == 2.0 |
| 11 |
assert not cast(0, CFUNCTYPE(c_int)) |
| 12 |
|
| 13 |
assert sqrt._objects is my_sqrt._objects |
| 14 |
my_sqrt._objects.clear() |
| 15 |
my_sqrt._objects.update(saved_objects) |
| 16 |
|
| 17 |
def test_cast_argumenterror(): |
| 18 |
param = c_uint(42) |
| 19 |
with pytest.raises(ArgumentError): |
| 20 |
cast(param, c_void_p) |
| 21 |
|
| 22 |
def test_c_bool(): |
| 23 |
x = c_bool(42) |
| 24 |
assert x.value is True |
| 25 |
x = c_bool(0.0) |
| 26 |
assert x.value is False |
| 27 |
x = c_bool("") |
| 28 |
assert x.value is False |
| 29 |
x = c_bool(['yadda']) |
| 30 |
assert x.value is True |
| 31 |
|
| 32 |
def test_cast_array(): |
| 33 |
import sys |
| 34 |
data = b'data' |
| 35 |
ubyte = c_ubyte * len(data) |
| 36 |
byteslike = ubyte.from_buffer_copy(data) |
| 37 |
m = memoryview(byteslike) |
| 38 |
if sys.version_info > (3, 3): |
| 39 |
b = m.cast('B') |
| 40 |
assert bytes(b) == data |
| 41 |
|
| 42 |
def test_cast_keepalive_bug(): |
| 43 |
p = cast((c_int * 10)(*range(10)), POINTER(c_int)) |
| 44 |
import gc |
| 45 |
gc.collect() |
| 46 |
assert [p[i] for i in range(10)] == list(range(10)) |