1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
import os
from rpython.rlib import entrypoint
from rpython.rtyper.lltypesystem import lltype, rffi
from rpython.translator.tool.cbuild import ExternalCompilationInfo
from pypy.interpreter.error import OperationError, oefmt
# ____________________________________________________________
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)
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 = space.newdict(module=True)
space.setitem_str(w_globals, "__builtins__", space.builtin)
pycode.exec_code(space, w_globals, w_globals)
class Global:
pass
glob = Global()
@entrypoint.entrypoint_highlevel('main', [rffi.INT, rffi.VOIDP],
c_name='pypy_init_embedded_cffi_module')
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
must_leave = False
try:
must_leave = space.threadlocals.try_enter_thread(space)
load_embedded_cffi_module(space, version, init_struct)
res = 0
except OperationError as operr:
from pypy.module._cffi_backend import errorbox
ecap = errorbox.start_error_capture(space)
operr.write_unraisable(space, "initialization of '%s'" % name,
with_traceback=True)
space.appexec([], r"""():
import sys
sys.stderr.write('pypy3 version: %s.%s.%s\n' %
sys.pypy_version_info[:3])
sys.stderr.write('sys.path: %r\n' % (sys.path,))
""")
errorbox.stop_error_capture(space, ecap)
res = -1
if must_leave:
space.threadlocals.leave_thread(space)
except Exception as 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)
# ____________________________________________________________
if os.name == 'nt':
do_includes = r"""
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#endif
#include <windows.h>
static void _cffi_init(void);
static void _cffi_init_once(void)
{
static LONG volatile lock = 0;
static int _init_called = 0;
while (InterlockedCompareExchange(&lock, 1, 0) != 0) {
SwitchToThread(); /* spin loop */
}
if (!_init_called) {
_cffi_init();
_init_called = 1;
}
InterlockedCompareExchange(&lock, 0, 1);
}
"""
else:
do_includes = r"""
#include <pthread.h>
static void _cffi_init(void);
static void _cffi_init_once(void)
{
static pthread_once_t once_control = PTHREAD_ONCE_INIT;
pthread_once(&once_control, _cffi_init);
}
"""
do_startup = do_includes + r"""
RPY_EXPORTED int 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;
static void _cffi_init_error(const char *msg, const char *extra)
{
fprintf(stderr,
"\nPyPy initialization failure when loading module '%s':\n%s%s\n",
_cffi_module_name, msg, extra);
}
static void _cffi_init(void)
{
if (rpython_startup_code() == 67) {
/* pypy was already initialized, probably because we are running
pypy which calls into some C code which independently happens
to be using a CFFI embedded module */
}
else {
RPyGilAllocate();
if (pypy_setup_home(NULL, 1) != 0) {
_cffi_init_error("pypy_setup_home() failed", "");
return;
}
}
_cffi_ready = 1;
}
RPY_EXPORTED
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.
It assumes that we don't hold the GIL before (if it exists), and we
don't hold it afterwards.
*/
_cffi_module_name = name; /* not really thread-safe, but better than
nothing */
_cffi_init_once();
return (int)_cffi_ready - 1;
}
"""
eci = ExternalCompilationInfo(separate_module_sources=[do_startup])
declare_c_function = rffi.llexternal_use_eci(eci)
|