| 1 |
import re |
| 2 |
from rpython.translator import exceptiontransform |
| 3 |
from rpython.rtyper.annlowlevel import llhelper |
| 4 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 5 |
from rpython.tool.sourcetools import func_with_new_name |
| 6 |
from rpython.rlib.unroll import unrolling_iterable |
| 7 |
from rpython.rlib.objectmodel import specialize |
| 8 |
from rpython.rlib.debug import fatalerror_notb |
| 9 |
from pypy.interpreter.error import OperationError |
| 10 |
from pypy.module._hpy_universal import llapi |
| 11 |
|
| 12 |
def _restore_gil_state(gil_release, _gil_auto): |
| 13 |
from rpython.rlib import rgil |
| 14 |
|
| 15 |
unlock = gil_release or _gil_auto |
| 16 |
if unlock: |
| 17 |
rgil.release() |
| 18 |
|
| 19 |
def deadlock_error(funcname): |
| 20 |
fatalerror_notb("GIL deadlock detected when a CPython C extension " |
| 21 |
"module calls '%s'" % (funcname,)) |
| 22 |
|
| 23 |
def no_gil_error(funcname): |
| 24 |
fatalerror_notb("GIL not held when a CPython C extension " |
| 25 |
"module calls '%s'" % (funcname,)) |
| 26 |
|
| 27 |
|
| 28 |
class APISet(object): |
| 29 |
|
| 30 |
def __init__(self, cts, mode, prefix=r'^_?HPy_?', force_c_name=False): |
| 31 |
self.cts = cts |
| 32 |
self.mode = mode |
| 33 |
self.prefix = re.compile(prefix) |
| 34 |
self.force_c_name = force_c_name |
| 35 |
self.all_functions = [] |
| 36 |
self.frozen = False |
| 37 |
|
| 38 |
def _freeze_(self): |
| 39 |
self.all_functions = unrolling_iterable(self.all_functions) |
| 40 |
self.frozen = True |
| 41 |
return True |
| 42 |
|
| 43 |
def parse_signature(self, cdecl, error_value): |
| 44 |
d = self.cts.parse_func(cdecl) |
| 45 |
ARGS = d.get_llargs(self.cts) |
| 46 |
RESULT = d.get_llresult(self.cts) |
| 47 |
FUNC = lltype.Ptr(lltype.FuncType(ARGS, RESULT)) |
| 48 |
return d.name, FUNC, self.get_ll_errval(d, FUNC, error_value) |
| 49 |
|
| 50 |
def get_ll_errval(self, d, FUNC, error_value): |
| 51 |
c_result_t = d.tp.result.get_c_name() |
| 52 |
if error_value is None: |
| 53 |
|
| 54 |
if c_result_t == 'HPy': |
| 55 |
return 0 |
| 56 |
elif c_result_t == 'void': |
| 57 |
return None |
| 58 |
elif isinstance(FUNC.TO.RESULT, lltype.Ptr): |
| 59 |
return lltype.nullptr(FUNC.TO.RESULT.TO) |
| 60 |
else: |
| 61 |
raise Exception( |
| 62 |
"API function %s: you must explicitly specify an error_value " |
| 63 |
"for functions returning %s" % (d.name, c_result_t)) |
| 64 |
elif error_value == 'CANNOT_FAIL': |
| 65 |
|
| 66 |
|
| 67 |
return exceptiontransform.default_error_value(FUNC.TO.RESULT) |
| 68 |
else: |
| 69 |
assert c_result_t != 'HPy' |
| 70 |
if lltype.typeOf(error_value) != FUNC.TO.RESULT: |
| 71 |
raise Exception( |
| 72 |
"API function %s: the specified error_value has the " |
| 73 |
"wrong lltype: expected %s, got %s" % (d.name, FUNC.TO.RESULT, |
| 74 |
lltype.typeOf(error_value))) |
| 75 |
return error_value |
| 76 |
|
| 77 |
|
| 78 |
def func(self, cdecl, cpyext=False, func_name=None, error_value=None, |
| 79 |
is_helper=False, gil=None): |
| 80 |
""" |
| 81 |
Declare an HPy API function. |
| 82 |
|
| 83 |
If the function is marked as cpyext=True, it will be included in the |
| 84 |
translation only if pypy.objspace.hpy_cpyext_API==True (the |
| 85 |
default). This is useful to exclude cpyext in test_ztranslation |
| 86 |
|
| 87 |
If func_name is given, the decorated function will be automatically |
| 88 |
renamed. Useful for automatically generated code, for example in |
| 89 |
interp_number.py |
| 90 |
|
| 91 |
error_value specifies the C value to return in case the function |
| 92 |
raises an RPython exception. The default behavior tries to be smart |
| 93 |
enough to work in the most common and standardized cases, but without |
| 94 |
guessing in case of doubts. In particular, there is no default |
| 95 |
error_value for "int" functions, because CPython's behavior is not |
| 96 |
consistent. |
| 97 |
|
| 98 |
error_value can be: |
| 99 |
|
| 100 |
- None (the default): automatically determine the error value. It |
| 101 |
works only for the following return types: |
| 102 |
* HPy: 0 |
| 103 |
* void: None |
| 104 |
* pointers: NULL |
| 105 |
|
| 106 |
- 'CANNOT_FAIL': special string to specify that this function is |
| 107 |
not supposed to fail. |
| 108 |
|
| 109 |
- a specific value: in this case, the lltype must exactly match |
| 110 |
what is specified for the function type. |
| 111 |
|
| 112 |
is_helper=True is for functions which are not in the ctx. Useful if |
| 113 |
you need a ll_helper with a specific C signature, for example to use |
| 114 |
as a C callback. |
| 115 |
|
| 116 |
gil is for handling the PyPy GIL before and after the call. This is |
| 117 |
currently a subset of the cpyext handling, it may expand in the future. |
| 118 |
|
| 119 |
gil can be: |
| 120 |
|
| 121 |
- None (the default): the GIL should be held, If not held, acquire |
| 122 |
it before the call and release it after the call. This is useful |
| 123 |
when using HPy in C++/DLL initialization functions before proper |
| 124 |
interpreter startup. |
| 125 |
- "acquire": deadlock if the GIL is not currently held, and acquire |
| 126 |
it before the call. Do nothing after the call (continue holding it). |
| 127 |
- "release": do nothing in the call, release the GIL after the call |
| 128 |
""" |
| 129 |
from rpython.rlib import rgil |
| 130 |
if self.frozen: |
| 131 |
raise RuntimeError( |
| 132 |
'Too late to call @api.func(), the API object has already been frozen. ' |
| 133 |
'If you are calling @api.func() to decorate module-level functions, ' |
| 134 |
'you might solve this by making sure that the module is imported ' |
| 135 |
'earlier') |
| 136 |
gil_auto_workaround = (gil is None) |
| 137 |
|
| 138 |
gil_acquire = (gil == "acquire") |
| 139 |
gil_release = (gil == "release") |
| 140 |
assert (gil is None or gil_acquire or gil_release) |
| 141 |
|
| 142 |
def decorate(fn): |
| 143 |
from pypy.module._hpy_universal.state import State |
| 144 |
name, ll_functype, ll_errval = self.parse_signature(cdecl, error_value) |
| 145 |
if name != fn.__name__: |
| 146 |
raise ValueError( |
| 147 |
'The name of the function and the signature do not match: ' |
| 148 |
'%s != %s' % (name, fn.__name__)) |
| 149 |
|
| 150 |
if func_name is not None: |
| 151 |
fn = func_with_new_name(fn, func_name) |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
@specialize.memo() |
| 158 |
def make_wrapper(space): |
| 159 |
def wrapper(*args): |
| 160 |
_gil_auto = False |
| 161 |
if gil_auto_workaround and not rgil.am_I_holding_the_GIL(): |
| 162 |
_gil_auto = True |
| 163 |
if _gil_auto or gil_acquire: |
| 164 |
if gil_acquire and rgil.am_I_holding_the_GIL(): |
| 165 |
deadlock_error(fn.__name__) |
| 166 |
rgil.acquire() |
| 167 |
else: |
| 168 |
if not rgil.am_I_holding_the_GIL(): |
| 169 |
no_gil_error(fn.__name__) |
| 170 |
state = space.fromcache(State) |
| 171 |
if self.mode == "debug": |
| 172 |
mode = llapi.MODE_DEBUG |
| 173 |
elif self.mode == "trace": |
| 174 |
mode = llapi.MODE_TRACE |
| 175 |
else: |
| 176 |
mode = llapi.MODE_UNIVERSAL |
| 177 |
handles = state.get_handle_manager(mode) |
| 178 |
try: |
| 179 |
retval = fn(space, handles, *args) |
| 180 |
except OperationError as e: |
| 181 |
_restore_gil_state(gil_release, _gil_auto) |
| 182 |
state.set_exception(e) |
| 183 |
return ll_errval |
| 184 |
_restore_gil_state(gil_release, _gil_auto) |
| 185 |
return retval |
| 186 |
wrapper.__name__ = 'ctx_%s' % fn.__name__ |
| 187 |
if self.force_c_name: |
| 188 |
wrapper.c_name = fn.__name__ |
| 189 |
return wrapper |
| 190 |
def get_llhelper(space): |
| 191 |
return llhelper(ll_functype, make_wrapper(space)) |
| 192 |
get_llhelper.__name__ = 'get_llhelper_%s' % fn.__name__ |
| 193 |
fn.get_llhelper = get_llhelper |
| 194 |
|
| 195 |
|
| 196 |
fn.basename = self.prefix.sub(r'', fn.__name__) |
| 197 |
|
| 198 |
fn.cpyext = cpyext |
| 199 |
fn.is_helper = is_helper |
| 200 |
|
| 201 |
self.all_functions.append(fn) |
| 202 |
return fn |
| 203 |
return decorate |
| 204 |
|
| 205 |
@staticmethod |
| 206 |
def int(x): |
| 207 |
""" |
| 208 |
Helper method to convert an RPython Signed into a C int |
| 209 |
""" |
| 210 |
return rffi.cast(rffi.INT_real, x) |
| 211 |
|
| 212 |
@staticmethod |
| 213 |
def cast(typename, x): |
| 214 |
""" |
| 215 |
Helper method to convert an RPython value into the correct C return |
| 216 |
type. |
| 217 |
""" |
| 218 |
lltype = llapi.cts.gettype(typename) |
| 219 |
return rffi.cast(lltype, x) |
| 220 |
|
| 221 |
@staticmethod |
| 222 |
def ccharp2text(space, ptr): |
| 223 |
""" |
| 224 |
Convert a C const char* into a W_UnicodeObject |
| 225 |
""" |
| 226 |
s = rffi.constcharp2str(ptr) |
| 227 |
return space.newtext(s) |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
API = APISet(llapi.cts, mode="universal") |
| 232 |
DEBUG = APISet(llapi.cts, mode="debug") |
| 233 |
TRACE = APISet(llapi.cts, mode="trace") |