| 1 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 2 |
from pypy.module.cpyext.api import ( |
| 3 |
cpython_api, bootstrap_function, PyObjectFields, cpython_struct, |
| 4 |
CANNOT_FAIL, slot_function) |
| 5 |
from pypy.module.cpyext.pyobject import ( |
| 6 |
PyObject, decref, make_ref, from_ref, track_reference, |
| 7 |
make_typedescr, get_typedescr) |
| 8 |
from pypy.module.cpyext.state import State |
| 9 |
from pypy.module.cpyext.pystate import PyThreadState |
| 10 |
from pypy.module.cpyext.funcobject import PyCodeObject |
| 11 |
from pypy.interpreter.pyframe import PyFrame |
| 12 |
from pypy.interpreter.pycode import PyCode |
| 13 |
from pypy.interpreter.pytraceback import PyTraceback |
| 14 |
|
| 15 |
PyFrameObjectStruct = lltype.ForwardReference() |
| 16 |
PyFrameObject = lltype.Ptr(PyFrameObjectStruct) |
| 17 |
PyFrameObjectFields = (PyObjectFields + |
| 18 |
(("f_code", PyCodeObject), |
| 19 |
("f_globals", PyObject), |
| 20 |
("f_locals", PyObject), |
| 21 |
("f_lineno", rffi.INT), |
| 22 |
("f_back", PyFrameObject), |
| 23 |
)) |
| 24 |
cpython_struct("PyFrameObject", PyFrameObjectFields, PyFrameObjectStruct) |
| 25 |
|
| 26 |
@bootstrap_function |
| 27 |
def init_frameobject(space): |
| 28 |
make_typedescr(PyFrame.typedef, |
| 29 |
basestruct=PyFrameObject.TO, |
| 30 |
attach=frame_attach, |
| 31 |
dealloc=frame_dealloc, |
| 32 |
realize=frame_realize) |
| 33 |
|
| 34 |
def frame_attach(space, py_obj, w_obj, w_userdata=None): |
| 35 |
"Fills a newly allocated PyFrameObject with a frame object" |
| 36 |
frame = space.interp_w(PyFrame, w_obj) |
| 37 |
py_frame = rffi.cast(PyFrameObject, py_obj) |
| 38 |
py_frame.c_f_code = rffi.cast(PyCodeObject, make_ref(space, frame.pycode)) |
| 39 |
py_frame.c_f_globals = make_ref(space, frame.get_w_globals()) |
| 40 |
py_frame.c_f_locals = make_ref(space, frame.get_w_locals()) |
| 41 |
f_back = frame.get_f_back() |
| 42 |
if f_back: |
| 43 |
py_frame.c_f_back = rffi.cast(PyFrameObject, make_ref(space, f_back)) |
| 44 |
else: |
| 45 |
py_frame.c_f_back = rffi.cast(PyFrameObject, 0) |
| 46 |
|
| 47 |
|
| 48 |
rffi.setintfield(py_frame, 'c_f_lineno', frame.getorcreatedebug().f_lineno) |
| 49 |
|
| 50 |
@slot_function([PyObject], lltype.Void) |
| 51 |
def frame_dealloc(space, py_obj): |
| 52 |
py_frame = rffi.cast(PyFrameObject, py_obj) |
| 53 |
py_code = rffi.cast(PyObject, py_frame.c_f_code) |
| 54 |
decref(space, py_code) |
| 55 |
decref(space, py_frame.c_f_globals) |
| 56 |
decref(space, py_frame.c_f_locals) |
| 57 |
decref(space, py_frame.c_f_back) |
| 58 |
from pypy.module.cpyext.object import _dealloc |
| 59 |
_dealloc(space, py_obj) |
| 60 |
|
| 61 |
def frame_realize(space, py_obj): |
| 62 |
""" |
| 63 |
Creates the frame in the interpreter. The PyFrameObject structure must not |
| 64 |
be modified after this call. |
| 65 |
""" |
| 66 |
py_frame = rffi.cast(PyFrameObject, py_obj) |
| 67 |
py_code = rffi.cast(PyObject, py_frame.c_f_code) |
| 68 |
w_code = from_ref(space, py_code) |
| 69 |
code = space.interp_w(PyCode, w_code) |
| 70 |
w_globals = from_ref(space, py_frame.c_f_globals) |
| 71 |
|
| 72 |
frame = space.FrameClass(space, code, w_globals, outer_func=None) |
| 73 |
d = frame.getorcreatedebug() |
| 74 |
d.f_lineno = rffi.getintfield(py_frame, 'c_f_lineno') |
| 75 |
track_reference(space, py_obj, frame) |
| 76 |
return frame |
| 77 |
|
| 78 |
@cpython_api([PyThreadState, PyCodeObject, PyObject, PyObject], PyFrameObject, |
| 79 |
result_is_ll=True) |
| 80 |
def PyFrame_New(space, tstate, w_code, w_globals, w_locals): |
| 81 |
typedescr = get_typedescr(PyFrame.typedef) |
| 82 |
py_obj = typedescr.allocate(space, space.gettypeobject(PyFrame.typedef)) |
| 83 |
py_frame = rffi.cast(PyFrameObject, py_obj) |
| 84 |
space.interp_w(PyCode, w_code) |
| 85 |
py_frame.c_f_code = rffi.cast(PyCodeObject, make_ref(space, w_code)) |
| 86 |
py_frame.c_f_globals = make_ref(space, w_globals) |
| 87 |
py_frame.c_f_locals = make_ref(space, w_locals) |
| 88 |
return py_frame |
| 89 |
|
| 90 |
@cpython_api([PyFrameObject], rffi.INT_real, error=-1) |
| 91 |
def PyTraceBack_Here(space, w_frame): |
| 92 |
from pypy.interpreter.pytraceback import record_application_traceback |
| 93 |
state = space.fromcache(State) |
| 94 |
if state.get_exception() is None: |
| 95 |
return -1 |
| 96 |
frame = space.interp_w(PyFrame, w_frame) |
| 97 |
record_application_traceback(space, state.get_exception(), frame, 0) |
| 98 |
return 0 |
| 99 |
|
| 100 |
@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL) |
| 101 |
def PyTraceBack_Check(space, w_obj): |
| 102 |
return isinstance(w_obj, PyTraceback) |
| 103 |
|
| 104 |
@cpython_api([PyFrameObject], PyObject) |
| 105 |
def PyFrame_GetGlobals(space, w_frame): |
| 106 |
frame = space.interp_w(PyFrame, w_frame) |
| 107 |
return frame.get_w_globals() |
| 108 |
|
| 109 |
@cpython_api([PyFrameObject], PyObject) |
| 110 |
def PyFrame_GetLocals(space, w_frame): |
| 111 |
frame = space.interp_w(PyFrame, w_frame) |
| 112 |
return frame.get_w_locals() |
| 113 |
|
| 114 |
@cpython_api([PyFrameObject], PyObject) |
| 115 |
def PyFrame_GetGenerator(space, w_frame): |
| 116 |
frame = space.interp_w(PyFrame, w_frame) |
| 117 |
return frame.get_generator() |
| 118 |
|
| 119 |
@cpython_api([PyFrameObject], PyObject) |
| 120 |
def PyFrame_GetBuiltins(space, w_frame): |
| 121 |
frame = space.interp_w(PyFrame, w_frame) |
| 122 |
return frame.fget_f_builtins(space) |
| 123 |
|
| 124 |
@cpython_api([PyFrameObject], rffi.INT_real, error=-1) |
| 125 |
def PyFrame_GetLasti(space, w_frame): |
| 126 |
frame = space.interp_w(PyFrame, w_frame) |
| 127 |
w_lasti = frame.fget_f_lasti(space) |
| 128 |
return space.int_w(w_lasti) |
| 129 |
|
| 130 |
@cpython_api([PyFrameObject], rffi.INT_real, error=-1) |
| 131 |
def PyFrame_GetLineNumber(space, w_frame): |
| 132 |
frame = space.interp_w(PyFrame, w_frame) |
| 133 |
return frame.get_last_lineno() |
| 134 |
|
| 135 |
@cpython_api([PyThreadState], PyFrameObject) |
| 136 |
def PyThreadState_GetFrame(space, tstate): |
| 137 |
ec = space.getexecutioncontext() |
| 138 |
caller = ec.gettopframe_nohidden() |
| 139 |
return caller |