diff options
Diffstat (limited to 'testing/cffi0/backend_tests.py')
-rw-r--r-- | testing/cffi0/backend_tests.py | 151 |
1 files changed, 75 insertions, 76 deletions
diff --git a/testing/cffi0/backend_tests.py b/testing/cffi0/backend_tests.py index ab013a1..aed1b9c 100644 --- a/testing/cffi0/backend_tests.py +++ b/testing/cffi0/backend_tests.py @@ -1,4 +1,3 @@ -import py import pytest import platform import sys, ctypes, ctypes.util @@ -13,7 +12,7 @@ SIZE_OF_WCHAR = ctypes.sizeof(ctypes.c_wchar) def needs_dlopen_none(): if sys.platform == 'win32' and not ctypes.util.find_library('c'): - py.test.skip("dlopen(None) cannot work on Windows with this runtime") + pytest.skip("dlopen(None) cannot work on Windows with this runtime") class BackendTests: @@ -75,10 +74,10 @@ class BackendTests: 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 @@ -86,7 +85,7 @@ class BackendTests: def test_new_unsupported_type(self): ffi = FFI(backend=self.Backend()) - 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): @@ -148,7 +147,7 @@ class BackendTests: 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 @@ -160,7 +159,7 @@ class BackendTests: 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): @@ -175,7 +174,7 @@ class BackendTests: def test_cannot_cast(self): ffi = FFI(backend=self.Backend()) 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 @@ -301,9 +300,9 @@ class BackendTests: 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 @@ -322,13 +321,13 @@ class BackendTests: 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): ffi = FFI(backend=self.Backend()) @@ -338,7 +337,7 @@ class BackendTests: 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)) @@ -346,8 +345,8 @@ class BackendTests: 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 @@ -382,7 +381,7 @@ class BackendTests: 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): ffi = FFI(backend=self.Backend()) @@ -410,8 +409,8 @@ class BackendTests: # 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 # @@ -444,7 +443,7 @@ class BackendTests: assert repr(s) == "<cdata 'struct foo *' owning %d bytes>" % ( SIZE_OF_INT + 2 * SIZE_OF_SHORT) # - py.test.raises(ValueError, ffi.new, "struct foo*", [1, 2, 3, 4]) + pytest.raises(ValueError, ffi.new, "struct foo*", [1, 2, 3, 4]) def test_constructor_struct_from_dict(self): ffi = FFI(backend=self.Backend()) @@ -453,7 +452,7 @@ class BackendTests: assert s.a == 0 assert s.b == 123 assert s.c == 456 - py.test.raises(KeyError, ffi.new, "struct foo*", {'d': 456}) + pytest.raises(KeyError, ffi.new, "struct foo*", {'d': 456}) def test_struct_pointer(self): ffi = FFI(backend=self.Backend()) @@ -469,7 +468,7 @@ class BackendTests: def test_struct_opaque(self): ffi = FFI(backend=self.Backend()) - py.test.raises(TypeError, ffi.new, "struct baz*") + pytest.raises(TypeError, ffi.new, "struct baz*") p = ffi.new("struct baz **") # this works assert p[0] == ffi.NULL @@ -536,19 +535,19 @@ class BackendTests: def test_union_opaque(self): ffi = FFI(backend=self.Backend()) - py.test.raises(TypeError, ffi.new, "union baz *") + pytest.raises(TypeError, ffi.new, "union baz *") u = ffi.new("union baz **") # this works assert u[0] == ffi.NULL def test_union_initializer(self): ffi = FFI(backend=self.Backend()) ffi.cdef("union foo { char a; int b; };") - py.test.raises(TypeError, ffi.new, "union foo*", b'A') - py.test.raises(TypeError, ffi.new, "union foo*", 5) - py.test.raises(ValueError, ffi.new, "union foo*", [b'A', 5]) + pytest.raises(TypeError, ffi.new, "union foo*", b'A') + pytest.raises(TypeError, ffi.new, "union foo*", 5) + pytest.raises(ValueError, ffi.new, "union foo*", [b'A', 5]) u = ffi.new("union foo*", [b'A']) assert u.a == b'A' - py.test.raises(TypeError, ffi.new, "union foo*", [1005]) + pytest.raises(TypeError, ffi.new, "union foo*", [1005]) u = ffi.new("union foo*", {'b': 12345}) assert u.b == 12345 u = ffi.new("union foo*", []) @@ -587,7 +586,7 @@ class BackendTests: 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): ffi = FFI(backend=self.Backend()) @@ -670,7 +669,7 @@ class BackendTests: def test_voidp(self): ffi = FFI(backend=self.Backend()) - 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]) @@ -678,7 +677,7 @@ class BackendTests: vp = p[0] with pytest.raises(TypeError): vp[0] - py.test.raises(TypeError, ffi.new, "short **", a) + pytest.raises(TypeError, ffi.new, "short **", a) # ffi.cdef("struct foo { void *p; int *q; short *r; };") s = ffi.new("struct foo *") @@ -694,7 +693,7 @@ class BackendTests: def test_functionptr_simple(self): ffi = FFI(backend=self.Backend()) - 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' @@ -970,7 +969,7 @@ class BackendTests: needs_dlopen_none() lib = ffi.dlopen(None) assert lib.B == 0 - py.test.raises(VerificationMissing, getattr, lib, "A") + pytest.raises(VerificationMissing, getattr, lib, "A") assert lib.C == 1 def test_array_of_struct(self): @@ -997,10 +996,10 @@ class BackendTests: 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): ffi = FFI(backend=self.Backend()) @@ -1014,7 +1013,7 @@ class BackendTests: ffi.cdef("struct foo { int a, b, c; };" "struct bar { struct foo d, e; };") assert ffi.offsetof("struct bar", "e") == 12 - py.test.raises(KeyError, ffi.offsetof, "struct bar", "e.a") + pytest.raises(KeyError, ffi.offsetof, "struct bar", "e.a") assert ffi.offsetof("struct bar", "e", "a") == 12 assert ffi.offsetof("struct bar", "e", "b") == 16 assert ffi.offsetof("struct bar", "e", "c") == 20 @@ -1170,7 +1169,7 @@ class BackendTests: try: b = ffi.buffer(a) except NotImplementedError as e: - py.test.skip(str(e)) + pytest.skip(str(e)) assert type(b) is ffi.buffer content = b[:] assert len(content) == len(b) == 2 @@ -1190,7 +1189,7 @@ class BackendTests: 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') @@ -1207,7 +1206,7 @@ class BackendTests: 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': @@ -1226,7 +1225,7 @@ class BackendTests: 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): @@ -1238,7 +1237,7 @@ class BackendTests: 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))) @@ -1257,7 +1256,7 @@ class BackendTests: 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))) @@ -1280,7 +1279,7 @@ class BackendTests: b_mid = ffi.buffer(a, 6) b_other = ffi.buffer(c, 6) except NotImplementedError as e: - py.test.skip(str(e)) + pytest.skip(str(e)) else: content = b_full[:] assert content == b_full == ba @@ -1309,7 +1308,7 @@ class BackendTests: assert q.data[6] == 15 def test_new_struct_containing_array_varsize(self): - py.test.skip("later?") + pytest.skip("later?") ffi = FFI(backend=self.Backend()) ffi.cdef("struct foo_s { int len; short data[]; };") p = ffi.new("struct foo_s *", 10) # a single integer is the length @@ -1340,14 +1339,14 @@ class BackendTests: ffi = FFI(backend=self.Backend()) f = ffi.cast("int(*)(int)", 42) assert f != ffi.NULL - py.test.raises(CDefError, ffi.cast, "int(int)", 42) - py.test.raises(CDefError, ffi.new, "int([5])(int)") + pytest.raises(CDefError, ffi.cast, "int(int)", 42) + pytest.raises(CDefError, 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 @@ -1369,7 +1368,7 @@ class BackendTests: assert g(f) == b'B' def test_vararg_callback(self): - py.test.skip("callback with '...'") + pytest.skip("callback with '...'") ffi = FFI(backend=self.Backend()) def cb(i, va_list): j = ffi.va_arg(va_list, "int") @@ -1433,7 +1432,7 @@ class BackendTests: def test_new_ctype(self): ffi = FFI(backend=self.Backend()) 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 @@ -1479,7 +1478,7 @@ class BackendTests: assert p.b == 12 assert p.c == 14 assert p.d == 14 - py.test.raises(ValueError, ffi.new, "struct foo_s *", [0, 0, 0, 0]) + pytest.raises(ValueError, ffi.new, "struct foo_s *", [0, 0, 0, 0]) def test_nested_field_offset_align(self): ffi = FFI(backend=self.Backend()) @@ -1529,7 +1528,7 @@ class BackendTests: assert p.b == 456 assert p.c == 123 assert p.d == 123 - py.test.raises(ValueError, ffi.new, "union foo_u *", [0, 0, 0]) + pytest.raises(ValueError, ffi.new, "union foo_u *", [0, 0, 0]) def test_nested_anonymous_struct_2(self): ffi = FFI(backend=self.Backend()) @@ -1545,7 +1544,7 @@ class BackendTests: assert p.a == 11 assert p.b == p.c == p.d == 22 assert p.e == 33 - py.test.raises(ValueError, ffi.new, "struct foo_s *", [11, 22, 33, 44]) + pytest.raises(ValueError, ffi.new, "struct foo_s *", [11, 22, 33, 44]) FOO = ffi.typeof("struct foo_s") fields = [(name, fld.offset, fld.flags) for (name, fld) in FOO.fields] assert fields == [ @@ -1626,7 +1625,7 @@ class BackendTests: def test_gc_disable(self): ffi = FFI(backend=self.Backend()) p = ffi.new("int *", 123) - py.test.raises(TypeError, ffi.gc, p, None) + pytest.raises(TypeError, ffi.gc, p, None) seen = [] q1 = ffi.gc(p, lambda p: seen.append(1)) q2 = ffi.gc(q1, lambda p: seen.append(2)) @@ -1669,8 +1668,8 @@ class BackendTests: 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_use_own_bool(self): ffi = FFI(backend=self.Backend()) @@ -1713,9 +1712,9 @@ class BackendTests: a = ffi.addressof(p[0]) assert repr(a).startswith("<cdata 'struct foo_s *' 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): ffi = FFI(backend=self.Backend()) @@ -1733,7 +1732,7 @@ class BackendTests: ffi.cdef("struct foo_s { int x, y; };" "struct bar_s { struct foo_s a, b; };") p = ffi.new("struct bar_s *") - py.test.raises(KeyError, ffi.addressof, p[0], 'b.y') + pytest.raises(KeyError, ffi.addressof, p[0], 'b.y') a = ffi.addressof(p[0], 'b', 'y') assert int(ffi.cast("uintptr_t", a)) == ( int(ffi.cast("uintptr_t", p)) + @@ -1752,7 +1751,7 @@ class BackendTests: 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 *") @@ -1763,7 +1762,7 @@ class BackendTests: ffi = FFI() 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) @@ -1771,7 +1770,7 @@ class BackendTests: ffi.cdef("struct foo { int a, b; };") array = ffi.new("struct foo[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) @@ -1803,7 +1802,7 @@ class BackendTests: ffi1 = FFI(backend=backend) ffi2 = FFI(backend=backend) ffi1.cdef("typedef signed char schar_t;") - py.test.raises(CDefError, ffi2.cast, "schar_t", 142) + pytest.raises(CDefError, ffi2.cast, "schar_t", 142) def test_include_typedef(self): backend = self.Backend() @@ -1855,7 +1854,7 @@ class BackendTests: ffi = FFI(backend=self.Backend()) ffi.cdef("#define FOO 42") ffi.cdef("#define FOO 42") - py.test.raises(FFIError, ffi.cdef, "#define FOO 43") + pytest.raises(FFIError, ffi.cdef, "#define FOO 43") def test_struct_packed(self): ffi = FFI(backend=self.Backend()) @@ -1891,9 +1890,9 @@ class BackendTests: def test_pack_valueerror(self): ffi = FFI(backend=self.Backend()) - py.test.raises(ValueError, ffi.cdef, "", pack=3) - py.test.raises(ValueError, ffi.cdef, "", packed=2) - py.test.raises(ValueError, ffi.cdef, "", packed=True, pack=1) + pytest.raises(ValueError, ffi.cdef, "", pack=3) + pytest.raises(ValueError, ffi.cdef, "", packed=2) + pytest.raises(ValueError, ffi.cdef, "", packed=True, pack=1) def test_define_integer_constant(self): ffi = FFI(backend=self.Backend()) @@ -1922,13 +1921,13 @@ class BackendTests: # definition was ignored. ffi = FFI(backend=self.Backend()) ffi.cdef("struct foo_s;") - py.test.raises(TypeError, ffi.new, "struct foo_s *") + pytest.raises(TypeError, ffi.new, "struct foo_s *") ffi.cdef("struct foo_s { int x; };") ffi.new("struct foo_s *") def test_ffi_self_include(self): ffi = FFI(backend=self.Backend()) - py.test.raises(ValueError, ffi.include, ffi) + pytest.raises(ValueError, ffi.include, ffi) def test_anonymous_enum_include(self): ffi1 = FFI() @@ -2016,12 +2015,12 @@ class BackendTests: p = ffi.new("int[4]", [10, 20, 30, 400]) q = ffi.new("int[4]", p) assert list(q) == [10, 20, 30, 400] - py.test.raises(TypeError, ffi.new, "int[3]", p) - py.test.raises(TypeError, ffi.new, "int[5]", p) - py.test.raises(TypeError, ffi.new, "int16_t[4]", p) + pytest.raises(TypeError, ffi.new, "int[3]", p) + pytest.raises(TypeError, ffi.new, "int[5]", p) + pytest.raises(TypeError, ffi.new, "int16_t[4]", p) s = ffi.new("struct {int i[4];}*", {'i': p}) assert list(s.i) == [10, 20, 30, 400] def test_too_many_initializers(self): ffi = FFI(backend=self.Backend()) - py.test.raises(IndexError, ffi.new, "int[4]", [10, 20, 30, 40, 50]) + pytest.raises(IndexError, ffi.new, "int[4]", [10, 20, 30, 40, 50]) |