Author: Armin Rigo <[email protected]>
Branch:
Changeset: r209:77b498c94a46
Date: 2014-12-19 16:58 +0100
http://bitbucket.org/cffi/creflect/changeset/77b498c94a46/
Log: more tests
diff --git a/zeffir/builder.c b/zeffir/builder.c
--- a/zeffir/builder.c
+++ b/zeffir/builder.c
@@ -956,7 +956,7 @@
_crx_qual_type result;
const char *err = creflect_decl_parser(&builder.cb, str, &result);
if (PyErr_Occurred()) {
- if (PyErr_ExceptionMatches(ZefError)) {
+ if (PyErr_ExceptionMatches(ZefError) && err != NULL) {
PyObject *exc, *val, *tb, *ps;
PyErr_Fetch(&exc, &val, &tb);
ps = PyObject_Str(val);
diff --git a/zeffir/test/test_c.py b/zeffir/test/test_c.py
--- a/zeffir/test/test_c.py
+++ b/zeffir/test/test_c.py
@@ -295,3 +295,124 @@
assert ffi.new(pp, max)[0] == max
py.test.raises(OverflowError, ffi.new, pp, -1)
py.test.raises(OverflowError, ffi.new, pp, max + 1)
+
+def test_reading_pointer_to_char():
+ ffi = support.new_ffi()
+ p = ffi.new("char *", None)
+ assert p[0] == b'\x00'
+ p = ffi.new("char *", b'A')
+ assert p[0] == b'A'
+ py.test.raises(TypeError, ffi.new, "char *", 65)
+ py.test.raises(TypeError, ffi.new, "char *", b"foo")
+ py.test.raises(TypeError, ffi.new, "char *", u+"foo")
+ c = ffi.new("char", b'A')
+ assert str(c) == repr(c)
+ assert int(c) == ord(b'A')
+ py.test.raises(TypeError, ffi.cast, "char", b'foo')
+ py.test.raises(TypeError, ffi.cast, "char", u+'foo')
+ py.test.raises(TypeError, ffi.new, "char", b'foo')
+ py.test.raises(TypeError, ffi.new, "char", u+'foo')
+
+def test_reading_pointer_to_pointer():
+ ffi = support.new_ffi()
+ q = ffi.new("int *", 42)
+ assert q[0] == 42
+ p = ffi.new("int * *")
+ assert p[0] is not None
+ assert p[0] == ffi.cast("void *", 0)
+ assert p[0] == ffi.cast("char *", 0)
+ assert p[0] != None
+ assert repr(p[0]) == "<cdata 'int *' NULL>"
+ p[0] = q
+ assert p[0] != ffi.cast("void *", 0)
+ assert p[0] != ffi.cast("char *", 0)
+ assert p[0][0] == 42
+ q[0] += 1
+ assert p[0][0] == 43
+ p = ffi.new("int * *", q)
+ assert p[0][0] == 43
+
+def test_hash_differences():
+ ffi = support.new_ffi()
+ for i in range(1, 50):
+ x1 = ffi.new("char", chr(i))
+ x2 = ffi.new("int", i)
+ if hash(x1) != hash(x2):
+ break
+ else:
+ raise AssertionError("hashes are equal")
+ for i in range(1, 50):
+ if hash(ffi.new("float", i)) != hash(float(i)):
+ break
+ else:
+ raise AssertionError("hashes are equal")
+
+def test_no_len_on_nonarray():
+ ffi = support.new_ffi()
+ py.test.raises(TypeError, len, ffi.new("int", 42))
+
+def test_cmp_none():
+ ffi = support.new_ffi()
+ x = ffi.new("int", 42)
+ assert (x == None) is False
+ assert (x != None) is True
+ assert (x == ["hello"]) is False
+ assert (x != ["hello"]) is True
+ y = ffi.new("int", 0)
+ assert (y == None) is False
+
+def test_invalid_indexing():
+ ffi = support.new_ffi()
+ x = ffi.new("int", 42)
+ py.test.raises(TypeError, "x[0]")
+
+def test_default_str():
+ ffi = support.new_ffi()
+ x = ffi.new("char", 'A')
+ assert str(x) == repr(x)
+ x = ffi.new("int", 42)
+ assert str(x) == repr(x)
+ x = ffi.new("int[10]")
+ assert str(x) == repr(x)
+
+def test_cast_from_cdataint():
+ ffi = support.new_ffi()
+ x = ffi.new("int", 0)
+ y = ffi.cast("int *", x)
+ assert bool(y) is False
+ #
+ x = ffi.new("int", 42)
+ y = ffi.new("int", x)
+ assert int(x) == int(y) == 42 == ffi.cast("int", y)
+ x = ffi.new("char", '\xFE')
+ y = ffi.new("char", x)
+ assert int(x) == int(y) == 254 == ffi.cast("int", y)
+ x = ffi.new("double", 42.42)
+ y = ffi.new("double", x)
+ assert float(x) == float(y) == 42.42 == ffi.cast("double", y)
+
+def test_void_type():
+ ffi = support.new_ffi()
+ p = ffi.typeof("void")
+ assert p.kind == "void"
+ assert p.cname == "void"
+ check_dir(p, ['kind', 'cname'])
+
+def test_array_type():
+ ffi = support.new_ffi()
+ p = ffi.typeof("int")
+ assert repr(p) == "<ctype 'int'>"
+ #
+ py.test.raises(ffi.error, ffi.typeof, "int[-42]")
+ #
+ p1 = ffi.typeof("int[]")
+ assert repr(p1) == "<ctype 'int[]'>"
+ py.test.raises(ffi.error, ffi.typeof, "int[][]")
+ e = py.test.raises(ffi.error, ffi.typeof, "int[42][]")
+ assert str(e.value) == "array type has incomplete element type 'int[]'"
+ #
+ p2 = ffi.typeof(" int [ 25 ] [ 42 ] ")
+ assert repr(p2) == "<ctype 'int[25][42]'>"
+ #
+ py.test.raises(OverflowError, ffi.typeof, "int[%d]" % (sys.maxsize + 1))
+ py.test.raises(OverflowError, ffi.typeof, "int[%d]" % (sys.maxsize // 3))
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit