Author: Jeremy Thurgood <fir...@gmail.com>
Branch: split-verify
Changeset: r1357:9f5e04349245
Date: 2013-10-06 15:36 +0200
http://bitbucket.org/cffi/cffi/changeset/9f5e04349245/

Log:    Fix (and test) ffi proxy functions.

diff --git a/cffi/builder.py b/cffi/builder.py
--- a/cffi/builder.py
+++ b/cffi/builder.py
@@ -13,85 +13,34 @@
 _ffi = FFI()
 
 
-### The functions below are proxies for `_ffi` to make things more convenient.
+### Proxy `_ffi` methods to make things more convenient.
 
 
-@wraps(_ffi.typeof)
-def typeof(cdecl):
-    return _ffi.typeof(cdecl)
+typeof = _ffi.typeof
+sizeof = _ffi.sizeof
+alignof = _ffi.alignof
+offsetof = _ffi.offsetof
+new = _ffi.new
+cast = _ffi.cast
+string = _ffi.string
+buffer = _ffi.buffer
+callback = _ffi.callback
+getctype = _ffi.getctype
+gc = _ffi.gc
 
 
-@wraps(_ffi.sizeof)
-def sizeof(cdecl):
-    return _ffi.sizeof(cdecl)
+# Can't have properties on modules. :-(
 
+def get_errno():
+    return _ffi.errno
 
-@wraps(_ffi.alignof)
-def alignof(cdecl):
-    return _ffi.alignof(cdecl)
+def set_errno(errno):
+    _ffi.errno = errno
 
 
-@wraps(_ffi.offsetof)
-def offsetof(cdecl):
-    return _ffi.offsetof(cdecl)
-
-
-@wraps(_ffi.new)
-def new(cdecl, init=None):
-    return _ffi.new(cdecl, init=init)
-
-
-@wraps(_ffi.cast)
-def cast(cdecl, source):
-    return _ffi.cast(cdecl, source)
-
-
-@wraps(_ffi.string)
-def string(cdecl, maxlen=-1):
-    return _ffi.string(cdecl, maxlen=-1)
-
-
-@wraps(_ffi.buffer)
-def buffer(cdecl, maxlen=-1):
-    return _ffi.buffer(cdecl, maxlen=-1)
-
-
-@wraps(_ffi.callback)
-def callback(cdecl, python_callable=None, error=None):
-    return _ffi.callback(cdecl, python_callable=python_callable, error=error)
-
-
-@wraps(_ffi.getctype)
-def getctype(cdecl, replace_with=''):
-    return _ffi.getctype(cdecl, replace_with=replace_with)
-
-
-@wraps(_ffi.gc)
-def gc(cdata, destructor):
-    return _ffi.gc(cdata, destructor)
-
-
-def _get_errno():
-    return _ffi.errno
-def _set_errno(errno):
-    _ffi.errno = errno
-errno = property(_get_errno, _set_errno, None,
-                 "the value of 'errno' from/to the C calls")
-
-
-@wraps(_ffi.addressof)
-def addressof(cdata, field=None):
-    return _ffi.addressof(cdata, field=field)
-
-
-@wraps(_ffi.new_handle)
-def new_handle(x):
-    return _ffi.new_handle(x)
-
-
-@wraps(_ffi.from_handle)
-def from_handle(x):
-    return _ffi.from_handle(x)
+addressof = _ffi.addressof
+new_handle = _ffi.new_handle
+from_handle = _ffi.from_handle
 
 
 ### The functions below are generated by cffi.
diff --git a/testing/test_makelib.py b/testing/test_makelib.py
--- a/testing/test_makelib.py
+++ b/testing/test_makelib.py
@@ -80,3 +80,73 @@
     assert lib_foo.sin(12.3) == math.sin(12.3)
     lib_bar = foo_ffi.load_bar()
     assert lib_bar.sin(12.3) == math.sin(12.3)
+
+
+def test_ffi_module_functions(tmpdir):
+    builder = FFIBuilder("foo_ffi", str(tmpdir))
+    builder.cdef("""
+        double sin(double x);
+    """)
+    builder.makelib('foo', '#include <math.h>')
+    builder.write_ffi_module()
+
+    sys.path.append(str(tmpdir))
+    try:
+        import foo_ffi
+    finally:
+        _clean_modules(tmpdir, 'foo_ffi')
+
+    assert foo_ffi.typeof == foo_ffi._ffi.typeof
+    assert foo_ffi.sizeof == foo_ffi._ffi.sizeof
+    assert foo_ffi.alignof == foo_ffi._ffi.alignof
+    assert foo_ffi.offsetof == foo_ffi._ffi.offsetof
+    assert foo_ffi.new == foo_ffi._ffi.new
+    assert foo_ffi.cast == foo_ffi._ffi.cast
+    assert foo_ffi.string == foo_ffi._ffi.string
+    assert foo_ffi.buffer == foo_ffi._ffi.buffer
+    assert foo_ffi.callback == foo_ffi._ffi.callback
+    assert foo_ffi.getctype == foo_ffi._ffi.getctype
+    assert foo_ffi.gc == foo_ffi._ffi.gc
+
+    foo_ffi.set_errno(7)
+    assert foo_ffi.get_errno() == 7
+
+    assert foo_ffi.addressof == foo_ffi._ffi.addressof
+    assert foo_ffi.new_handle == foo_ffi._ffi.new_handle
+    assert foo_ffi.from_handle == foo_ffi._ffi.from_handle
+
+
+def test_ffi_do_some_stuff(tmpdir):
+    builder = FFIBuilder("foo_ffi", str(tmpdir))
+    builder.cdef("""
+        struct foo_s { int x; int y; };
+        int grid_distance(struct foo_s offset);
+    """)
+    builder.makelib('foo', """
+        struct foo_s { int x; int y; };
+        int grid_distance(struct foo_s offset) {
+            return offset.x + offset.y;
+        }
+    """)
+    builder.write_ffi_module()
+
+    sys.path.append(str(tmpdir))
+    try:
+        import foo_ffi
+    finally:
+        _clean_modules(tmpdir, 'foo_ffi')
+
+    my_struct = foo_ffi.new('struct foo_s *', {'x': 1, 'y': 2})
+    assert foo_ffi.typeof(my_struct) == foo_ffi.typeof("struct foo_s *")
+    assert foo_ffi.sizeof('struct foo_s') == 2 * foo_ffi.sizeof('int')
+    assert foo_ffi.alignof('struct foo_s') == foo_ffi.sizeof('int')
+    assert foo_ffi.typeof(foo_ffi.cast('long', 42)) == foo_ffi.typeof('long')
+    assert foo_ffi.string(foo_ffi.new('char *', b"\x00")) == b""
+
+    def cb(n):
+        return n + 1
+    f = foo_ffi.callback("int(*)(int)", cb)
+    assert f(1) == 2
+
+    lib = foo_ffi.load_foo()
+    assert lib.grid_distance(my_struct[0]) == 3
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to