| 1 |
secondary_entrypoints = {"main": []} |
| 2 |
|
| 3 |
import py |
| 4 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 5 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 6 |
from rpython.rlib.objectmodel import we_are_translated |
| 7 |
|
| 8 |
annotated_jit_entrypoints = [] |
| 9 |
|
| 10 |
def export_symbol(func): |
| 11 |
func.exported_symbol = True |
| 12 |
return func |
| 13 |
|
| 14 |
all_jit_entrypoints = [] |
| 15 |
|
| 16 |
def jit_entrypoint(argtypes, restype, c_name): |
| 17 |
def deco(func): |
| 18 |
func.c_name = c_name |
| 19 |
func.relax_sig_check = True |
| 20 |
export_symbol(func) |
| 21 |
all_jit_entrypoints.append((func, argtypes, restype)) |
| 22 |
return func |
| 23 |
return deco |
| 24 |
|
| 25 |
def entrypoint_lowlevel(key, argtypes, c_name=None, relax=False): |
| 26 |
""" Note: entrypoint should acquire the GIL and call |
| 27 |
llop.gc_stack_bottom on its own. |
| 28 |
|
| 29 |
If in doubt, use entrypoint_highlevel(). |
| 30 |
|
| 31 |
if key == 'main' than it's included by default |
| 32 |
""" |
| 33 |
def deco(func): |
| 34 |
secondary_entrypoints.setdefault(key, []).append((func, argtypes)) |
| 35 |
if c_name is not None: |
| 36 |
func.c_name = c_name |
| 37 |
if relax: |
| 38 |
func.relax_sig_check = True |
| 39 |
export_symbol(func) |
| 40 |
return func |
| 41 |
return deco |
| 42 |
|
| 43 |
|
| 44 |
pypy_debug_catch_fatal_exception = rffi.llexternal('pypy_debug_catch_fatal_exception', [], lltype.Void) |
| 45 |
|
| 46 |
def entrypoint_highlevel(key, argtypes, c_name=None): |
| 47 |
""" |
| 48 |
Export the decorated Python function as C, under the name 'c_name'. |
| 49 |
|
| 50 |
The function is wrapped inside a function that does the necessary |
| 51 |
GIL-acquiring and GC-root-stack-bottom-ing. |
| 52 |
|
| 53 |
If key == 'main' then it's included by default; otherwise you need |
| 54 |
to list the key in the config's secondaryentrypoints (or give it |
| 55 |
on the command-line with --entrypoints when translating). |
| 56 |
""" |
| 57 |
def deco(func): |
| 58 |
source = py.code.Source(""" |
| 59 |
from rpython.rlib import rgil |
| 60 |
|
| 61 |
def wrapper(%(args)s): |
| 62 |
# acquire the GIL |
| 63 |
rgil.acquire_maybe_in_new_thread() |
| 64 |
# |
| 65 |
llop.gc_stack_bottom(lltype.Void) # marker to enter RPython from C |
| 66 |
# this should not raise |
| 67 |
try: |
| 68 |
res = func(%(args)s) |
| 69 |
except Exception, e: |
| 70 |
if not we_are_translated(): |
| 71 |
import traceback |
| 72 |
traceback.print_exc() |
| 73 |
raise |
| 74 |
else: |
| 75 |
print(str(e)) |
| 76 |
pypy_debug_catch_fatal_exception() |
| 77 |
llop.debug_fatalerror(lltype.Void, "error in c callback") |
| 78 |
assert 0 # dead code |
| 79 |
# release the GIL |
| 80 |
rgil.release() |
| 81 |
# |
| 82 |
return res |
| 83 |
""" % {'args': ', '.join(['arg%d' % i for i in range(len(argtypes))])}) |
| 84 |
d = {'rffi': rffi, 'lltype': lltype, |
| 85 |
'pypy_debug_catch_fatal_exception': pypy_debug_catch_fatal_exception, |
| 86 |
'llop': llop, 'func': func, 'we_are_translated': we_are_translated} |
| 87 |
exec source.compile() in d |
| 88 |
wrapper = d['wrapper'] |
| 89 |
secondary_entrypoints.setdefault(key, []).append((wrapper, argtypes)) |
| 90 |
wrapper.__name__ = func.__name__ |
| 91 |
if c_name is not None: |
| 92 |
wrapper.c_name = c_name |
| 93 |
export_symbol(wrapper) |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
func.exported_wrapper = wrapper |
| 99 |
return func |
| 100 |
return deco |
| 101 |
|
| 102 |
|
| 103 |
def entrypoint(*args, **kwds): |
| 104 |
raise Exception("entrypoint.entrypoint() is removed because of a bug. " |
| 105 |
"Remove the 'aroundstate' code in your functions and " |
| 106 |
"then call entrypoint_highlevel(), which does that for " |
| 107 |
"you. Another difference is that entrypoint_highlevel() " |
| 108 |
"returns the normal Python function, which can be safely " |
| 109 |
"called from more Python code.") |