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