[pypy-commit] cffi default: Test and fix
arigo
noreply at buildbot.pypy.org
Tue Jun 26 18:47:50 CEST 2012
Author: Armin Rigo <arigo at tunes.org>
Branch:
Changeset: r531:561037ef6db0
Date: 2012-06-26 18:47 +0200
http://bitbucket.org/cffi/cffi/changeset/561037ef6db0/
Log: Test and fix
diff --git a/c/_ffi_backend.c b/c/_ffi_backend.c
--- a/c/_ffi_backend.c
+++ b/c/_ffi_backend.c
@@ -757,12 +757,21 @@
if (!CData_Check(init))
goto cannot_convert;
ctinit = ((CDataObject *)init)->c_type;
- if (!(ctinit->ct_flags & (CT_POINTER|CT_FUNCTIONPTR|CT_ARRAY)))
- goto cannot_convert;
- if (ctinit->ct_itemdescr != ct->ct_itemdescr &&
- !(ct->ct_itemdescr->ct_flags & CT_CAST_ANYTHING) &&
- !(ctinit->ct_itemdescr->ct_flags & CT_CAST_ANYTHING))
- goto cannot_convert;
+ if (!(ctinit->ct_flags & (CT_POINTER|CT_FUNCTIONPTR))) {
+ if (ctinit->ct_flags & CT_ARRAY)
+ ctinit = (CTypeDescrObject *)ctinit->ct_stuff;
+ else
+ goto cannot_convert;
+ }
+ if (ctinit != ct) {
+ if (((ct->ct_flags & CT_POINTER) &&
+ (ct->ct_itemdescr->ct_flags & CT_CAST_ANYTHING)) ||
+ ((ctinit->ct_flags & CT_POINTER) &&
+ (ctinit->ct_itemdescr->ct_flags & CT_CAST_ANYTHING)))
+ ; /* accept void* or char* as either source or target */
+ else
+ goto cannot_convert;
+ }
ptrdata = ((CDataObject *)init)->c_data;
*(char **)data = ptrdata;
diff --git a/c/test_c.py b/c/test_c.py
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -603,12 +603,13 @@
def test_struct_init_list():
BVoidP = new_pointer_type(new_void_type())
BInt = new_primitive_type("int")
+ BIntPtr = new_pointer_type(BInt)
BStruct = new_struct_type("foo")
BStructPtr = new_pointer_type(BStruct)
complete_struct_or_union(BStruct, [('a1', BInt, -1),
('a2', BInt, -1),
('a3', BInt, -1),
- ('p4', new_pointer_type(BInt), -1)])
+ ('p4', BIntPtr, -1)])
s = newp(BStructPtr, [123, 456])
assert s.a1 == 123
assert s.a2 == 456
@@ -623,7 +624,7 @@
#
py.test.raises(KeyError, newp, BStructPtr, {'foobar': 0})
#
- p = newp(new_pointer_type(BInt), 14141)
+ p = newp(BIntPtr, 14141)
s = newp(BStructPtr, [12, 34, 56, p])
assert s.p4 == p
#
@@ -1130,3 +1131,16 @@
py.test.raises(NotImplementedError, new_function_type, (),
new_union_type("foo_u"))
py.test.raises(TypeError, new_function_type, (), BArray)
+
+def test_cast_with_functionptr():
+ BFunc = new_function_type((), new_void_type())
+ BFunc2 = new_function_type((), new_primitive_type("short"))
+ BCharP = new_pointer_type(new_primitive_type("char"))
+ BIntP = new_pointer_type(new_primitive_type("int"))
+ BStruct = new_struct_type("foo")
+ BStructPtr = new_pointer_type(BStruct)
+ complete_struct_or_union(BStruct, [('a1', BFunc, -1)])
+ newp(BStructPtr, [cast(BFunc, 0)])
+ newp(BStructPtr, [cast(BCharP, 0)])
+ py.test.raises(TypeError, newp, BStructPtr, [cast(BIntP, 0)])
+ py.test.raises(TypeError, newp, BStructPtr, [cast(BFunc2, 0)])
More information about the pypy-commit
mailing list