| 1 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 2 |
from rpython.rlib import jit |
| 3 |
|
| 4 |
from pypy.interpreter.error import oefmt |
| 5 |
from pypy.interpreter.module import Module |
| 6 |
from pypy.module import _cffi_backend |
| 7 |
from pypy.module._cffi_backend import parse_c_type |
| 8 |
from pypy.module._cffi_backend.ffi_obj import W_FFIObject |
| 9 |
from pypy.module._cffi_backend.lib_obj import W_LibObject |
| 10 |
|
| 11 |
|
| 12 |
VERSION_MIN = 0x2601 |
| 13 |
VERSION_MAX = 0x28FF |
| 14 |
|
| 15 |
VERSION_EXPORT = 0x0A03 |
| 16 |
|
| 17 |
INITFUNCPTR = lltype.Ptr(lltype.FuncType([rffi.VOIDPP], lltype.Void)) |
| 18 |
|
| 19 |
@jit.dont_look_inside |
| 20 |
def load_cffi1_module(space, name, path, initptr): |
| 21 |
|
| 22 |
from pypy.module._cffi_backend.call_python import get_ll_cffi_call_python |
| 23 |
|
| 24 |
initfunc = rffi.cast(INITFUNCPTR, initptr) |
| 25 |
with lltype.scoped_alloc(rffi.VOIDPP.TO, 16, zero=True) as p: |
| 26 |
p[0] = rffi.cast(rffi.VOIDP, VERSION_EXPORT) |
| 27 |
p[1] = rffi.cast(rffi.VOIDP, get_ll_cffi_call_python()) |
| 28 |
initfunc(p) |
| 29 |
version = rffi.cast(lltype.Signed, p[0]) |
| 30 |
if not (VERSION_MIN <= version <= VERSION_MAX): |
| 31 |
raise oefmt(space.w_ImportError, |
| 32 |
"cffi extension module '%s' uses an unknown version tag %s. " |
| 33 |
"This module might need a more recent version of PyPy. " |
| 34 |
"The current PyPy provides CFFI %s.", |
| 35 |
name, hex(version), _cffi_backend.VERSION) |
| 36 |
src_ctx = rffi.cast(parse_c_type.PCTX, p[1]) |
| 37 |
|
| 38 |
ffi = W_FFIObject(space, src_ctx) |
| 39 |
lib = W_LibObject(ffi, name) |
| 40 |
if src_ctx.c_includes: |
| 41 |
lib.make_includes_from(src_ctx.c_includes) |
| 42 |
|
| 43 |
w_name = space.newtext(name) |
| 44 |
module = Module(space, w_name) |
| 45 |
if path is not None: |
| 46 |
module.setdictvalue(space, '__file__', space.newfilename(path)) |
| 47 |
module.setdictvalue(space, 'ffi', ffi) |
| 48 |
module.setdictvalue(space, 'lib', lib) |
| 49 |
w_modules_dict = space.sys.get('modules') |
| 50 |
space.setitem(w_modules_dict, w_name, module) |
| 51 |
space.setitem(w_modules_dict, space.newtext(name + '.lib'), lib) |
| 52 |
return module |