[pypy-commit] pypy cffi-static-callback-embedding: Translation fixes, manual testing...

arigo pypy.commits at gmail.com
Thu Dec 31 10:59:48 EST 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-static-callback-embedding
Changeset: r81511:d5c92fe3af5b
Date: 2015-12-31 15:30 +0000
http://bitbucket.org/pypy/pypy/changeset/d5c92fe3af5b/

Log:	Translation fixes, manual testing...

diff --git a/pypy/module/_cffi_backend/__init__.py b/pypy/module/_cffi_backend/__init__.py
--- a/pypy/module/_cffi_backend/__init__.py
+++ b/pypy/module/_cffi_backend/__init__.py
@@ -1,6 +1,7 @@
 import sys
 from pypy.interpreter.mixedmodule import MixedModule
-from rpython.rlib import rdynload, clibffi
+from rpython.rlib import rdynload, clibffi, entrypoint
+from rpython.rtyper.lltypesystem import rffi
 
 VERSION = "1.4.2"
 
@@ -66,8 +67,8 @@
         interpleveldefs['FFI_STDCALL'] = 'space.wrap(%d)' % FFI_STDCALL
 
     def startup(self, space):
-        from pypy.module._cffi_backend import cffi1_module
-        cffi1_module.glob.space = space
+        from pypy.module._cffi_backend import embedding
+        embedding.glob.space = space
 
 
 def get_dict_rtld_constants():
@@ -82,3 +83,11 @@
 
 for _name, _value in get_dict_rtld_constants().items():
     Module.interpleveldefs[_name] = 'space.wrap(%d)' % _value
+
+
+# write this entrypoint() here, to make sure it is registered early enough
+ at entrypoint.entrypoint_highlevel('main', [rffi.INT, rffi.VOIDP],
+                                 c_name='pypy_init_embedded_cffi_module')
+def pypy_init_embedded_cffi_module(version, init_struct):
+    from pypy.module._cffi_backend import embedding
+    return embedding.pypy_init_embedded_cffi_module(version, init_struct)
diff --git a/pypy/module/_cffi_backend/cffi1_module.py b/pypy/module/_cffi_backend/cffi1_module.py
--- a/pypy/module/_cffi_backend/cffi1_module.py
+++ b/pypy/module/_cffi_backend/cffi1_module.py
@@ -1,7 +1,7 @@
+import os
 from rpython.rtyper.lltypesystem import lltype, rffi
-from rpython.rlib.entrypoint import entrypoint
 
-from pypy.interpreter.error import OperationError, oefmt
+from pypy.interpreter.error import oefmt
 from pypy.interpreter.module import Module
 from pypy.module._cffi_backend import parse_c_type
 from pypy.module._cffi_backend.ffi_obj import W_FFIObject
@@ -47,81 +47,3 @@
     space.setitem(w_modules_dict, w_name, space.wrap(module))
     space.setitem(w_modules_dict, space.wrap(name + '.lib'), space.wrap(lib))
     return module
-
-
-# ____________________________________________________________
-
-
-EMBED_VERSION_MIN    = 0xB011
-EMBED_VERSION_MAX    = 0xB0FF
-
-STDERR = 2
-INITSTRUCTPTR = lltype.Ptr(lltype.Struct('CFFI_INIT',
-                                         ('name', rffi.CCHARP),
-                                         ('func', rffi.VOIDP),
-                                         ('code', rffi.CCHARP)))
-
-def load_embedded_cffi_module(space, version, init_struct):
-    from pypy.module._cffi_backend.embedding import declare_c_function
-    declare_c_function()     # translation-time hint only:
-                             # declare _cffi_carefully_make_gil()
-    #
-    version = rffi.cast(lltype.Signed, version)
-    if not (VERSION_MIN <= version <= VERSION_MAX):
-        raise oefmt(space.w_ImportError,
-            "cffi embedded module has got unknown version tag %s",
-            hex(version))
-    #
-    if space.config.objspace.usemodules.thread:
-        from pypy.module.thread import os_thread
-        os_thread.setup_threads(space)
-    #
-    name = rffi.charp2str(init_struct.name)
-    module = load_cffi1_module(space, name, None, init_struct.func)
-    code = rffi.charp2str(init_struct.code)
-    compiler = space.createcompiler()
-    pycode = compiler.compile(code, "<init code for '%s'>" % name, 'exec', 0)
-    w_globals = module.getdict(space)
-    space.call_method(w_globals, "setdefault", space.wrap("__builtins__"),
-                      space.wrap(space.builtin))
-    pycode.exec_code(space, w_globals, w_globals)
-
-
-class Global:
-    pass
-glob = Global()
-
- at entrypoint('main', [rffi.INT, rffi.VOIDP],
-            c_name='_pypy_init_embedded_cffi_module')
-def _pypy_init_embedded_cffi_module(version, init_struct):
-    name = "?"
-    try:
-        init_struct = rffi.cast(INITSTRUCTPTR, init_struct)
-        name = rffi.charp2str(init_struct.name)
-        #
-        space = glob.space
-        try:
-            load_embedded_cffi_module(space, version, init_struct)
-            res = 0
-        except OperationError, operr:
-            operr.write_unraisable(space, "initialization of '%s'" % name,
-                                   with_traceback=True)
-            space.appexec([], """():
-                import sys
-                sys.stderr.write('pypy version: %s.%s.%s\n' %
-                                 sys.pypy_version_info[:3])
-                sys.stderr.write('sys.path: %r\n' % (sys.path,))
-            """)
-            res = -1
-    except Exception, e:
-        # oups! last-level attempt to recover.
-        try:
-            os.write(STDERR, "From initialization of '")
-            os.write(STDERR, name)
-            os.write(STDERR, "':\n")
-            os.write(STDERR, str(e))
-            os.write(STDERR, "\n")
-        except:
-            pass
-        res = -1
-    return rffi.cast(rffi.INT, res)
diff --git a/pypy/module/_cffi_backend/embedding.py b/pypy/module/_cffi_backend/embedding.py
--- a/pypy/module/_cffi_backend/embedding.py
+++ b/pypy/module/_cffi_backend/embedding.py
@@ -1,13 +1,97 @@
-from rpython.rtyper.lltypesystem import rffi
+from rpython.rtyper.lltypesystem import lltype, rffi
+from rpython.translator.tool.cbuild import ExternalCompilationInfo
 
+from pypy.interpreter.error import OperationError, oefmt
 
-declare_c_function = rffi.llexternal_use_eci(separate_module_sources=[
-"""
+# ____________________________________________________________
+
+
+EMBED_VERSION_MIN    = 0xB011
+EMBED_VERSION_MAX    = 0xB0FF
+
+STDERR = 2
+INITSTRUCTPTR = lltype.Ptr(lltype.Struct('CFFI_INIT',
+                                         ('name', rffi.CCHARP),
+                                         ('func', rffi.VOIDP),
+                                         ('code', rffi.CCHARP)))
+
+def load_embedded_cffi_module(space, version, init_struct):
+    from pypy.module._cffi_backend.cffi1_module import load_cffi1_module
+    declare_c_function()     # translation-time hint only:
+                             # declare _cffi_carefully_make_gil()
+    #
+    version = rffi.cast(lltype.Signed, version)
+    if not (EMBED_VERSION_MIN <= version <= EMBED_VERSION_MAX):
+        raise oefmt(space.w_ImportError,
+            "cffi embedded module has got unknown version tag %s",
+            hex(version))
+    #
+    if space.config.objspace.usemodules.thread:
+        from pypy.module.thread import os_thread
+        os_thread.setup_threads(space)
+    #
+    name = rffi.charp2str(init_struct.name)
+    module = load_cffi1_module(space, name, None, init_struct.func)
+    code = rffi.charp2str(init_struct.code)
+    compiler = space.createcompiler()
+    pycode = compiler.compile(code, "<init code for '%s'>" % name, 'exec', 0)
+    w_globals = module.getdict(space)
+    space.call_method(w_globals, "setdefault", space.wrap("__builtins__"),
+                      space.wrap(space.builtin))
+    pycode.exec_code(space, w_globals, w_globals)
+
+
+class Global:
+    pass
+glob = Global()
+
+def pypy_init_embedded_cffi_module(version, init_struct):
+    # called from __init__.py
+    name = "?"
+    try:
+        init_struct = rffi.cast(INITSTRUCTPTR, init_struct)
+        name = rffi.charp2str(init_struct.name)
+        #
+        space = glob.space
+        try:
+            load_embedded_cffi_module(space, version, init_struct)
+            res = 0
+        except OperationError, operr:
+            operr.write_unraisable(space, "initialization of '%s'" % name,
+                                   with_traceback=True)
+            space.appexec([], r"""():
+                import sys
+                sys.stderr.write('pypy version: %s.%s.%s\n' %
+                                 sys.pypy_version_info[:3])
+                sys.stderr.write('sys.path: %r\n' % (sys.path,))
+            """)
+            res = -1
+    except Exception, e:
+        # oups! last-level attempt to recover.
+        try:
+            os.write(STDERR, "From initialization of '")
+            os.write(STDERR, name)
+            os.write(STDERR, "':\n")
+            os.write(STDERR, str(e))
+            os.write(STDERR, "\n")
+        except:
+            pass
+        res = -1
+    return rffi.cast(rffi.INT, res)
+
+# ____________________________________________________________
+
+
+eci = ExternalCompilationInfo(separate_module_sources=[
+r"""
 /* XXX Windows missing */
 #include <stdio.h>
 #include <dlfcn.h>
 #include <pthread.h>
 
+RPY_EXPORTED void rpython_startup_code(void);
+RPY_EXPORTED int pypy_setup_home(char *, int);
+
 static unsigned char _cffi_ready = 0;
 static const char *volatile _cffi_module_name;
 
@@ -24,6 +108,8 @@
     char *home;
 
     rpython_startup_code();
+    RPyGilAllocate();
+    RPyGilRelease();
 
     if (dladdr(&_cffi_init, &info) == 0) {
         _cffi_init_error("dladdr() failed: ", dlerror());
@@ -34,14 +120,11 @@
         _cffi_init_error("pypy_setup_home() failed", "");
         return;
     }
-
-    RPyGilAllocate();
-    RPyGilRelease();
     _cffi_ready = 1;
 }
 
 RPY_EXPORTED
-int _cffi_carefully_make_gil(const char *name)
+int pypy_carefully_make_gil(const char *name)
 {
     /* For CFFI: this initializes the GIL and loads the home path.
        It can be called completely concurrently from unrelated threads.
@@ -56,3 +139,5 @@
     return (int)_cffi_ready - 1;
 }
 """])
+
+declare_c_function = rffi.llexternal_use_eci(eci)


More information about the pypy-commit mailing list