| 1 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 2 |
from rpython.rlib import jit |
| 3 |
from rpython.rlib.objectmodel import specialize, not_rpython |
| 4 |
|
| 5 |
from pypy.interpreter.error import oefmt |
| 6 |
|
| 7 |
from pypy.module._hpy_universal import llapi |
| 8 |
from pypy.module._hpy_universal import handlemanager |
| 9 |
from pypy.module._hpy_universal.bridge import BRIDGE, hpy_get_bridge |
| 10 |
|
| 11 |
|
| 12 |
class State(object): |
| 13 |
@not_rpython |
| 14 |
def __init__(self, space): |
| 15 |
self.space = space |
| 16 |
uctx = lltype.malloc(llapi.HPyContext.TO, flavor='raw', immortal=True) |
| 17 |
dctx = lltype.malloc(llapi.HPyContext.TO, flavor='raw', immortal=True) |
| 18 |
|
| 19 |
self.u_handles = handlemanager.HandleManager(space, uctx) |
| 20 |
self.d_handles = handlemanager.DebugHandleManager(space, dctx, self.u_handles) |
| 21 |
self.t_handles = handlemanager.TraceHandleManager(space, self.u_handles) |
| 22 |
|
| 23 |
@jit.dont_look_inside |
| 24 |
def setup(self, space): |
| 25 |
self.u_handles.setup_universal_ctx() |
| 26 |
self.d_handles.setup_debug_ctx() |
| 27 |
self.t_handles.setup_trace_ctx() |
| 28 |
self.global_handles = {} |
| 29 |
self.setup_bridge() |
| 30 |
|
| 31 |
@staticmethod |
| 32 |
def get(space): |
| 33 |
return space.fromcache(State) |
| 34 |
|
| 35 |
@specialize.arg(1) |
| 36 |
def get_handle_manager(self, mode): |
| 37 |
if mode == llapi.MODE_DEBUG: |
| 38 |
return self.d_handles |
| 39 |
elif mode == llapi.MODE_UNIVERSAL: |
| 40 |
return self.u_handles |
| 41 |
elif mode == llapi.MODE_TRACE: |
| 42 |
return self.t_handles |
| 43 |
else: |
| 44 |
raise oefmt(self.space.w_RuntimeError, "MODE %d not valid", mode) |
| 45 |
|
| 46 |
def setup_bridge(self): |
| 47 |
if self.space.config.translating: |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
with lltype.scoped_alloc(rffi.CArray(rffi.VOIDP), 1) as ptr: |
| 55 |
for func in BRIDGE.all_functions: |
| 56 |
ptr[0] = rffi.cast(rffi.VOIDP, func.get_llhelper(self.space)) |
| 57 |
else: |
| 58 |
|
| 59 |
|
| 60 |
bridge = hpy_get_bridge() |
| 61 |
for func in BRIDGE.all_functions: |
| 62 |
funcptr = rffi.cast(rffi.VOIDP, func.get_llhelper(self.space)) |
| 63 |
fieldname = 'c_' + func.__name__ |
| 64 |
setattr(bridge, fieldname, funcptr) |
| 65 |
|
| 66 |
def was_already_setup(self): |
| 67 |
return bool(self.u_handles.ctx) |
| 68 |
|
| 69 |
@not_rpython |
| 70 |
def reset(self): |
| 71 |
""" |
| 72 |
Only for tests: reset all the C globals to match the current state. |
| 73 |
""" |
| 74 |
self.setup_bridge() |
| 75 |
llapi.hpy_debug_set_ctx(self.d_handles.ctx) |
| 76 |
tctx = llapi.hpy_trace_get_ctx(self.u_handles.ctx) |
| 77 |
tctx.c__private = llapi.cts.cast('void*', 0) |
| 78 |
self.global_handles = {} |
| 79 |
|
| 80 |
def set_exception(self, operror): |
| 81 |
self.clear_exception() |
| 82 |
ec = self.space.getexecutioncontext() |
| 83 |
ec.cpyext_operror = operror |
| 84 |
|
| 85 |
def clear_exception(self): |
| 86 |
"""Clear the current exception state, and return the operror.""" |
| 87 |
ec = self.space.getexecutioncontext() |
| 88 |
operror = ec.cpyext_operror |
| 89 |
ec.cpyext_operror = None |
| 90 |
return operror |
| 91 |
|
| 92 |
def get_exception(self): |
| 93 |
ec = self.space.getexecutioncontext() |
| 94 |
return ec.cpyext_operror |
| 95 |
|
| 96 |
def raise_current_exception(self): |
| 97 |
operror = self.clear_exception() |
| 98 |
if operror: |
| 99 |
raise operror |
| 100 |
else: |
| 101 |
raise oefmt(self.space.w_SystemError, |
| 102 |
"Function returned an error result without setting an " |
| 103 |
"exception") |