[pypy-commit] cffi cffi-1.0: Found out a "cleaner" way to hack for verify()

arigo noreply at buildbot.pypy.org
Fri Apr 17 17:13:54 CEST 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-1.0
Changeset: r1742:84244c3a7537
Date: 2015-04-17 17:14 +0200
http://bitbucket.org/cffi/cffi/changeset/84244c3a7537/

Log:	Found out a "cleaner" way to hack for verify()

diff --git a/new/ffi_obj.c b/new/ffi_obj.c
--- a/new/ffi_obj.c
+++ b/new/ffi_obj.c
@@ -536,40 +536,7 @@
 }
 #endif
 
-static PyObject *ffi__verified(FFIObject *self, PyObject *args)
-{
-    FFIObject *srcffi;
-
-    if (!PyArg_ParseTuple(args, "O!:_verified", &FFI_Type, &srcffi))
-        return NULL;
-
-    if (!srcffi->ctx_is_static)
-        goto invalid;
-
-    if (self->ctx_is_static)
-        goto invalid;
-
-    size_t i;
-    const char *p = (const char *)self->info.ctx;
-    for (i = 0; i < sizeof(struct _cffi_type_context_s); i++) {
-        if (*p++ != '\0')
-            goto invalid;
-    }
-
-    PyMem_Free((void *)self->info.ctx);
-    self->ctx_is_static = 1;
-    self->info.ctx = srcffi->info.ctx;
-
-    Py_INCREF(Py_None);
-    return Py_None;
-
- invalid:
-    PyErr_SetString(PyExc_ValueError, "XXX invalid source or destination");
-    return NULL;
-}
-
 static PyMethodDef ffi_methods[] = {
-    {"_verified",     (PyCFunction)ffi__verified, METH_VARARGS},
 #if 0
     {"addressof",     (PyCFunction)ffi_addressof, METH_VARARGS},
     {"cast",          (PyCFunction)ffi_cast,      METH_VARARGS},
diff --git a/new/recompiler.py b/new/recompiler.py
--- a/new/recompiler.py
+++ b/new/recompiler.py
@@ -558,15 +558,18 @@
     outputfilename = ffiplatform.compile(tmpdir, ext)
     return outputfilename
 
-def verify2(ffi, module_name, preamble, *args, **kwds):
+def verify(ffi, module_name, preamble, *args, **kwds):
     import imp
     assert module_name not in sys.modules, "module name conflict: %r" % (
         module_name,)
     outputfilename = recompile(ffi, module_name, preamble, *args, **kwds)
     module = imp.load_dynamic(module_name, outputfilename)
-    return module.ffi, module.lib
-
-def verify(ffi, module_name, preamble, *args, **kwds):
-    ffi2, lib = verify2(ffi, module_name, preamble, *args, **kwds)
-    ffi._verified(ffi2)
-    return lib
+    #
+    # hack hack hack: copy all *bound methods* from module.ffi back to the
+    # ffi instance.  Then calls like ffi.new() will invoke module.ffi.new().
+    for name in dir(module.ffi):
+        if not name.startswith('_'):
+            attr = getattr(module.ffi, name)
+            if attr is not getattr(ffi, name):
+                setattr(ffi, name, attr)
+    return module.lib
diff --git a/new/test_recompiler.py b/new/test_recompiler.py
--- a/new/test_recompiler.py
+++ b/new/test_recompiler.py
@@ -1,5 +1,5 @@
 import py
-from recompiler import Recompiler, verify, verify2
+from recompiler import Recompiler, verify
 from cffi1 import FFI
 
 
@@ -97,8 +97,8 @@
 def test_verify_typedef():
     ffi = FFI()
     ffi.cdef("typedef int **foo_t;")
-    ffi2, lib = verify2(ffi, 'test_verify_typedef', 'typedef int **foo_t;')
-    assert ffi2.sizeof("foo_t") == ffi.sizeof("void *")
+    lib = verify(ffi, 'test_verify_typedef', 'typedef int **foo_t;')
+    assert ffi.sizeof("foo_t") == ffi.sizeof("void *")
 
 def test_global_var_int():
     ffi = FFI()
@@ -160,20 +160,20 @@
 def test_verify_opaque_struct():
     ffi = FFI()
     ffi.cdef("struct foo_s;")
-    ffi, lib = verify2(ffi, 'test_verify_opaque_struct', "struct foo_s;")
+    lib = verify(ffi, 'test_verify_opaque_struct', "struct foo_s;")
     assert ffi.typeof("struct foo_s").cname == "struct foo_s"
 
 def test_verify_opaque_union():
     ffi = FFI()
     ffi.cdef("union foo_s;")
-    ffi, lib = verify2(ffi, 'test_verify_opaque_union', "union foo_s;")
+    lib = verify(ffi, 'test_verify_opaque_union', "union foo_s;")
     assert ffi.typeof("union foo_s").cname == "union foo_s"
 
 def test_verify_struct():
     ffi = FFI()
     ffi.cdef("""struct foo_s { int b; short a; };
                 struct bar_s { struct foo_s *f; };""")
-    ffi, lib = verify2(ffi, 'test_verify_struct',
+    lib = verify(ffi, 'test_verify_struct',
                  """struct foo_s { short a; int b; };
                     struct bar_s { struct foo_s *f; };""")
     ffi.typeof("struct bar_s *")
@@ -188,8 +188,8 @@
 def test_type_caching():
     ffi1 = FFI(); ffi1.cdef("struct foo_s;")
     ffi2 = FFI(); ffi2.cdef("struct foo_s;")    # different one!
-    ffi1, lib1 = verify2(ffi1, 'test_type_caching_1', 'struct foo_s;')
-    ffi2, lib2 = verify2(ffi2, 'test_type_caching_2', 'struct foo_s;')
+    lib1 = verify(ffi1, 'test_type_caching_1', 'struct foo_s;')
+    lib2 = verify(ffi2, 'test_type_caching_2', 'struct foo_s;')
     # shared types
     assert ffi1.typeof("long") is ffi2.typeof("long")
     assert ffi1.typeof("long**") is ffi2.typeof("long * *")


More information about the pypy-commit mailing list