PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/module/sys/currentframes.py

https://bitbucket.org/pypy/pypy/
Python | 77 lines | 76 code | 0 blank | 1 comment | 0 complexity | e255d331c7e5ab935228625e3cd043b5 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. """
  2. Implementation of the 'sys._current_frames()' routine.
  3. """
  4. from pypy.interpreter import gateway
  5. app = gateway.applevel('''
  6. "NOT_RPYTHON"
  7. import __builtin__
  8. class fake_code(object):
  9. co_name = "?"
  10. co_filename = "?"
  11. co_firstlineno = 0
  12. class fake_frame(object):
  13. f_back = None
  14. f_builtins = __builtin__.__dict__
  15. f_code = fake_code()
  16. f_exc_traceback = None
  17. f_exc_type = None
  18. f_exc_value = None
  19. f_globals = {}
  20. f_lasti = -1
  21. f_lineno = 0
  22. f_locals = {}
  23. f_restricted = False
  24. f_trace = None
  25. def __init__(self, f):
  26. if f is not None:
  27. for name in ["f_builtins", "f_code", "f_globals", "f_lasti",
  28. "f_lineno"]:
  29. setattr(self, name, getattr(f, name))
  30. ''')
  31. def _current_frames(space):
  32. """_current_frames() -> dictionary
  33. Return a dictionary mapping each current thread T's thread id to T's
  34. current stack "frame". Functions in the traceback module can build the
  35. call stack given such a frame.
  36. Note that in PyPy this returns fake frame objects, to avoid a runtime
  37. penalty everywhere with the JIT. (So far these fake frames can be
  38. completely uninformative depending on the JIT state; we could return
  39. more with more efforts.)
  40. This function should be used for specialized purposes only."""
  41. w_result = space.newdict()
  42. w_fake_frame = app.wget(space, "fake_frame")
  43. ecs = space.threadlocals.getallvalues()
  44. for thread_ident, ec in ecs.items():
  45. vref = ec.topframeref
  46. frames = []
  47. while not vref.virtual:
  48. f = vref()
  49. if f is None:
  50. break
  51. frames.append(f)
  52. vref = f.f_backref
  53. else:
  54. frames.append(None)
  55. w_topframe = space.wrap(None)
  56. w_prevframe = None
  57. for f in frames:
  58. w_nextframe = space.call_function(w_fake_frame, space.wrap(f))
  59. if w_prevframe is None:
  60. w_topframe = w_nextframe
  61. else:
  62. space.setattr(w_prevframe, space.wrap('f_back'), w_nextframe)
  63. w_prevframe = w_nextframe
  64. space.setitem(w_result,
  65. space.wrap(thread_ident),
  66. w_topframe)
  67. return w_result