| 1 | from rpython.rtyper.lltypesystem import rffi, lltype |
| 2 | from pypy.module.cpyext.api import ( |
| 3 | PyObjectFields, CONST_STRING, CANNOT_FAIL, Py_ssize_t, |
| 4 | cpython_api, bootstrap_function, cpython_struct, |
| 5 | slot_function) |
| 6 | from pypy.module.cpyext.pyobject import ( |
| 7 | PyObject, make_ref, from_ref, decref, make_typedescr) |
| 8 | from pypy.module.cpyext.frameobject import PyFrameObject |
| 9 | from pypy.interpreter.error import OperationError |
| 10 | from pypy.interpreter.pytraceback import PyTraceback |
| 11 | from pypy.interpreter import pycode |
| 12 | |
| 13 | |
| 14 | PyTracebackObjectStruct = lltype.ForwardReference() |
| 15 | PyTracebackObject = lltype.Ptr(PyTracebackObjectStruct) |
| 16 | PyTracebackObjectFields = PyObjectFields + ( |
| 17 | ("tb_next", PyTracebackObject), |
| 18 | ("tb_frame", PyFrameObject), |
| 19 | ("tb_lasti", rffi.INT), |
| 20 | ("tb_lineno", rffi.INT), |
| 21 | ) |
| 22 | cpython_struct("PyTracebackObject", PyTracebackObjectFields, PyTracebackObjectStruct) |
| 23 | |
| 24 | @bootstrap_function |
| 25 | def init_traceback(space): |
| 26 | make_typedescr(PyTraceback.typedef, |
| 27 | basestruct=PyTracebackObject.TO, |
| 28 | attach=traceback_attach, |
| 29 | dealloc=traceback_dealloc) |
| 30 | |
| 31 | |
| 32 | def traceback_attach(space, py_obj, w_obj, w_userdata=None): |
| 33 | py_traceback = rffi.cast(PyTracebackObject, py_obj) |
| 34 | traceback = space.interp_w(PyTraceback, w_obj) |
| 35 | if traceback.next is None: |
| 36 | w_next_traceback = None |
| 37 | else: |
| 38 | w_next_traceback = traceback.next |
| 39 | py_traceback.c_tb_next = rffi.cast(PyTracebackObject, make_ref(space, w_next_traceback)) |
| 40 | py_traceback.c_tb_frame = rffi.cast(PyFrameObject, make_ref(space, traceback.frame)) |
| 41 | rffi.setintfield(py_traceback, 'c_tb_lasti', traceback.lasti) |
| 42 | rffi.setintfield(py_traceback, 'c_tb_lineno',traceback.get_lineno()) |
| 43 | |
| 44 | @slot_function([PyObject], lltype.Void) |
| 45 | def traceback_dealloc(space, py_obj): |
| 46 | py_traceback = rffi.cast(PyTracebackObject, py_obj) |
| 47 | decref(space, rffi.cast(PyObject, py_traceback.c_tb_next)) |
| 48 | decref(space, rffi.cast(PyObject, py_traceback.c_tb_frame)) |
| 49 | from pypy.module.cpyext.object import _dealloc |
| 50 | _dealloc(space, py_obj) |