diff options
Diffstat (limited to 'testing/cffi1/test_new_ffi_1.py')
-rw-r--r-- | testing/cffi1/test_new_ffi_1.py | 131 |
1 files changed, 65 insertions, 66 deletions
diff --git a/testing/cffi1/test_new_ffi_1.py b/testing/cffi1/test_new_ffi_1.py index 640830b..c874a36 100644 --- a/testing/cffi1/test_new_ffi_1.py +++ b/testing/cffi1/test_new_ffi_1.py @@ -1,4 +1,3 @@ -import py import pytest import platform, imp import sys, os, ctypes @@ -153,17 +152,17 @@ class TestNewFFI1: assert int(q) == int(p) assert hash(q) == hash(p) c_decl_ptr = '%s *' % c_decl - py.test.raises(OverflowError, ffi.new, c_decl_ptr, min - 1) - py.test.raises(OverflowError, ffi.new, c_decl_ptr, max + 1) - py.test.raises(OverflowError, ffi.new, c_decl_ptr, long(min - 1)) - py.test.raises(OverflowError, ffi.new, c_decl_ptr, long(max + 1)) + pytest.raises(OverflowError, ffi.new, c_decl_ptr, min - 1) + pytest.raises(OverflowError, ffi.new, c_decl_ptr, max + 1) + pytest.raises(OverflowError, ffi.new, c_decl_ptr, long(min - 1)) + pytest.raises(OverflowError, ffi.new, c_decl_ptr, long(max + 1)) assert ffi.new(c_decl_ptr, min)[0] == min assert ffi.new(c_decl_ptr, max)[0] == max assert ffi.new(c_decl_ptr, long(min))[0] == min assert ffi.new(c_decl_ptr, long(max))[0] == max def test_new_unsupported_type(self): - e = py.test.raises(TypeError, ffi.new, "int") + e = pytest.raises(TypeError, ffi.new, "int") assert str(e.value) == "expected a pointer or array ctype, got 'int'" def test_new_single_integer(self): @@ -220,7 +219,7 @@ class TestNewFFI1: with pytest.raises(IndexError): p[10] # - py.test.raises(TypeError, ffi.new, "int[]") + pytest.raises(TypeError, ffi.new, "int[]") # p = ffi.new("int[]", [-6, -7]) # a list is all the items, like C assert p[0] == -6 @@ -232,7 +231,7 @@ class TestNewFFI1: p = ffi.new("int[]", 0) with pytest.raises(IndexError): p[0] - py.test.raises(ValueError, ffi.new, "int[]", -1) + pytest.raises(ValueError, ffi.new, "int[]", -1) assert repr(p) == "<cdata 'int[]' owning 0 bytes>" def test_pointer_init(self): @@ -245,7 +244,7 @@ class TestNewFFI1: def test_cannot_cast(self): a = ffi.new("short int[10]") - e = py.test.raises(TypeError, ffi.new, "long int **", a) + e = pytest.raises(TypeError, ffi.new, "long int **", a) msg = str(e.value) assert "'short[10]'" in msg and "'long *'" in msg @@ -362,9 +361,9 @@ class TestNewFFI1: assert not bool(ffi.cast("char", 0)) assert bool(ffi.cast("char", 1)) assert bool(ffi.cast("char", 255)) - py.test.raises(TypeError, ffi.new, "char*", 32) - py.test.raises(TypeError, ffi.new, "char*", u+"x") - py.test.raises(TypeError, ffi.new, "char*", b"foo") + pytest.raises(TypeError, ffi.new, "char*", 32) + pytest.raises(TypeError, ffi.new, "char*", u+"x") + pytest.raises(TypeError, ffi.new, "char*", b"foo") # p = ffi.new("char[]", [b'a', b'b', b'\x9c']) assert len(p) == 3 @@ -383,13 +382,13 @@ class TestNewFFI1: p = ffi.new("char[2]", b"ab") assert len(p) == 2 assert [p[i] for i in range(2)] == [b'a', b'b'] - py.test.raises(IndexError, ffi.new, "char[2]", b"abc") + pytest.raises(IndexError, ffi.new, "char[2]", b"abc") def check_wchar_t(self, ffi): try: ffi.cast("wchar_t", 0) except NotImplementedError: - py.test.skip("NotImplementedError: wchar_t") + pytest.skip("NotImplementedError: wchar_t") def test_wchar_t(self): self.check_wchar_t(ffi) @@ -398,7 +397,7 @@ class TestNewFFI1: if SIZE_OF_WCHAR > 2: assert ffi.new("wchar_t*", u+'\U00012345')[0] == u+'\U00012345' else: - py.test.raises(TypeError, ffi.new, "wchar_t*", u+'\U00012345') + pytest.raises(TypeError, ffi.new, "wchar_t*", u+'\U00012345') assert ffi.new("wchar_t*")[0] == u+'\x00' assert int(ffi.cast("wchar_t", 300)) == 300 assert not bool(ffi.cast("wchar_t", 0)) @@ -406,8 +405,8 @@ class TestNewFFI1: assert bool(ffi.cast("wchar_t", 65535)) if SIZE_OF_WCHAR > 2: assert bool(ffi.cast("wchar_t", 65536)) - py.test.raises(TypeError, ffi.new, "wchar_t*", 32) - py.test.raises(TypeError, ffi.new, "wchar_t*", "foo") + pytest.raises(TypeError, ffi.new, "wchar_t*", 32) + pytest.raises(TypeError, ffi.new, "wchar_t*", "foo") # p = ffi.new("wchar_t[]", [u+'a', u+'b', u+'\u1234']) assert len(p) == 3 @@ -442,7 +441,7 @@ class TestNewFFI1: p = ffi.new("wchar_t[2]", u+"ab") assert len(p) == 2 assert [p[i] for i in range(2)] == [u+'a', u+'b'] - py.test.raises(IndexError, ffi.new, "wchar_t[2]", u+"abc") + pytest.raises(IndexError, ffi.new, "wchar_t[2]", u+"abc") def test_none_as_null_doesnt_work(self): p = ffi.new("int*[1]") @@ -468,8 +467,8 @@ class TestNewFFI1: # p = ffi.new("float*", 15.75) assert p[0] == 15.75 - py.test.raises(TypeError, int, p) - py.test.raises(TypeError, float, p) + pytest.raises(TypeError, int, p) + pytest.raises(TypeError, float, p) p[0] = 0.0 assert bool(p) is True # @@ -500,14 +499,14 @@ class TestNewFFI1: assert repr(s) == "<cdata 'struct simple *' owning %d bytes>" % ( SIZE_OF_INT + 2 * SIZE_OF_SHORT) # - py.test.raises(ValueError, ffi.new, "struct simple*", [1, 2, 3, 4]) + pytest.raises(ValueError, ffi.new, "struct simple*", [1, 2, 3, 4]) def test_constructor_struct_from_dict(self): s = ffi.new("struct simple*", {'b': 123, 'c': 456}) assert s.a == 0 assert s.b == 123 assert s.c == 456 - py.test.raises(KeyError, ffi.new, "struct simple*", {'d': 456}) + pytest.raises(KeyError, ffi.new, "struct simple*", {'d': 456}) def test_struct_pointer(self): s = ffi.new("struct simple*") @@ -520,10 +519,10 @@ class TestNewFFI1: s[1] def test_struct_opaque(self): - py.test.raises(ffi.error, ffi.new, "struct baz*") + pytest.raises(ffi.error, ffi.new, "struct baz*") # should 'ffi.new("struct baz **") work? it used to, but it was # not particularly useful... - py.test.raises(ffi.error, ffi.new, "struct baz**") + pytest.raises(ffi.error, ffi.new, "struct baz**") def test_pointer_to_struct(self): s = ffi.new("struct simple *") @@ -580,18 +579,18 @@ class TestNewFFI1: SIZE_OF_INT,) def test_union_opaque(self): - py.test.raises(ffi.error, ffi.new, "union baz*") + pytest.raises(ffi.error, ffi.new, "union baz*") # should 'ffi.new("union baz **") work? it used to, but it was # not particularly useful... - py.test.raises(ffi.error, ffi.new, "union baz**") + pytest.raises(ffi.error, ffi.new, "union baz**") def test_union_initializer(self): - py.test.raises(TypeError, ffi.new, "union init_u*", b'A') - py.test.raises(TypeError, ffi.new, "union init_u*", 5) - py.test.raises(ValueError, ffi.new, "union init_u*", [b'A', 5]) + pytest.raises(TypeError, ffi.new, "union init_u*", b'A') + pytest.raises(TypeError, ffi.new, "union init_u*", 5) + pytest.raises(ValueError, ffi.new, "union init_u*", [b'A', 5]) u = ffi.new("union init_u*", [b'A']) assert u.a == b'A' - py.test.raises(TypeError, ffi.new, "union init_u*", [1005]) + pytest.raises(TypeError, ffi.new, "union init_u*", [1005]) u = ffi.new("union init_u*", {'b': 12345}) assert u.b == 12345 u = ffi.new("union init_u*", []) @@ -623,7 +622,7 @@ class TestNewFFI1: assert str(x) == repr(x) assert ffi.string(x) == b"x" assert ffi.string(ffi.new("char*", b"\x00")) == b"" - py.test.raises(TypeError, ffi.new, "char*", unicode("foo")) + pytest.raises(TypeError, ffi.new, "char*", unicode("foo")) def test_unicode_from_wchar_pointer(self): self.check_wchar_t(ffi) @@ -698,7 +697,7 @@ class TestNewFFI1: assert s.name == ffi.NULL def test_voidp(self): - py.test.raises(TypeError, ffi.new, "void*") + pytest.raises(TypeError, ffi.new, "void*") p = ffi.new("void **") assert p[0] == ffi.NULL a = ffi.new("int[]", [10, 11, 12]) @@ -706,7 +705,7 @@ class TestNewFFI1: vp = p[0] with pytest.raises(TypeError): vp[0] - py.test.raises(TypeError, ffi.new, "short **", a) + pytest.raises(TypeError, ffi.new, "short **", a) # s = ffi.new("struct voidp *") s.p = a # works @@ -720,7 +719,7 @@ class TestNewFFI1: s.r = b # fails def test_functionptr_simple(self): - py.test.raises(TypeError, ffi.callback, "int(*)(int)", 0) + pytest.raises(TypeError, ffi.callback, "int(*)(int)", 0) def cb(n): return n + 1 cb.__qualname__ = 'cb' @@ -992,10 +991,10 @@ class TestNewFFI1: assert list(a) == [b"h", b"e", b"l", b"l", b"o", b"\0"] assert list(iter(a)) == [b"h", b"e", b"l", b"l", b"o", b"\0"] # - py.test.raises(TypeError, iter, ffi.cast("char *", a)) - py.test.raises(TypeError, list, ffi.cast("char *", a)) - py.test.raises(TypeError, iter, ffi.new("int *")) - py.test.raises(TypeError, list, ffi.new("int *")) + pytest.raises(TypeError, iter, ffi.cast("char *", a)) + pytest.raises(TypeError, list, ffi.cast("char *", a)) + pytest.raises(TypeError, iter, ffi.new("int *")) + pytest.raises(TypeError, list, ffi.new("int *")) def test_offsetof(self): # struct abc { int a, b, c; }; @@ -1006,7 +1005,7 @@ class TestNewFFI1: def test_offsetof_nested(self): # struct nesting { struct abc d, e; }; assert ffi.offsetof("struct nesting", "e") == 12 - py.test.raises(KeyError, ffi.offsetof, "struct nesting", "e.a") + pytest.raises(KeyError, ffi.offsetof, "struct nesting", "e.a") assert ffi.offsetof("struct nesting", "e", "a") == 12 assert ffi.offsetof("struct nesting", "e", "b") == 16 assert ffi.offsetof("struct nesting", "e", "c") == 20 @@ -1054,7 +1053,7 @@ class TestNewFFI1: # typedef enum { AA1, BB1, CC1 } foo_e_t; # typedef struct { foo_e_t f:2; } bfenum_t; if sys.platform == "win32": - py.test.skip("enums are not unsigned") + pytest.skip("enums are not unsigned") s = ffi.new("bfenum_t *") s.f = 2 assert s.f == 2 @@ -1154,7 +1153,7 @@ class TestNewFFI1: try: b = ffi.buffer(a) except NotImplementedError as e: - py.test.skip(str(e)) + pytest.skip(str(e)) content = b[:] assert len(content) == len(b) == 2 if sys.byteorder == 'little': @@ -1172,7 +1171,7 @@ class TestNewFFI1: try: b = ffi.buffer(a) except NotImplementedError as e: - py.test.skip(str(e)) + pytest.skip(str(e)) content = b[:] if sys.byteorder == 'little': assert content.startswith(b'\x64\x00\x00\x00\x65\x00\x00\x00') @@ -1188,7 +1187,7 @@ class TestNewFFI1: try: b = ffi.buffer(a, 1) except NotImplementedError as e: - py.test.skip(str(e)) + pytest.skip(str(e)) content = b[:] assert len(content) == 1 if sys.byteorder == 'little': @@ -1206,7 +1205,7 @@ class TestNewFFI1: try: ffi.buffer(a1) except NotImplementedError as e: - py.test.skip(str(e)) + pytest.skip(str(e)) assert ffi.buffer(a1)[:] == ffi.buffer(a2, 4*10)[:] def test_ffi_buffer_with_file(self): @@ -1217,7 +1216,7 @@ class TestNewFFI1: try: ffi.buffer(a, 512) except NotImplementedError as e: - py.test.skip(str(e)) + pytest.skip(str(e)) f.write(ffi.buffer(a, 1000 * ffi.sizeof("int"))) f.seek(0) assert f.read() == arraytostring(array.array('i', range(1000))) @@ -1235,7 +1234,7 @@ class TestNewFFI1: try: ffi.buffer(a, 512) except NotImplementedError as e: - py.test.skip(str(e)) + pytest.skip(str(e)) f.write(ffi.buffer(a, 1000 * ffi.sizeof("int"))) f.seek(0) assert f.read() == arraytostring(array.array('i', range(1000))) @@ -1254,7 +1253,7 @@ class TestNewFFI1: def test_struct_containing_array_varsize_workaround(self): if sys.platform == "win32": - py.test.skip("array of length 0 not supported") + pytest.skip("array of length 0 not supported") # struct array0 { int len; short data[0]; }; p = ffi.new("char[]", ffi.sizeof("struct array0") + 7 * SIZE_OF_SHORT) q = ffi.cast("struct array0 *", p) @@ -1266,7 +1265,7 @@ class TestNewFFI1: assert q.data[6] == 15 def test_new_struct_containing_array_varsize(self): - py.test.skip("later?") + pytest.skip("later?") ffi.cdef("struct foo_s { int len; short data[]; };") p = ffi.new("struct foo_s *", 10) # a single integer is the length assert p.len == 0 @@ -1294,14 +1293,14 @@ class TestNewFFI1: def test_array_of_func_ptr(self): f = ffi.cast("int(*)(int)", 42) assert f != ffi.NULL - py.test.raises(ffi.error, ffi.cast, "int(int)", 42) - py.test.raises(ffi.error, ffi.new, "int([5])(int)") + pytest.raises(ffi.error, ffi.cast, "int(int)", 42) + pytest.raises(ffi.error, ffi.new, "int([5])(int)") a = ffi.new("int(*[5])(int)", [f]) assert ffi.getctype(ffi.typeof(a)) == "int(*[5])(int)" assert len(a) == 5 assert a[0] == f assert a[1] == ffi.NULL - py.test.raises(TypeError, ffi.cast, "int(*)(int)[5]", 0) + pytest.raises(TypeError, ffi.cast, "int(*)(int)[5]", 0) # def cb(n): return n + 1 @@ -1322,7 +1321,7 @@ class TestNewFFI1: assert g(f) == b'B' def test_vararg_callback(self): - py.test.skip("callback with '...'") + pytest.skip("callback with '...'") def cb(i, va_list): j = ffi.va_arg(va_list, "int") k = ffi.va_arg(va_list, "long long") @@ -1349,7 +1348,7 @@ class TestNewFFI1: def test_new_ctype(self): p = ffi.new("int *") - py.test.raises(TypeError, ffi.new, p) + pytest.raises(TypeError, ffi.new, p) p = ffi.new(ffi.typeof("int *"), 42) assert p[0] == 42 @@ -1540,17 +1539,17 @@ class TestNewFFI1: assert int(ffi.cast("_Bool", b'\x80')) == 1 assert ffi.new("_Bool *", False)[0] == 0 assert ffi.new("_Bool *", 1)[0] == 1 - py.test.raises(OverflowError, ffi.new, "_Bool *", 2) - py.test.raises(TypeError, ffi.string, ffi.cast("_Bool", 2)) + pytest.raises(OverflowError, ffi.new, "_Bool *", 2) + pytest.raises(TypeError, ffi.string, ffi.cast("_Bool", 2)) def test_addressof(self): p = ffi.new("struct ab *") a = ffi.addressof(p[0]) assert repr(a).startswith("<cdata 'struct ab *' 0x") assert a == p - py.test.raises(TypeError, ffi.addressof, p) - py.test.raises((AttributeError, TypeError), ffi.addressof, 5) - py.test.raises(TypeError, ffi.addressof, ffi.cast("int", 5)) + pytest.raises(TypeError, ffi.addressof, p) + pytest.raises((AttributeError, TypeError), ffi.addressof, 5) + pytest.raises(TypeError, ffi.addressof, ffi.cast("int", 5)) def test_addressof_field(self): p = ffi.new("struct ab *") @@ -1564,7 +1563,7 @@ class TestNewFFI1: def test_addressof_field_nested(self): # struct nesting { struct abc d, e; }; p = ffi.new("struct nesting *") - py.test.raises(KeyError, ffi.addressof, p[0], 'e.b') + pytest.raises(KeyError, ffi.addressof, p[0], 'e.b') a = ffi.addressof(p[0], 'e', 'b') assert int(ffi.cast("uintptr_t", a)) == ( int(ffi.cast("uintptr_t", p)) + @@ -1581,7 +1580,7 @@ class TestNewFFI1: p0 = ffi.addressof(p) assert p0 == p assert ffi.typeof(p0) is ffi.typeof("int(*)[52]") - py.test.raises(TypeError, ffi.addressof, p0) + pytest.raises(TypeError, ffi.addressof, p0) # p1 = ffi.addressof(p, 25) assert ffi.typeof(p1) is ffi.typeof("int *") @@ -1591,14 +1590,14 @@ class TestNewFFI1: def test_addressof_pointer(self): array = ffi.new("int[50]") p = ffi.cast("int *", array) - py.test.raises(TypeError, ffi.addressof, p) + pytest.raises(TypeError, ffi.addressof, p) assert ffi.addressof(p, 0) == p assert ffi.addressof(p, 25) == p + 25 assert ffi.typeof(ffi.addressof(p, 25)) == ffi.typeof(p) # array = ffi.new("struct ab[50]") p = ffi.cast("int *", array) - py.test.raises(TypeError, ffi.addressof, p) + pytest.raises(TypeError, ffi.addressof, p) assert ffi.addressof(p, 0) == p assert ffi.addressof(p, 25) == p + 25 assert ffi.typeof(ffi.addressof(p, 25)) == ffi.typeof(p) @@ -1675,7 +1674,7 @@ class TestNewFFI1: def test_not_supported_bitfield_in_result(self): # struct ints_and_bitfield { int a,b,c,d,e; int x:1; }; - e = py.test.raises(NotImplementedError, ffi.callback, + e = pytest.raises(NotImplementedError, ffi.callback, "struct ints_and_bitfield foo(void)", lambda: 42) assert str(e.value) == ("struct ints_and_bitfield(*)(): " "callback with unsupported argument or return type or with '...'") @@ -1692,7 +1691,7 @@ class TestNewFFI1: assert ffi.typeof(p) == ffi.typeof("void *") assert ffi.from_handle(p) is o assert ffi.from_handle(ffi.cast("char *", p)) is o - py.test.raises(RuntimeError, ffi.from_handle, ffi.NULL) + pytest.raises(RuntimeError, ffi.from_handle, ffi.NULL) def test_struct_array_no_length(self): # struct array_no_length { int x; int a[]; }; @@ -1709,8 +1708,8 @@ class TestNewFFI1: assert q.a[0] == 200 assert q.a[1] == 300 assert q.a[2] == 400 - py.test.raises(TypeError, len, q.a) - py.test.raises(TypeError, list, q.a) + pytest.raises(TypeError, len, q.a) + pytest.raises(TypeError, list, q.a) def test_all_primitives(self): assert set(PRIMITIVE_TO_INDEX) == set([ |