PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/pypy/module/cpyext/frameobject.py

https://bitbucket.org/alex_gaynor/pypy-postgresql/
Python | 89 lines | 76 code | 9 blank | 4 comment | 2 complexity | a673fc1690b587f0c27b5a319fc38611 MD5 | raw file
  1. from pypy.rpython.lltypesystem import rffi, lltype
  2. from pypy.module.cpyext.api import (
  3. cpython_api, bootstrap_function, PyObjectFields, cpython_struct,
  4. CANNOT_FAIL)
  5. from pypy.module.cpyext.pyobject import (
  6. PyObject, Py_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. PyFrameObjectStruct = lltype.ForwardReference()
  15. PyFrameObject = lltype.Ptr(PyFrameObjectStruct)
  16. PyFrameObjectFields = (PyObjectFields +
  17. (("f_code", PyCodeObject),
  18. ("f_globals", PyObject),
  19. ("f_lineno", rffi.INT),
  20. ))
  21. cpython_struct("PyFrameObject", PyFrameObjectFields, PyFrameObjectStruct)
  22. @bootstrap_function
  23. def init_frameobject(space):
  24. make_typedescr(PyFrame.typedef,
  25. basestruct=PyFrameObject.TO,
  26. attach=frame_attach,
  27. dealloc=frame_dealloc,
  28. realize=frame_realize)
  29. def frame_attach(space, py_obj, w_obj):
  30. "Fills a newly allocated PyFrameObject with a frame object"
  31. frame = space.interp_w(PyFrame, w_obj)
  32. py_frame = rffi.cast(PyFrameObject, py_obj)
  33. py_frame.c_f_code = rffi.cast(PyCodeObject, make_ref(space, frame.pycode))
  34. py_frame.c_f_globals = make_ref(space, frame.w_globals)
  35. rffi.setintfield(py_frame, 'c_f_lineno', frame.f_lineno)
  36. @cpython_api([PyObject], lltype.Void, external=False)
  37. def frame_dealloc(space, py_obj):
  38. py_frame = rffi.cast(PyFrameObject, py_obj)
  39. py_code = rffi.cast(PyObject, py_frame.c_f_code)
  40. Py_DecRef(space, py_code)
  41. Py_DecRef(space, py_frame.c_f_globals)
  42. from pypy.module.cpyext.object import PyObject_dealloc
  43. PyObject_dealloc(space, py_obj)
  44. def frame_realize(space, py_obj):
  45. """
  46. Creates the frame in the interpreter. The PyFrameObject structure must not
  47. be modified after this call.
  48. """
  49. py_frame = rffi.cast(PyFrameObject, py_obj)
  50. py_code = rffi.cast(PyObject, py_frame.c_f_code)
  51. w_code = from_ref(space, py_code)
  52. code = space.interp_w(PyCode, w_code)
  53. w_globals = from_ref(space, py_frame.c_f_globals)
  54. frame = PyFrame(space, code, w_globals, closure=None)
  55. frame.f_lineno = py_frame.c_f_lineno
  56. w_obj = space.wrap(frame)
  57. track_reference(space, py_obj, w_obj)
  58. return w_obj
  59. @cpython_api([PyThreadState, PyCodeObject, PyObject, PyObject], PyFrameObject)
  60. def PyFrame_New(space, tstate, w_code, w_globals, w_locals):
  61. typedescr = get_typedescr(PyFrame.typedef)
  62. py_obj = typedescr.allocate(space, space.gettypeobject(PyFrame.typedef))
  63. py_frame = rffi.cast(PyFrameObject, py_obj)
  64. space.interp_w(PyCode, w_code) # sanity check
  65. py_frame.c_f_code = rffi.cast(PyCodeObject, make_ref(space, w_code))
  66. py_frame.c_f_globals = make_ref(space, w_globals)
  67. return py_frame
  68. @cpython_api([PyFrameObject], rffi.INT_real, error=-1)
  69. def PyTraceBack_Here(space, w_frame):
  70. from pypy.interpreter.pytraceback import record_application_traceback
  71. state = space.fromcache(State)
  72. if state.operror is None:
  73. return -1
  74. frame = space.interp_w(PyFrame, w_frame)
  75. record_application_traceback(space, state.operror, frame, 0)
  76. return 0
  77. @cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
  78. def PyTraceBack_Check(space, w_obj):
  79. obj = space.interpclass_w(w_obj)
  80. return obj is not None and isinstance(obj, PyTraceback)