[pypy-commit] cffi cffi-1.0: in-progress: import backend_tests
arigo
noreply at buildbot.pypy.org
Sun Apr 26 10:13:38 CEST 2015
Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-1.0
Changeset: r1831:48276f27dd98
Date: 2015-04-26 10:14 +0200
http://bitbucket.org/cffi/cffi/changeset/48276f27dd98/
Log: in-progress: import backend_tests
diff --git a/testing/backend_tests.py b/_cffi1/test_new_ffi_1.py
copy from testing/backend_tests.py
copy to _cffi1/test_new_ffi_1.py
--- a/testing/backend_tests.py
+++ b/_cffi1/test_new_ffi_1.py
@@ -1,8 +1,11 @@
import py
-import platform
+import platform, imp
import sys, ctypes
-from cffi import FFI, CDefError, FFIError
-from testing.support import *
+import cffi
+from cffi import CDefError, FFIError
+from .udir import udir
+from .recompiler import recompile
+from .support import *
SIZE_OF_INT = ctypes.sizeof(ctypes.c_int)
SIZE_OF_LONG = ctypes.sizeof(ctypes.c_long)
@@ -11,10 +14,75 @@
SIZE_OF_WCHAR = ctypes.sizeof(ctypes.c_wchar)
-class BackendTests:
+def setup_module():
+ global ffi
+ ffi1 = cffi.FFI()
+ DEFS = r"""
+ struct repr { short a, b, c; };
+ struct simple { int a; short b, c; };
+ struct array { int a[2]; char b[3]; };
+ struct recursive { int value; struct recursive *next; };
+ union simple_u { int a; short b, c; };
+ union init_u { char a; int b; };
+ struct four_s { int a; short b, c, d; };
+ union four_u { int a; short b, c, d; };
+ struct string { const char *name; };
+ struct ustring { const wchar_t *name; };
+ struct voidp { void *p; int *q; short *r; };
+ struct ab { int a, b; };
+ struct abc { int a, b, c; };
+
+ enum foq { A0, B0, CC0, D0 };
+ enum bar { A1, B1=-2, CC1, D1, E1 };
+ enum baz { A2=0x1000, B2=0x2000 };
+ enum foo2 { A3, B3, C3, D3 };
+ struct bar_with_e { enum foo2 e; };
+ enum noncont { A4, B4=42, C4 };
+ enum etypes {A5='!', B5='\'', C5=0x10, D5=010, E5=- 0x10, F5=-010};
+ typedef enum { Value0 = 0 } e_t, *pe_t;
+ enum e_noninj { AA3=0, BB3=0, CC3=0, DD3=0 };
+ enum e_prev { AA4, BB4=2, CC4=4, DD4=BB4, EE4, FF4=CC4, GG4=FF4 };
+
+ struct nesting { struct abc d, e; };
+ struct array2 { int a, b; int c[99]; };
+ struct align { char a; short b; char c; };
+ struct bitfield { int a:10, b:20, c:3; };
+ typedef enum { AA2, BB2, CC2 } foo_e_t;
+ typedef struct { foo_e_t f:2; } bfenum_t;
+ typedef struct { int a; } anon_foo_t;
+ typedef struct { char b, c; } anon_bar_t;
+ typedef struct named_foo_s { int a; } named_foo_t, *named_foo_p;
+ typedef struct { int a; } unnamed_foo_t, *unnamed_foo_p;
+ struct nonpacked { char a; int b; };
+ struct array0 { int len; short data[0]; };
+
+ struct nested_anon {
+ struct { int a, b; };
+ union { int c, d; };
+ };
+ union nested_anon_u {
+ struct { int a, b; };
+ union { int c, d; };
+ };
+ struct abc50 { int a, b; int c[50]; };
+ """
+ DEFS_PACKED = """
+ struct is_packed { char a; int b; } /*here*/;
+ """
+ CCODE = DEFS + DEFS_PACKED.replace('/*here*/', '__attribute__((packed))')
+
+ ffi1.cdef(DEFS)
+ ffi1.cdef(DEFS_PACKED, packed=True)
+
+ outputfilename = recompile(ffi1, "test_old_ffi1", CCODE,
+ tmpdir=str(udir))
+ module = imp.load_dynamic("test_old_ffi1", outputfilename)
+ ffi = module.ffi
+
+
+class TestOldFFI1:
def test_integer_ranges(self):
- ffi = FFI(backend=self.Backend())
for (c_type, size) in [('char', 1),
('short', 2),
('short int', 2),
@@ -34,7 +102,6 @@
self._test_int_type(ffi, c_decl, size, unsigned)
def test_fixedsize_int(self):
- ffi = FFI(backend=self.Backend())
for size in [1, 2, 4, 8]:
self._test_int_type(ffi, 'int%d_t' % (8*size), size, False)
self._test_int_type(ffi, 'uint%d_t' % (8*size), size, True)
@@ -79,12 +146,10 @@
assert ffi.new(c_decl_ptr, long(max))[0] == max
def test_new_unsupported_type(self):
- ffi = FFI(backend=self.Backend())
e = py.test.raises(TypeError, ffi.new, "int")
assert str(e.value) == "expected a pointer or array ctype, got 'int'"
def test_new_single_integer(self):
- ffi = FFI(backend=self.Backend())
p = ffi.new("int *") # similar to ffi.new("int[1]")
assert p[0] == 0
p[0] = -123
@@ -94,14 +159,12 @@
assert repr(p) == "<cdata 'int *' owning %d bytes>" % SIZE_OF_INT
def test_new_array_no_arg(self):
- ffi = FFI(backend=self.Backend())
p = ffi.new("int[10]")
# the object was zero-initialized:
for i in range(10):
assert p[i] == 0
def test_array_indexing(self):
- ffi = FFI(backend=self.Backend())
p = ffi.new("int[10]")
p[0] = 42
p[9] = 43
@@ -113,7 +176,6 @@
py.test.raises(IndexError, "p[-1] = 44")
def test_new_array_args(self):
- ffi = FFI(backend=self.Backend())
# this tries to be closer to C: where we say "int x[5] = {10, 20, ..}"
# then here we must enclose the items in a list
p = ffi.new("int[5]", [10, 20, 30, 40, 50])
@@ -132,7 +194,6 @@
assert repr(p) == "<cdata 'int[4]' owning %d bytes>" % (4*SIZE_OF_INT)
def test_new_array_varsize(self):
- ffi = FFI(backend=self.Backend())
p = ffi.new("int[]", 10) # a single integer is the length
assert p[9] == 0
py.test.raises(IndexError, "p[10]")
@@ -151,7 +212,6 @@
assert repr(p) == "<cdata 'int[]' owning 0 bytes>"
def test_pointer_init(self):
- ffi = FFI(backend=self.Backend())
n = ffi.new("int *", 24)
a = ffi.new("int *[10]", [ffi.NULL, ffi.NULL, n, n, ffi.NULL])
for i in range(10):
@@ -160,14 +220,12 @@
assert a[2] == a[3] == n
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)
msg = str(e.value)
assert "'short[10]'" in msg and "'long *'" in msg
def test_new_pointer_to_array(self):
- ffi = FFI(backend=self.Backend())
a = ffi.new("int[4]", [100, 102, 104, 106])
p = ffi.new("int **", a)
assert p[0] == ffi.cast("int *", a)
@@ -180,7 +238,6 @@
# keepalive: a
def test_pointer_direct(self):
- ffi = FFI(backend=self.Backend())
p = ffi.cast("int*", 0)
assert p is not None
assert bool(p) is False
@@ -196,9 +253,7 @@
assert p[1] == 456
def test_repr(self):
- typerepr = self.TypeRepr
- ffi = FFI(backend=self.Backend())
- ffi.cdef("struct foo { short a, b, c; };")
+ typerepr = "<ctype '%s'>"
p = ffi.cast("short unsigned int", 0)
assert repr(p) == "<cdata 'unsigned short' 0>"
assert repr(ffi.typeof(p)) == typerepr % "unsigned short"
@@ -222,10 +277,10 @@
assert repr(p) == "<cdata 'int *[2][3]' owning %d bytes>" % (
6*SIZE_OF_PTR)
assert repr(ffi.typeof(p)) == typerepr % "int *[2][3]"
- p = ffi.new("struct foo *")
- assert repr(p) == "<cdata 'struct foo *' owning %d bytes>" % (
+ p = ffi.new("struct repr *")
+ assert repr(p) == "<cdata 'struct repr *' owning %d bytes>" % (
3*SIZE_OF_SHORT)
- assert repr(ffi.typeof(p)) == typerepr % "struct foo *"
+ assert repr(ffi.typeof(p)) == typerepr % "struct repr *"
#
q = ffi.cast("short", -123)
assert repr(q) == "<cdata 'short' -123>"
@@ -238,17 +293,16 @@
q = ffi.cast("int*", p)
assert repr(q).startswith("<cdata 'int *' 0x")
assert repr(ffi.typeof(q)) == typerepr % "int *"
- p = ffi.new("struct foo*")
- q = ffi.cast("struct foo *", p)
- assert repr(q).startswith("<cdata 'struct foo *' 0x")
- assert repr(ffi.typeof(q)) == typerepr % "struct foo *"
+ p = ffi.new("struct repr*")
+ q = ffi.cast("struct repr *", p)
+ assert repr(q).startswith("<cdata 'struct repr *' 0x")
+ assert repr(ffi.typeof(q)) == typerepr % "struct repr *"
prevrepr = repr(q)
q = q[0]
assert repr(q) == prevrepr.replace(' *', ' &')
- assert repr(ffi.typeof(q)) == typerepr % "struct foo"
+ assert repr(ffi.typeof(q)) == typerepr % "struct repr"
def test_new_array_of_array(self):
- ffi = FFI(backend=self.Backend())
p = ffi.new("int[3][4]")
p[0][0] = 10
p[2][3] = 33
@@ -257,12 +311,10 @@
py.test.raises(IndexError, "p[1][-1]")
def test_constructor_array_of_array(self):
- ffi = FFI(backend=self.Backend())
p = ffi.new("int[3][2]", [[10, 11], [12, 13], [14, 15]])
assert p[2][1] == 15
def test_new_array_of_pointer_1(self):
- ffi = FFI(backend=self.Backend())
n = ffi.new("int*", 99)
p = ffi.new("int*[4]")
p[3] = n
@@ -271,7 +323,6 @@
assert a[0] == 99
def test_new_array_of_pointer_2(self):
- ffi = FFI(backend=self.Backend())
n = ffi.new("int[1]", [99])
p = ffi.new("int*[4]")
p[3] = n
@@ -280,7 +331,6 @@
assert a[0] == 99
def test_char(self):
- ffi = FFI(backend=self.Backend())
assert ffi.new("char*", b"\xff")[0] == b'\xff'
assert ffi.new("char*")[0] == b'\x00'
assert int(ffi.cast("char", 300)) == 300 - 256
@@ -315,7 +365,6 @@
py.test.skip("NotImplementedError: wchar_t")
def test_wchar_t(self):
- ffi = FFI(backend=self.Backend())
self.check_wchar_t(ffi)
assert ffi.new("wchar_t*", u+'x')[0] == u+'x'
assert ffi.new("wchar_t*", u+'\u1234')[0] == u+'\u1234'
@@ -366,7 +415,6 @@
py.test.raises(IndexError, ffi.new, "wchar_t[2]", u+"abc")
def test_none_as_null_doesnt_work(self):
- ffi = FFI(backend=self.Backend())
p = ffi.new("int*[1]")
assert p[0] is not None
assert p[0] != None
@@ -381,7 +429,6 @@
assert p[0] == ffi.NULL
def test_float(self):
- ffi = FFI(backend=self.Backend())
p = ffi.new("float[]", [-2, -2.5])
assert p[0] == -2.0
assert p[1] == -2.5
@@ -406,37 +453,31 @@
assert p[0] == INF # infinite, not enough precision
def test_struct_simple(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("struct foo { int a; short b, c; };")
- s = ffi.new("struct foo*")
+ s = ffi.new("struct simple*")
assert s.a == s.b == s.c == 0
s.b = -23
assert s.b == -23
py.test.raises(OverflowError, "s.b = 32768")
#
- s = ffi.new("struct foo*", [-2, -3])
+ s = ffi.new("struct simple*", [-2, -3])
assert s.a == -2
assert s.b == -3
assert s.c == 0
py.test.raises((AttributeError, TypeError), "del s.a")
- assert repr(s) == "<cdata 'struct foo *' owning %d bytes>" % (
+ assert repr(s) == "<cdata 'struct simple *' owning %d bytes>" % (
SIZE_OF_INT + 2 * SIZE_OF_SHORT)
#
- py.test.raises(ValueError, ffi.new, "struct foo*", [1, 2, 3, 4])
+ py.test.raises(ValueError, ffi.new, "struct simple*", [1, 2, 3, 4])
def test_constructor_struct_from_dict(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("struct foo { int a; short b, c; };")
- s = ffi.new("struct foo*", {'b': 123, 'c': 456})
+ 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 foo*", {'d': 456})
+ py.test.raises(KeyError, ffi.new, "struct simple*", {'d': 456})
def test_struct_pointer(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("struct foo { int a; short b, c; };")
- s = ffi.new("struct foo*")
+ s = ffi.new("struct simple*")
assert s[0].a == s[0].b == s[0].c == 0
s[0].b = -23
assert s[0].b == s.b == -23
@@ -444,18 +485,16 @@
py.test.raises(IndexError, "s[1]")
def test_struct_opaque(self):
- ffi = FFI(backend=self.Backend())
- py.test.raises(TypeError, ffi.new, "struct baz*")
- p = ffi.new("struct baz **") # this works
- assert p[0] == ffi.NULL
+ py.test.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**")
def test_pointer_to_struct(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("struct foo { int a; short b, c; };")
- s = ffi.new("struct foo *")
+ s = ffi.new("struct simple *")
s.a = -42
assert s[0].a == -42
- p = ffi.new("struct foo **", s)
+ p = ffi.new("struct simple **", s)
assert p[0].a == -42
assert p[0][0].a == -42
p[0].a = -43
@@ -472,9 +511,7 @@
assert p[0][0].a == -46
def test_constructor_struct_of_array(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("struct foo { int a[2]; char b[3]; };")
- s = ffi.new("struct foo *", [[10, 11], [b'a', b'b', b'c']])
+ s = ffi.new("struct array *", [[10, 11], [b'a', b'b', b'c']])
assert s.a[1] == 11
assert s.b[2] == b'c'
s.b[1] = b'X'
@@ -483,10 +520,8 @@
assert s.b[2] == b'c'
def test_recursive_struct(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("struct foo { int value; struct foo *next; };")
- s = ffi.new("struct foo*")
- t = ffi.new("struct foo*")
+ s = ffi.new("struct recursive*")
+ t = ffi.new("struct recursive*")
s.value = 123
s.next = t
t.value = 456
@@ -494,60 +529,51 @@
assert s.next.value == 456
def test_union_simple(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("union foo { int a; short b, c; };")
- u = ffi.new("union foo*")
+ u = ffi.new("union simple_u*")
assert u.a == u.b == u.c == 0
u.b = -23
assert u.b == -23
assert u.a != 0
py.test.raises(OverflowError, "u.b = 32768")
#
- u = ffi.new("union foo*", [-2])
+ u = ffi.new("union simple_u*", [-2])
assert u.a == -2
py.test.raises((AttributeError, TypeError), "del u.a")
- assert repr(u) == "<cdata 'union foo *' owning %d bytes>" % SIZE_OF_INT
+ assert repr(u) == "<cdata 'union simple_u *' owning %d bytes>" % (
+ SIZE_OF_INT,)
def test_union_opaque(self):
- ffi = FFI(backend=self.Backend())
- py.test.raises(TypeError, ffi.new, "union baz *")
- u = ffi.new("union baz **") # this works
- assert u[0] == ffi.NULL
+ py.test.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**")
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])
- u = ffi.new("union foo*", [b'A'])
+ 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])
+ u = ffi.new("union init_u*", [b'A'])
assert u.a == b'A'
- py.test.raises(TypeError, ffi.new, "union foo*", [1005])
- u = ffi.new("union foo*", {'b': 12345})
+ py.test.raises(TypeError, ffi.new, "union init_u*", [1005])
+ u = ffi.new("union init_u*", {'b': 12345})
assert u.b == 12345
- u = ffi.new("union foo*", [])
+ u = ffi.new("union init_u*", [])
assert u.a == b'\x00'
assert u.b == 0
def test_sizeof_type(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("""
- struct foo { int a; short b, c, d; };
- union foo { int a; short b, c, d; };
- """)
for c_type, expected_size in [
('char', 1),
('unsigned int', 4),
('char *', SIZE_OF_PTR),
('int[5]', 20),
- ('struct foo', 12),
- ('union foo', 4),
+ ('struct four_s', 12),
+ ('union four_u', 4),
]:
size = ffi.sizeof(c_type)
assert size == expected_size, (size, expected_size, ctype)
def test_sizeof_cdata(self):
- ffi = FFI(backend=self.Backend())
assert ffi.sizeof(ffi.new("short*")) == SIZE_OF_PTR
assert ffi.sizeof(ffi.cast("short", 123)) == SIZE_OF_SHORT
#
@@ -556,7 +582,6 @@
assert ffi.sizeof(a) == 5 * SIZE_OF_INT
def test_string_from_char_pointer(self):
- ffi = FFI(backend=self.Backend())
x = ffi.new("char*", b"x")
assert str(x) == repr(x)
assert ffi.string(x) == b"x"
@@ -564,7 +589,6 @@
py.test.raises(TypeError, ffi.new, "char*", unicode("foo"))
def test_unicode_from_wchar_pointer(self):
- ffi = FFI(backend=self.Backend())
self.check_wchar_t(ffi)
x = ffi.new("wchar_t*", u+"x")
assert unicode(x) == unicode(repr(x))
@@ -572,7 +596,6 @@
assert ffi.string(ffi.new("wchar_t*", u+"\x00")) == u+""
def test_string_from_char_array(self):
- ffi = FFI(backend=self.Backend())
p = ffi.new("char[]", b"hello.")
p[5] = b'!'
assert ffi.string(p) == b"hello!"
@@ -589,7 +612,6 @@
assert ffi.string(p) == b'hello'
def test_string_from_wchar_array(self):
- ffi = FFI(backend=self.Backend())
self.check_wchar_t(ffi)
assert ffi.string(ffi.cast("wchar_t", "x")) == u+"x"
assert ffi.string(ffi.cast("wchar_t", u+"x")) == u+"x"
@@ -616,11 +638,9 @@
assert ffi.string(p, 2) == u+'he'
def test_fetch_const_char_p_field(self):
- # 'const' is ignored so far
- ffi = FFI(backend=self.Backend())
- ffi.cdef("struct foo { const char *name; };")
+ # 'const' is ignored so far, in the declaration of 'struct string'
t = ffi.new("const char[]", b"testing")
- s = ffi.new("struct foo*", [t])
+ s = ffi.new("struct string*", [t])
assert type(s.name) not in (bytes, str, unicode)
assert ffi.string(s.name) == b"testing"
py.test.raises(TypeError, "s.name = None")
@@ -629,18 +649,15 @@
def test_fetch_const_wchar_p_field(self):
# 'const' is ignored so far
- ffi = FFI(backend=self.Backend())
self.check_wchar_t(ffi)
- ffi.cdef("struct foo { const wchar_t *name; };")
t = ffi.new("const wchar_t[]", u+"testing")
- s = ffi.new("struct foo*", [t])
+ s = ffi.new("struct ustring*", [t])
assert type(s.name) not in (bytes, str, unicode)
assert ffi.string(s.name) == u+"testing"
s.name = ffi.NULL
assert s.name == ffi.NULL
def test_voidp(self):
- ffi = FFI(backend=self.Backend())
py.test.raises(TypeError, ffi.new, "void*")
p = ffi.new("void **")
assert p[0] == ffi.NULL
@@ -650,8 +667,7 @@
py.test.raises(TypeError, "vp[0]")
py.test.raises(TypeError, ffi.new, "short **", a)
#
- ffi.cdef("struct foo { void *p; int *q; short *r; };")
- s = ffi.new("struct foo *")
+ s = ffi.new("struct voidp *")
s.p = a # works
s.q = a # works
py.test.raises(TypeError, "s.r = a") # fails
@@ -661,7 +677,6 @@
py.test.raises(TypeError, "s.r = b") # fails
def test_functionptr_simple(self):
- ffi = FFI(backend=self.Backend())
py.test.raises(TypeError, ffi.callback, "int(*)(int)", 0)
def cb(n):
return n + 1
@@ -686,12 +701,10 @@
assert res == 46
def test_functionptr_advanced(self):
- ffi = FFI(backend=self.Backend())
t = ffi.typeof("int(*(*)(int))(int)")
- assert repr(t) == self.TypeRepr % "int(*(*)(int))(int)"
+ assert repr(t) == "<ctype '%s'>" % "int(*(*)(int))(int)"
def test_functionptr_voidptr_return(self):
- ffi = FFI(backend=self.Backend())
def cb():
return ffi.NULL
p = ffi.callback("void*(*)()", cb)
@@ -707,7 +720,6 @@
assert res == void_ptr
def test_functionptr_intptr_return(self):
- ffi = FFI(backend=self.Backend())
def cb():
return ffi.NULL
p = ffi.callback("int*(*)()", cb)
@@ -729,7 +741,6 @@
assert res == int_array_ptr
def test_functionptr_void_return(self):
- ffi = FFI(backend=self.Backend())
def foo():
pass
foo_cb = ffi.callback("void foo()", foo)
@@ -737,7 +748,6 @@
assert result is None
def test_char_cast(self):
- ffi = FFI(backend=self.Backend())
p = ffi.cast("int", b'\x01')
assert ffi.typeof(p) is ffi.typeof("int")
assert int(p) == 1
@@ -749,7 +759,6 @@
assert int(p) == 0x81
def test_wchar_cast(self):
- ffi = FFI(backend=self.Backend())
self.check_wchar_t(ffi)
p = ffi.cast("int", ffi.cast("wchar_t", u+'\u1234'))
assert int(p) == 0x1234
@@ -764,7 +773,6 @@
assert int(p) == 0x1234
def test_cast_array_to_charp(self):
- ffi = FFI(backend=self.Backend())
a = ffi.new("short int[]", [0x1234, 0x5678])
p = ffi.cast("char*", a)
data = b''.join([p[i] for i in range(4)])
@@ -774,7 +782,6 @@
assert data == b'\x12\x34\x56\x78'
def test_cast_between_pointers(self):
- ffi = FFI(backend=self.Backend())
a = ffi.new("short int[]", [0x1234, 0x5678])
p = ffi.cast("short*", a)
p2 = ffi.cast("int*", p)
@@ -786,7 +793,6 @@
assert data == b'\x12\x34\x56\x78'
def test_cast_pointer_and_int(self):
- ffi = FFI(backend=self.Backend())
a = ffi.new("short int[]", [0x1234, 0x5678])
l1 = ffi.cast("intptr_t", a)
p = ffi.cast("short*", a)
@@ -798,7 +804,6 @@
assert int(ffi.cast("intptr_t", ffi.NULL)) == 0
def test_cast_functionptr_and_int(self):
- ffi = FFI(backend=self.Backend())
def cb(n):
return n + 1
a = ffi.callback("int(*)(int)", cb)
@@ -810,7 +815,6 @@
assert hash(a) == hash(b)
def test_callback_crash(self):
- ffi = FFI(backend=self.Backend())
def cb(n):
raise Exception
a = ffi.callback("int(*)(int)", cb, error=42)
@@ -818,19 +822,15 @@
assert res == 42
def test_structptr_argument(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("struct foo_s { int a, b; };")
def cb(p):
return p[0].a * 1000 + p[0].b * 100 + p[1].a * 10 + p[1].b
- a = ffi.callback("int(*)(struct foo_s[])", cb)
+ a = ffi.callback("int(*)(struct ab[])", cb)
res = a([[5, 6], {'a': 7, 'b': 8}])
assert res == 5678
res = a([[5], {'b': 8}])
assert res == 5008
def test_array_argument_as_list(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("struct foo_s { int a, b; };")
seen = []
def cb(argv):
seen.append(ffi.string(argv[0]))
@@ -840,7 +840,6 @@
assert seen == [b"foobar", b"baz"]
def test_cast_float(self):
- ffi = FFI(backend=self.Backend())
a = ffi.cast("float", 12)
assert float(a) == 12.0
a = ffi.cast("float", 12.5)
@@ -864,31 +863,30 @@
assert ffi.string(a) == b"B"
def test_enum(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("enum foo { A0, B0, CC0, D0 };")
- assert ffi.string(ffi.cast("enum foo", 0)) == "A0"
- assert ffi.string(ffi.cast("enum foo", 2)) == "CC0"
- assert ffi.string(ffi.cast("enum foo", 3)) == "D0"
- assert ffi.string(ffi.cast("enum foo", 4)) == "4"
- ffi.cdef("enum bar { A1, B1=-2, CC1, D1, E1 };")
+ # enum foq { A0, B0, CC0, D0 };
+ assert ffi.string(ffi.cast("enum foq", 0)) == "A0"
+ assert ffi.string(ffi.cast("enum foq", 2)) == "CC0"
+ assert ffi.string(ffi.cast("enum foq", 3)) == "D0"
+ assert ffi.string(ffi.cast("enum foq", 4)) == "4"
+ # enum bar { A1, B1=-2, CC1, D1, E1 };
assert ffi.string(ffi.cast("enum bar", 0)) == "A1"
assert ffi.string(ffi.cast("enum bar", -2)) == "B1"
assert ffi.string(ffi.cast("enum bar", -1)) == "CC1"
assert ffi.string(ffi.cast("enum bar", 1)) == "E1"
assert ffi.cast("enum bar", -2) != ffi.cast("enum bar", -2)
- assert ffi.cast("enum foo", 0) != ffi.cast("enum bar", 0)
+ assert ffi.cast("enum foq", 0) != ffi.cast("enum bar", 0)
assert ffi.cast("enum bar", 0) != ffi.cast("int", 0)
assert repr(ffi.cast("enum bar", -1)) == "<cdata 'enum bar' -1: CC1>"
- assert repr(ffi.cast("enum foo", -1)) == ( # enums are unsigned, if
- "<cdata 'enum foo' 4294967295>") # they contain no neg value
- ffi.cdef("enum baz { A2=0x1000, B2=0x2000 };")
+ assert repr(ffi.cast("enum foq", -1)) == ( # enums are unsigned, if
+ "<cdata 'enum foq' 4294967295>") # they contain no neg value
+ # enum baz { A2=0x1000, B2=0x2000 };
assert ffi.string(ffi.cast("enum baz", 0x1000)) == "A2"
assert ffi.string(ffi.cast("enum baz", 0x2000)) == "B2"
def test_enum_in_struct(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("enum foo { A, B, C, D }; struct bar { enum foo e; };")
- s = ffi.new("struct bar *")
+ # enum foo2 { A3, B3, C3, D3 };
+ # struct bar_with_e { enum foo2 e; };
+ s = ffi.new("struct bar_with_e *")
s.e = 0
assert s.e == 0
s.e = 3
@@ -897,39 +895,35 @@
s[0].e = 2
assert s.e == 2
assert s[0].e == 2
- s.e = ffi.cast("enum foo", -1)
+ s.e = ffi.cast("enum foo2", -1)
assert s.e == 4294967295
assert s[0].e == 4294967295
s.e = s.e
- py.test.raises(TypeError, "s.e = 'B'")
+ py.test.raises(TypeError, "s.e = 'B3'")
py.test.raises(TypeError, "s.e = '2'")
py.test.raises(TypeError, "s.e = '#2'")
py.test.raises(TypeError, "s.e = '#7'")
def test_enum_non_contiguous(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("enum foo { A, B=42, C };")
- assert ffi.string(ffi.cast("enum foo", 0)) == "A"
- assert ffi.string(ffi.cast("enum foo", 42)) == "B"
- assert ffi.string(ffi.cast("enum foo", 43)) == "C"
- invalid_value = ffi.cast("enum foo", 2)
+ # enum noncont { A4, B4=42, C4 };
+ assert ffi.string(ffi.cast("enum noncont", 0)) == "A4"
+ assert ffi.string(ffi.cast("enum noncont", 42)) == "B4"
+ assert ffi.string(ffi.cast("enum noncont", 43)) == "C4"
+ invalid_value = ffi.cast("enum noncont", 2)
assert int(invalid_value) == 2
assert ffi.string(invalid_value) == "2"
def test_enum_char_hex_oct(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef(r"enum foo{A='!', B='\'', C=0x10, D=010, E=- 0x10, F=-010};")
- assert ffi.string(ffi.cast("enum foo", ord('!'))) == "A"
- assert ffi.string(ffi.cast("enum foo", ord("'"))) == "B"
- assert ffi.string(ffi.cast("enum foo", 16)) == "C"
- assert ffi.string(ffi.cast("enum foo", 8)) == "D"
- assert ffi.string(ffi.cast("enum foo", -16)) == "E"
- assert ffi.string(ffi.cast("enum foo", -8)) == "F"
+ # enum etypes {A5='!', B5='\'', C5=0x10, D5=010, E5=- 0x10, F5=-010};
+ assert ffi.string(ffi.cast("enum etypes", ord('!'))) == "A5"
+ assert ffi.string(ffi.cast("enum etypes", ord("'"))) == "B5"
+ assert ffi.string(ffi.cast("enum etypes", 16)) == "C5"
+ assert ffi.string(ffi.cast("enum etypes", 8)) == "D5"
+ assert ffi.string(ffi.cast("enum etypes", -16)) == "E5"
+ assert ffi.string(ffi.cast("enum etypes", -8)) == "F5"
def test_array_of_struct(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("struct foo { int a, b; };")
- s = ffi.new("struct foo[1]")
+ s = ffi.new("struct ab[1]")
py.test.raises(AttributeError, 's.b')
py.test.raises(AttributeError, 's.b = 412')
s[0].b = 412
@@ -937,12 +931,10 @@
py.test.raises(IndexError, 's[1]')
def test_pointer_to_array(self):
- ffi = FFI(backend=self.Backend())
p = ffi.new("int(**)[5]")
assert repr(p) == "<cdata 'int(* *)[5]' owning %d bytes>" % SIZE_OF_PTR
def test_iterate_array(self):
- ffi = FFI(backend=self.Backend())
a = ffi.new("char[]", b"hello")
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"]
@@ -953,43 +945,37 @@
py.test.raises(TypeError, list, ffi.new("int *"))
def test_offsetof(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("struct foo { int a, b, c; };")
- assert ffi.offsetof("struct foo", "a") == 0
- assert ffi.offsetof("struct foo", "b") == 4
- assert ffi.offsetof("struct foo", "c") == 8
+ # struct abc { int a, b, c; };
+ assert ffi.offsetof("struct abc", "a") == 0
+ assert ffi.offsetof("struct abc", "b") == 4
+ assert ffi.offsetof("struct abc", "c") == 8
def test_offsetof_nested(self):
- ffi = FFI(backend=self.Backend())
- 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")
- assert ffi.offsetof("struct bar", "e", "a") == 12
- assert ffi.offsetof("struct bar", "e", "b") == 16
- assert ffi.offsetof("struct bar", "e", "c") == 20
+ # struct nesting { struct abc d, e; };
+ assert ffi.offsetof("struct nesting", "e") == 12
+ py.test.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
def test_offsetof_array(self):
- ffi = FFI(backend=self.Backend())
assert ffi.offsetof("int[]", 51) == 51 * ffi.sizeof("int")
assert ffi.offsetof("int *", 51) == 51 * ffi.sizeof("int")
- ffi.cdef("struct bar { int a, b; int c[99]; };")
- assert ffi.offsetof("struct bar", "c") == 2 * ffi.sizeof("int")
- assert ffi.offsetof("struct bar", "c", 0) == 2 * ffi.sizeof("int")
- assert ffi.offsetof("struct bar", "c", 51) == 53 * ffi.sizeof("int")
+ # struct array2 { int a, b; int c[99]; };
+ assert ffi.offsetof("struct array2", "c") == 2 * ffi.sizeof("int")
+ assert ffi.offsetof("struct array2", "c", 0) == 2 * ffi.sizeof("int")
+ assert ffi.offsetof("struct array2", "c", 51) == 53 * ffi.sizeof("int")
def test_alignof(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("struct foo { char a; short b; char c; };")
+ # struct align { char a; short b; char c; };
assert ffi.alignof("int") == 4
assert ffi.alignof("double") in (4, 8)
- assert ffi.alignof("struct foo") == 2
+ assert ffi.alignof("struct align") == 2
def test_bitfield(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("struct foo { int a:10, b:20, c:3; };")
- assert ffi.sizeof("struct foo") == 8
- s = ffi.new("struct foo *")
+ # struct bitfield { int a:10, b:20, c:3; };
+ assert ffi.sizeof("struct bitfield") == 8
+ s = ffi.new("struct bitfield *")
s.a = 511
py.test.raises(OverflowError, "s.a = 512")
py.test.raises(OverflowError, "s[0].a = 512")
@@ -1006,35 +992,31 @@
assert s.c == -4
def test_bitfield_enum(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("""
- typedef enum { AA, BB, CC } foo_e;
- typedef struct { foo_e f:2; } foo_s;
- """)
- s = ffi.new("foo_s *")
+ # typedef enum { AA1, BB1, CC1 } foo_e_t;
+ # typedef struct { foo_e_t f:2; } bfenum_t;
+ s = ffi.new("bfenum_t *")
s.f = 2
assert s.f == 2
def test_anonymous_struct(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("typedef struct { int a; } foo_t;")
- ffi.cdef("typedef struct { char b, c; } bar_t;")
- f = ffi.new("foo_t *", [12345])
- b = ffi.new("bar_t *", [b"B", b"C"])
+ # typedef struct { int a; } anon_foo_t;
+ # typedef struct { char b, c; } anon_bar_t;
+ f = ffi.new("anon_foo_t *", [12345])
+ b = ffi.new("anon_bar_t *", [b"B", b"C"])
assert f.a == 12345
assert b.b == b"B"
assert b.c == b"C"
- assert repr(b).startswith("<cdata 'bar_t *'")
+ assert repr(b).startswith("<cdata 'anon_bar_t *'")
def test_struct_with_two_usages(self):
- for name in ['foo_s', '']: # anonymous or not
- ffi = FFI(backend=self.Backend())
- ffi.cdef("typedef struct %s { int a; } foo_t, *foo_p;" % name)
- f = ffi.new("foo_t *", [12345])
- ps = ffi.new("foo_p[]", [f])
+ # typedef struct named_foo_s { int a; } named_foo_t, *named_foo_p;
+ # typedef struct { int a; } unnamed_foo_t, *unnamed_foo_p;
+ f = ffi.new("named_foo_t *", [12345])
+ ps = ffi.new("named_foo_p[]", [f])
+ f = ffi.new("unnamed_foo_t *", [12345])
+ ps = ffi.new("unnamed_foo_p[]", [f])
def test_pointer_arithmetic(self):
- ffi = FFI(backend=self.Backend())
s = ffi.new("short[]", list(range(100, 110)))
p = ffi.cast("short *", s)
assert p[2] == 102
@@ -1048,7 +1030,6 @@
assert p+1 == s+1
def test_pointer_comparison(self):
- ffi = FFI(backend=self.Backend())
s = ffi.new("short[]", list(range(100)))
p = ffi.cast("short *", s)
assert (p < s) is False
@@ -1099,7 +1080,6 @@
assert (q != None) is True
def test_no_integer_comparison(self):
- ffi = FFI(backend=self.Backend())
x = ffi.cast("int", 123)
y = ffi.cast("int", 456)
py.test.raises(TypeError, "x < y")
@@ -1109,7 +1089,6 @@
py.test.raises(TypeError, "z < y")
def test_ffi_buffer_ptr(self):
- ffi = FFI(backend=self.Backend())
a = ffi.new("short *", 100)
try:
b = ffi.buffer(a)
@@ -1128,7 +1107,6 @@
assert a[0] == 101
def test_ffi_buffer_array(self):
- ffi = FFI(backend=self.Backend())
a = ffi.new("int[]", list(range(100, 110)))
try:
b = ffi.buffer(a)
@@ -1145,7 +1123,6 @@
assert a[1] == 0x45
def test_ffi_buffer_ptr_size(self):
- ffi = FFI(backend=self.Backend())
a = ffi.new("short *", 0x4243)
try:
b = ffi.buffer(a, 1)
@@ -1163,7 +1140,6 @@
assert a[0] == 0x6343
def test_ffi_buffer_array_size(self):
- ffi = FFI(backend=self.Backend())
a1 = ffi.new("int[]", list(range(100, 110)))
a2 = ffi.new("int[]", list(range(100, 115)))
try:
@@ -1173,7 +1149,6 @@
assert ffi.buffer(a1)[:] == ffi.buffer(a2, 4*10)[:]
def test_ffi_buffer_with_file(self):
- ffi = FFI(backend=self.Backend())
import tempfile, os, array
fd, filename = tempfile.mkstemp()
f = os.fdopen(fd, 'r+b')
@@ -1193,7 +1168,6 @@
os.unlink(filename)
def test_ffi_buffer_with_io(self):
- ffi = FFI(backend=self.Backend())
import io, array
f = io.BytesIO()
a = ffi.new("int[]", list(range(1005)))
@@ -1211,18 +1185,16 @@
f.close()
def test_array_in_struct(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("struct foo_s { int len; short data[5]; };")
- p = ffi.new("struct foo_s *")
- p.data[3] = 5
- assert p.data[3] == 5
- assert repr(p.data).startswith("<cdata 'short[5]' 0x")
+ # struct array { int a[2]; char b[3]; };
+ p = ffi.new("struct array *")
+ p.a[1] = 5
+ assert p.a[1] == 5
+ assert repr(p.a).startswith("<cdata 'int[2]' 0x")
def test_struct_containing_array_varsize_workaround(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("struct foo_s { int len; short data[0]; };")
- p = ffi.new("char[]", ffi.sizeof("struct foo_s") + 7 * SIZE_OF_SHORT)
- q = ffi.cast("struct foo_s *", p)
+ # 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)
assert q.len == 0
# 'q.data' gets not a 'short[0]', but just a 'short *' instead
assert repr(q.data).startswith("<cdata 'short *' 0x")
@@ -1232,7 +1204,6 @@
def test_new_struct_containing_array_varsize(self):
py.test.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
assert p.len == 0
@@ -1240,7 +1211,6 @@
py.test.raises(IndexError, "p.data[10]")
def test_ffi_typeof_getcname(self):
- ffi = FFI(backend=self.Backend())
assert ffi.getctype("int") == "int"
assert ffi.getctype("int", 'x') == "int x"
assert ffi.getctype("int*") == "int *"
@@ -1258,7 +1228,6 @@
assert ffi.getctype("int[5]", ' ** foo ') == "int(** foo)[5]"
def test_array_of_func_ptr(self):
- ffi = FFI(backend=self.Backend())
f = ffi.cast("int(*)(int)", 42)
assert f != ffi.NULL
py.test.raises(CDefError, ffi.cast, "int(int)", 42)
@@ -1279,7 +1248,6 @@
def test_callback_as_function_argument(self):
# In C, function arguments can be declared with a function type,
# which is automatically replaced with the ptr-to-function type.
- ffi = FFI(backend=self.Backend())
def cb(a, b):
return chr(ord(a) + ord(b)).encode()
f = ffi.callback("char cb(char, char)", cb)
@@ -1291,7 +1259,6 @@
def test_vararg_callback(self):
py.test.skip("callback with '...'")
- ffi = FFI(backend=self.Backend())
def cb(i, va_list):
j = ffi.va_arg(va_list, "int")
k = ffi.va_arg(va_list, "long long")
@@ -1301,7 +1268,6 @@
assert res == 20 + 300 + 5000
def test_callback_decorator(self):
- ffi = FFI(backend=self.Backend())
#
@ffi.callback("long(long, long)", error=42)
def cb(a, b):
@@ -1312,6 +1278,7 @@
assert cb((1 << (sz*8-1)) - 1, -10) == 42
def test_unique_types(self):
+ xxxx
ffi1 = FFI(backend=self.Backend())
ffi2 = FFI(backend=self.Backend())
assert ffi1.typeof("char") is ffi2.typeof("char ")
@@ -1331,46 +1298,38 @@
assert ffi1.typeof("struct foo*") is ffi1.typeof("struct foo *")
def test_anonymous_enum(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("typedef enum { Value0 = 0 } e, *pe;\n"
- "typedef enum { Value1 = 1 } e1;")
- assert ffi.getctype("e*") == 'e *'
- assert ffi.getctype("pe") == 'e *'
- assert ffi.getctype("e1*") == 'e1 *'
+ # typedef enum { Value0 = 0 } e_t, *pe_t;
+ assert ffi.getctype("e_t*") == 'e_t *'
+ assert ffi.getctype("pe_t") == 'e_t *'
+ assert ffi.getctype("foo_e_t*") == 'foo_e_t *'
def test_new_ctype(self):
- ffi = FFI(backend=self.Backend())
p = ffi.new("int *")
py.test.raises(TypeError, ffi.new, p)
p = ffi.new(ffi.typeof("int *"), 42)
assert p[0] == 42
def test_enum_with_non_injective_mapping(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("enum e { AA=0, BB=0, CC=0, DD=0 };")
- e = ffi.cast("enum e", 0)
- assert ffi.string(e) == "AA" # pick the first one arbitrarily
+ # enum e_noninj { AA3=0, BB3=0, CC3=0, DD3=0 };
+ e = ffi.cast("enum e_noninj", 0)
+ assert ffi.string(e) == "AA3" # pick the first one arbitrarily
def test_enum_refer_previous_enum_value(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("enum e { AA, BB=2, CC=4, DD=BB, EE, FF=CC, GG=FF };")
- assert ffi.string(ffi.cast("enum e", 2)) == "BB"
- assert ffi.string(ffi.cast("enum e", 3)) == "EE"
- assert ffi.sizeof("char[DD]") == 2
- assert ffi.sizeof("char[EE]") == 3
- assert ffi.sizeof("char[FF]") == 4
- assert ffi.sizeof("char[GG]") == 4
+ # enum e_prev { AA4, BB4=2, CC4=4, DD4=BB4, EE4, FF4=CC4, GG4=FF4 };
+ assert ffi.string(ffi.cast("enum e_prev", 2)) == "BB4"
+ assert ffi.string(ffi.cast("enum e_prev", 3)) == "EE4"
+ assert ffi.sizeof("char[DD4]") == 2
+ assert ffi.sizeof("char[EE4]") == 3
+ assert ffi.sizeof("char[FF4]") == 4
+ assert ffi.sizeof("char[GG4]") == 4
def test_nested_anonymous_struct(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("""
- struct foo_s {
- struct { int a, b; };
- union { int c, d; };
- };
- """)
- assert ffi.sizeof("struct foo_s") == 3 * SIZE_OF_INT
- p = ffi.new("struct foo_s *", [1, 2, 3])
+ # struct nested_anon {
+ # struct { int a, b; };
+ # union { int c, d; };
+ # };
+ assert ffi.sizeof("struct nested_anon") == 3 * SIZE_OF_INT
+ p = ffi.new("struct nested_anon *", [1, 2, 3])
assert p.a == 1
assert p.b == 2
assert p.c == 3
@@ -1382,22 +1341,19 @@
assert p.b == 19
assert p.c == 17
assert p.d == 17
- p = ffi.new("struct foo_s *", {'b': 12, 'd': 14})
+ p = ffi.new("struct nested_anon *", {'b': 12, 'd': 14})
assert p.a == 0
assert p.b == 12
assert p.c == 14
assert p.d == 14
def test_nested_anonymous_union(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("""
- union foo_u {
- struct { int a, b; };
- union { int c, d; };
- };
- """)
- assert ffi.sizeof("union foo_u") == 2 * SIZE_OF_INT
- p = ffi.new("union foo_u *", [5])
+ # union nested_anon_u {
+ # struct { int a, b; };
+ # union { int c, d; };
+ # };
+ assert ffi.sizeof("union nested_anon_u") == 2 * SIZE_OF_INT
+ p = ffi.new("union nested_anon_u *", [5])
assert p.a == 5
assert p.b == 0
assert p.c == 5
@@ -1410,12 +1366,12 @@
assert p.b == 19
assert p.c == 17
assert p.d == 17
- p = ffi.new("union foo_u *", {'d': 14})
+ p = ffi.new("union nested_anon_u *", {'d': 14})
assert p.a == 14
assert p.b == 0
assert p.c == 14
assert p.d == 14
- p = ffi.new("union foo_u *", {'b': 12})
+ p = ffi.new("union nested_anon_u *", {'b': 12})
assert p.a == 0
assert p.b == 12
assert p.c == 0
@@ -1425,14 +1381,12 @@
# to give both 'a' and 'b'
def test_cast_to_array_type(self):
- ffi = FFI(backend=self.Backend())
p = ffi.new("int[4]", [-5])
q = ffi.cast("int[3]", p)
assert q[0] == -5
assert repr(q).startswith("<cdata 'int[3]' 0x")
def test_gc(self):
- ffi = FFI(backend=self.Backend())
p = ffi.new("int *", 123)
seen = []
def destructor(p1):
@@ -1447,7 +1401,6 @@
assert seen == [1]
def test_CData_CType(self):
- ffi = FFI(backend=self.Backend())
assert isinstance(ffi.cast("int", 0), ffi.CData)
assert isinstance(ffi.new("int *"), ffi.CData)
assert not isinstance(ffi.typeof("int"), ffi.CData)
@@ -1455,11 +1408,9 @@
assert not isinstance(ffi.new("int *"), ffi.CType)
def test_CData_CType_2(self):
- ffi = FFI(backend=self.Backend())
assert isinstance(ffi.typeof("int"), ffi.CType)
def test_bool(self):
- ffi = FFI(backend=self.Backend())
assert int(ffi.cast("_Bool", 0.1)) == 1
assert int(ffi.cast("_Bool", -0.0)) == 0
assert int(ffi.cast("_Bool", b'\x02')) == 1
@@ -1470,82 +1421,40 @@
py.test.raises(OverflowError, ffi.new, "_Bool *", 2)
py.test.raises(TypeError, ffi.string, ffi.cast("_Bool", 2))
- def test_use_own_bool(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("""typedef int bool;""")
-
- def test_ordering_bug1(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("""
- struct foo_s {
- struct bar_s *p;
- };
- struct bar_s {
- struct foo_s foo;
- };
- """)
- q = ffi.new("struct foo_s *")
- bar = ffi.new("struct bar_s *")
- q.p = bar
- assert q.p.foo.p == ffi.NULL
-
- def test_ordering_bug2(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("""
- struct bar_s;
-
- struct foo_s {
- void (*foo)(struct bar_s[]);
- };
-
- struct bar_s {
- struct foo_s foo;
- };
- """)
- q = ffi.new("struct foo_s *")
-
def test_addressof(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("struct foo_s { int x, y; };")
- p = ffi.new("struct foo_s *")
+ p = ffi.new("struct ab *")
a = ffi.addressof(p[0])
- assert repr(a).startswith("<cdata 'struct foo_s *' 0x")
+ 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))
def test_addressof_field(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("struct foo_s { int x, y; };")
- p = ffi.new("struct foo_s *")
- a = ffi.addressof(p[0], 'y')
- assert repr(a).startswith("<cdata 'int *' 0x")
- assert int(ffi.cast("uintptr_t", a)) == (
+ p = ffi.new("struct ab *")
+ b = ffi.addressof(p[0], 'b')
+ assert repr(b).startswith("<cdata 'int *' 0x")
+ assert int(ffi.cast("uintptr_t", b)) == (
int(ffi.cast("uintptr_t", p)) + ffi.sizeof("int"))
- assert a == ffi.addressof(p, 'y')
- assert a != ffi.addressof(p, 'x')
+ assert b == ffi.addressof(p, 'b')
+ assert b != ffi.addressof(p, 'a')
def test_addressof_field_nested(self):
- ffi = FFI(backend=self.Backend())
- 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')
- a = ffi.addressof(p[0], 'b', 'y')
+ # struct nesting { struct abc d, e; };
+ p = ffi.new("struct nesting *")
+ py.test.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)) +
- ffi.sizeof("struct foo_s") + ffi.sizeof("int"))
+ ffi.sizeof("struct abc") + ffi.sizeof("int"))
def test_addressof_anonymous_struct(self):
- ffi = FFI()
- ffi.cdef("typedef struct { int x; } foo_t;")
- p = ffi.new("foo_t *")
+ # typedef struct { int a; } anon_foo_t;
+ p = ffi.new("anon_foo_t *")
a = ffi.addressof(p[0])
assert a == p
def test_addressof_array(self):
- ffi = FFI()
p = ffi.new("int[52]")
p0 = ffi.addressof(p)
assert p0 == p
@@ -1558,7 +1467,6 @@
assert ffi.addressof(p, 0) == p
def test_addressof_pointer(self):
- ffi = FFI()
array = ffi.new("int[50]")
p = ffi.cast("int *", array)
py.test.raises(TypeError, ffi.addressof, p)
@@ -1566,8 +1474,7 @@
assert ffi.addressof(p, 25) == p + 25
assert ffi.typeof(ffi.addressof(p, 25)) == ffi.typeof(p)
#
- ffi.cdef("struct foo { int a, b; };")
- array = ffi.new("struct foo[50]")
+ array = ffi.new("struct ab[50]")
p = ffi.cast("int *", array)
py.test.raises(TypeError, ffi.addressof, p)
assert ffi.addressof(p, 0) == p
@@ -1575,28 +1482,33 @@
assert ffi.typeof(ffi.addressof(p, 25)) == ffi.typeof(p)
def test_addressof_array_in_struct(self):
- ffi = FFI()
- ffi.cdef("struct foo { int a, b; int c[50]; };")
- p = ffi.new("struct foo *")
+ # struct abc50 { int a, b; int c[50]; };
+ p = ffi.new("struct abc50 *")
p1 = ffi.addressof(p, "c", 25)
assert ffi.typeof(p1) is ffi.typeof("int *")
assert p1 == ffi.cast("int *", p) + 27
assert ffi.addressof(p, "c") == ffi.cast("int *", p) + 2
assert ffi.addressof(p, "c", 0) == ffi.cast("int *", p) + 2
p2 = ffi.addressof(p, 1)
- assert ffi.typeof(p2) is ffi.typeof("struct foo *")
+ assert ffi.typeof(p2) is ffi.typeof("struct abc50 *")
assert p2 == p + 1
def test_multiple_independent_structs(self):
- ffi1 = FFI(); ffi1.cdef("struct foo { int x; };")
- ffi2 = FFI(); ffi2.cdef("struct foo { int y, z; };")
- foo1 = ffi1.new("struct foo *", [10])
- foo2 = ffi2.new("struct foo *", [20, 30])
+ CDEF2 = "struct ab { int x; };"
+ ffi2 = cffi.FFI(); ffi2.cdef(CDEF2)
+ outputfilename = recompile(ffi2, "test_multiple_independent_structs",
+ CDEF2, tmpdir=str(udir))
+ module = imp.load_dynamic("test_multiple_independent_structs",
+ outputfilename)
+ ffi1 = module.ffi
+ foo1 = ffi1.new("struct ab *", [10])
+ foo2 = ffi .new("struct ab *", [20, 30])
assert foo1.x == 10
- assert foo2.y == 20
- assert foo2.z == 30
+ assert foo2.a == 20
+ assert foo2.b == 30
def test_missing_include(self):
+ py.test.xfail("ffi.include")
backend = self.Backend()
ffi1 = FFI(backend=backend)
ffi2 = FFI(backend=backend)
@@ -1604,6 +1516,7 @@
py.test.raises(CDefError, ffi2.cast, "schar_t", 142)
def test_include_typedef(self):
+ py.test.xfail("ffi.include")
backend = self.Backend()
ffi1 = FFI(backend=backend)
ffi2 = FFI(backend=backend)
@@ -1613,6 +1526,7 @@
assert int(p) == 142 - 256
def test_include_struct(self):
+ py.test.xfail("ffi.include")
backend = self.Backend()
ffi1 = FFI(backend=backend)
ffi2 = FFI(backend=backend)
@@ -1622,6 +1536,7 @@
assert p.x == 142
def test_include_union(self):
+ py.test.xfail("ffi.include")
backend = self.Backend()
ffi1 = FFI(backend=backend)
ffi2 = FFI(backend=backend)
@@ -1631,6 +1546,7 @@
assert p.x == 142
def test_include_enum(self):
+ py.test.xfail("ffi.include")
backend = self.Backend()
ffi1 = FFI(backend=backend)
ffi2 = FFI(backend=backend)
@@ -1641,6 +1557,7 @@
assert ffi2.sizeof("char[FC]") == 2
def test_include_typedef_2(self):
+ py.test.xfail("ffi.include")
backend = self.Backend()
ffi1 = FFI(backend=backend)
ffi2 = FFI(backend=backend)
@@ -1649,16 +1566,9 @@
p = ffi2.new("foo_p", [142])
assert p.x == 142
- def test_ignore_multiple_declarations_of_constant(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("#define FOO 42")
- ffi.cdef("#define FOO 42")
- py.test.raises(FFIError, ffi.cdef, "#define FOO 43")
-
def test_struct_packed(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("struct nonpacked { char a; int b; };")
- ffi.cdef("struct is_packed { char a; int b; };", packed=True)
+ # struct nonpacked { char a; int b; };
+ # struct is_packed { char a; int b; } __attribute__((packed));
assert ffi.sizeof("struct nonpacked") == 8
assert ffi.sizeof("struct is_packed") == 5
assert ffi.alignof("struct nonpacked") == 4
@@ -1672,25 +1582,3 @@
assert s[0].a == b'X'
assert s[1].b == -4892220
assert s[1].a == b'Y'
-
- def test_define_integer_constant(self):
- ffi = FFI(backend=self.Backend())
- ffi.cdef("""
- #define DOT_0 0
- #define DOT 100
- #define DOT_OCT 0100l
- #define DOT_HEX 0x100u
- #define DOT_HEX2 0X10
- #define DOT_UL 1000UL
- enum foo {AA, BB=DOT, CC};
- """)
- lib = ffi.dlopen(None)
- assert ffi.string(ffi.cast("enum foo", 100)) == "BB"
- assert lib.DOT_0 == 0
- assert lib.DOT == 100
- assert lib.DOT_OCT == 0o100
- assert lib.DOT_HEX == 0x100
- assert lib.DOT_HEX2 == 0x10
- assert lib.DOT_UL == 1000
-
-
More information about the pypy-commit
mailing list