| 1 | import pytest |
| 2 | from ctypes import * |
| 3 | |
| 4 | |
| 5 | def test_restype_setattr(dll): |
| 6 | func = dll._testfunc_p_p |
| 7 | with pytest.raises(TypeError): |
| 8 | setattr(func, 'restype', 20) |
| 9 | |
| 10 | def test_argtypes_setattr(dll): |
| 11 | func = dll._testfunc_p_p |
| 12 | with pytest.raises(TypeError): |
| 13 | setattr(func, 'argtypes', 20) |
| 14 | with pytest.raises(TypeError): |
| 15 | setattr(func, 'argtypes', [20]) |
| 16 | |
| 17 | func = CFUNCTYPE(c_long, c_void_p, c_long)(lambda: None) |
| 18 | assert func.argtypes == (c_void_p, c_long) |
| 19 | |
| 20 | def test_paramflags_setattr(): |
| 21 | func = CFUNCTYPE(c_long, c_void_p, c_long)(lambda: None) |
| 22 | with pytest.raises(TypeError): |
| 23 | setattr(func, 'paramflags', 'spam') |
| 24 | with pytest.raises(ValueError): |
| 25 | setattr(func, 'paramflags', (1, 2, 3, 4)) |
| 26 | with pytest.raises(TypeError): |
| 27 | setattr(func, 'paramflags', ((1,), ('a',))) |
| 28 | func.paramflags = (1,), (1|4,) |
| 29 | |
| 30 | def test_kwargs(dll): |
| 31 | proto = CFUNCTYPE(c_char_p, c_char_p, c_int) |
| 32 | paramflags = (1, 'text', b"tavino"), (1, 'letter', ord('v')) |
| 33 | func = proto(('my_strchr', dll), paramflags) |
| 34 | assert func.argtypes == (c_char_p, c_int) |
| 35 | assert func.restype == c_char_p |
| 36 | |
| 37 | result = func(b"abcd", ord('b')) |
| 38 | assert result == b"bcd" |
| 39 | |
| 40 | result = func() |
| 41 | assert result == b"vino" |
| 42 | |
| 43 | result = func(b"grapevine") |
| 44 | assert result == b"vine" |
| 45 | |
| 46 | result = func(text=b"grapevine") |
| 47 | assert result == b"vine" |
| 48 | |
| 49 | result = func(letter=ord('i')) |
| 50 | assert result == b"ino" |
| 51 | |
| 52 | result = func(letter=ord('p'), text=b"impossible") |
| 53 | assert result == b"possible" |
| 54 | |
| 55 | result = func(text=b"impossible", letter=ord('p')) |
| 56 | assert result == b"possible" |
| 57 | |
| 58 | def test_array_to_ptr_wrongtype(dll): |
| 59 | ARRAY = c_byte * 8 |
| 60 | func = dll._testfunc_ai8 |
| 61 | func.restype = POINTER(c_int) |
| 62 | func.argtypes = [c_int * 8] |
| 63 | array = ARRAY(1, 2, 3, 4, 5, 6, 7, 8) |
| 64 | with pytest.raises(ArgumentError): |
| 65 | func(array) |