| 1 | def _current_frames(space): |
| 2 | """_current_frames() -> dictionary |
| 3 | |
| 4 | Return a dictionary mapping each current thread T's thread id to T's |
| 5 | current stack frame. Functions in the traceback module can build the |
| 6 | call stack given such a frame. |
| 7 | |
| 8 | Note that in PyPy with the JIT, calling this function causes a runtime |
| 9 | penalty in all threads, depending on the internal JIT state. In each |
| 10 | thread, the penalty should only be noticeable if this call was done |
| 11 | while in the middle of a long-running function. |
| 12 | |
| 13 | This function should be used for specialized purposes only.""" |
| 14 | w_result = space.newdict() |
| 15 | ecs = space.threadlocals.getallvalues() |
| 16 | for thread_ident, ec in ecs.items(): |
| 17 | w_topframe = ec.gettopframe_nohidden() |
| 18 | if w_topframe is None: |
| 19 | continue |
| 20 | space.setitem(w_result, |
| 21 | space.newint_from_size_t(thread_ident), |
| 22 | w_topframe) |
| 23 | return w_result |
| 24 | |
| 25 | |
| 26 | def _current_exceptions(space): |
| 27 | """_current_exceptions() -> dictionary |
| 28 | |
| 29 | Return a dict mapping each thread's identifier to its current raised |
| 30 | exception. |
| 31 | |
| 32 | Note that in PyPy with the JIT, calling this function causes a runtime |
| 33 | penalty in all threads, depending on the internal JIT state. In each |
| 34 | thread, the penalty should only be noticeable if this call was done |
| 35 | while in the middle of a long-running function. |
| 36 | |
| 37 | This function should be used for specialized purposes only.""" |
| 38 | w_result = space.newdict() |
| 39 | ecs = space.threadlocals.getallvalues() |
| 40 | for thread_ident, ec in ecs.items(): |
| 41 | operror = ec.sys_exc_info() |
| 42 | if not operror: |
| 43 | space.setitem(w_result, |
| 44 | space.newint_from_size_t(thread_ident), |
| 45 | space.newtuple([space.w_None] * 3)) |
| 46 | else: |
| 47 | space.setitem(w_result, |
| 48 | space.newint_from_size_t(thread_ident), |
| 49 | space.newtuple([operror.w_type, |
| 50 | operror.get_w_value(space), |
| 51 | operror.get_w_traceback(space)])) |
| 52 | return w_result |
| 53 | |
| 54 |