| 1 |
import os |
| 2 |
from rpython.rlib.objectmodel import specialize, instantiate |
| 3 |
from rpython.rlib.rarithmetic import intmask |
| 4 |
from rpython.rlib import jit |
| 5 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 6 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 7 |
from rpython.rtyper.annlowlevel import llhelper |
| 8 |
|
| 9 |
from pypy.interpreter.error import oefmt |
| 10 |
from pypy.interpreter.gateway import interp2app |
| 11 |
from pypy.module._cffi_backend import parse_c_type |
| 12 |
from pypy.module._cffi_backend import cerrno |
| 13 |
from pypy.module._cffi_backend import cffi_opcode |
| 14 |
from pypy.module._cffi_backend import realize_c_type |
| 15 |
from pypy.module._cffi_backend.realize_c_type import getop, getarg |
| 16 |
|
| 17 |
|
| 18 |
STDERR = 2 |
| 19 |
EXTERNPY_FN = lltype.FuncType([parse_c_type.PEXTERNPY, rffi.CCHARP], |
| 20 |
lltype.Void) |
| 21 |
|
| 22 |
|
| 23 |
def _cffi_call_python(ll_externpy, ll_args): |
| 24 |
"""Invoked by the helpers generated from extern "Python" in the cdef. |
| 25 |
|
| 26 |
'externpy' is a static structure that describes which of the |
| 27 |
extern "Python" functions is called. It has got fields 'name' and |
| 28 |
'type_index' describing the function, and more reserved fields |
| 29 |
that are initially zero. These reserved fields are set up by |
| 30 |
ffi.def_extern(), which invokes externpy_deco() below. |
| 31 |
|
| 32 |
'args' is a pointer to an array of 8-byte entries. Each entry |
| 33 |
contains an argument. If an argument is less than 8 bytes, only |
| 34 |
the part at the beginning of the entry is initialized. If an |
| 35 |
argument is 'long double' or a struct/union, then it is passed |
| 36 |
by reference. |
| 37 |
|
| 38 |
'args' is also used as the place to write the result to |
| 39 |
(directly, even if more than 8 bytes). In all cases, 'args' is |
| 40 |
at least 8 bytes in size. |
| 41 |
""" |
| 42 |
from pypy.module._cffi_backend.ccallback import reveal_callback |
| 43 |
from rpython.rlib import rgil |
| 44 |
|
| 45 |
rgil.acquire_maybe_in_new_thread() |
| 46 |
llop.gc_stack_bottom(lltype.Void) |
| 47 |
|
| 48 |
cerrno._errno_after(rffi.RFFI_ERR_ALL | rffi.RFFI_ALT_ERRNO) |
| 49 |
|
| 50 |
if not ll_externpy.c_reserved1: |
| 51 |
|
| 52 |
|
| 53 |
try: |
| 54 |
funcname = rffi.constcharp2str(ll_externpy.c_name) |
| 55 |
msg = ("extern \"Python\": function %s() called, but no code was " |
| 56 |
"attached to it yet with @ffi.def_extern(). " |
| 57 |
"Returning 0.\n" % (funcname,)) |
| 58 |
os.write(STDERR, msg) |
| 59 |
except: |
| 60 |
pass |
| 61 |
for i in range(intmask(ll_externpy.c_size_of_result)): |
| 62 |
ll_args[i] = '\x00' |
| 63 |
else: |
| 64 |
externpython = reveal_callback(ll_externpy.c_reserved1) |
| 65 |
|
| 66 |
|
| 67 |
externpython.invoke(ll_args, ll_args) |
| 68 |
|
| 69 |
cerrno._errno_before(rffi.RFFI_ERR_ALL | rffi.RFFI_ALT_ERRNO) |
| 70 |
|
| 71 |
rgil.release() |
| 72 |
|
| 73 |
|
| 74 |
def get_ll_cffi_call_python(): |
| 75 |
return llhelper(lltype.Ptr(EXTERNPY_FN), _cffi_call_python) |
| 76 |
|
| 77 |
|
| 78 |
class KeepaliveCache: |
| 79 |
def __init__(self, space): |
| 80 |
self.cache_dict = {} |
| 81 |
|
| 82 |
|
| 83 |
@jit.dont_look_inside |
| 84 |
def externpy_deco(space, w_ffi, w_python_callable, w_name, w_error, w_onerror): |
| 85 |
from pypy.module._cffi_backend.ffi_obj import W_FFIObject |
| 86 |
from pypy.module._cffi_backend.ccallback import W_ExternPython |
| 87 |
|
| 88 |
ffi = space.interp_w(W_FFIObject, w_ffi) |
| 89 |
|
| 90 |
if space.is_w(w_name, space.w_None): |
| 91 |
w_name = space.getattr(w_python_callable, space.newtext('__name__')) |
| 92 |
name = space.text_w(w_name) |
| 93 |
|
| 94 |
ctx = ffi.ctxobj.ctx |
| 95 |
index = parse_c_type.search_in_globals(ctx, name) |
| 96 |
if index < 0: |
| 97 |
raise externpy_not_found(ffi, name) |
| 98 |
globals = rffi.cast(rffi.CArrayPtr(parse_c_type.GLOBAL_S), ctx.c_globals) |
| 99 |
|
| 100 |
g = globals[index] |
| 101 |
if getop(g.c_type_op) != cffi_opcode.OP_EXTERN_PYTHON: |
| 102 |
raise externpy_not_found(ffi, name) |
| 103 |
|
| 104 |
w_ct = realize_c_type.realize_c_type(ffi, ctx.c_types, getarg(g.c_type_op)) |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
externpy = rffi.cast(parse_c_type.PEXTERNPY, g.c_address) |
| 112 |
externpython = instantiate(W_ExternPython, nonmovable=True) |
| 113 |
cdata = rffi.cast(rffi.CCHARP, externpy) |
| 114 |
W_ExternPython.__init__(externpython, space, cdata, |
| 115 |
w_ct, w_python_callable, w_error, w_onerror) |
| 116 |
|
| 117 |
key = rffi.cast(lltype.Signed, externpy) |
| 118 |
space.fromcache(KeepaliveCache).cache_dict[key] = externpython |
| 119 |
externpy.c_reserved1 = externpython.hide_object() |
| 120 |
|
| 121 |
|
| 122 |
return w_python_callable |
| 123 |
|
| 124 |
|
| 125 |
def externpy_not_found(ffi, name): |
| 126 |
raise oefmt(ffi.w_FFIError, |
| 127 |
"ffi.def_extern('%s'): no 'extern \"Python\"' " |
| 128 |
"function with this name", name) |
| 129 |
|
| 130 |
@specialize.memo() |
| 131 |
def get_generic_decorator(space): |
| 132 |
return interp2app(externpy_deco).spacebind(space) |