Author: Armin Rigo <ar...@tunes.org> Branch: Changeset: r564:a506f1856eb6 Date: 2012-06-29 10:49 +0200 http://bitbucket.org/cffi/cffi/changeset/a506f1856eb6/
Log: Test and implementation (again) for verify(). diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c --- a/c/_cffi_backend.c +++ b/c/_cffi_backend.c @@ -3694,6 +3694,7 @@ _cffi_from_c_char, convert_to_object, convert_from_object, + convert_struct_to_owning_object, }; /************************************************************/ diff --git a/cffi/verifier.py b/cffi/verifier.py --- a/cffi/verifier.py +++ b/cffi/verifier.py @@ -184,6 +184,9 @@ elif isinstance(tp, model.ArrayType): return '_cffi_from_c_deref((char *)%s, _cffi_type(%d))' % ( var, self.gettypenum(tp)) + elif isinstance(tp, model.StructType): + return '_cffi_from_c_struct((char *)&%s, _cffi_type(%d))' % ( + var, self.gettypenum(tp)) else: raise NotImplementedError(tp) @@ -614,7 +617,9 @@ ((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[16]) #define _cffi_to_c \ ((int(*)(char *, CTypeDescrObject *, PyObject *))_cffi_exports[17]) -#define _CFFI_NUM_EXPORTS 18 +#define _cffi_from_c_struct \ + ((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[18]) +#define _CFFI_NUM_EXPORTS 19 #if SIZEOF_LONG < SIZEOF_LONG_LONG # define _cffi_to_c_long_long PyLong_AsLongLong diff --git a/testing/test_verify.py b/testing/test_verify.py --- a/testing/test_verify.py +++ b/testing/test_verify.py @@ -576,3 +576,23 @@ """) msg = 'cannot pass as a argument a struct that was completed with verify()' assert msg in str(e.value) + +def test_func_returns_struct(): + ffi = FFI() + ffi.cdef(""" + struct foo_s { int aa, bb; }; + struct foo_s foo(int a, int b); + """) + lib = ffi.verify(""" + struct foo_s { int aa, bb; }; + struct foo_s foo(int a, int b) { + struct foo_s r; + r.aa = a*a; + r.bb = b*b; + return r; + } + """) + s = lib.foo(6, 7) + assert repr(s) == "<cdata 'struct foo_s' owning 8 bytes>" + assert s.aa == 36 + assert s.bb == 49 _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit