Skip to content
Myr

Started work on support for transparent proxies for the debug hack

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 6a3e95d · Browse files · 1 file+39 added−17 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+39 added−17 removed

  • other:+39 added−17 removed

1 file+39 added−17 removed

modified:jinja2/debug.py+39 added-17 removed
10 unmodified lines
11 :license: BSD, see LICENSE for more details.12"""13import sys14import traceback15from types import TracebackType16from jinja2.utils import CodeType, missing, internal_code17from jinja2.exceptions import TemplateSyntaxError1819# on pypy we can take advantage of transparent proxies20try:21 from __pypy__ import tproxy22except ImportError:23 tproxy = None242526# how does the raise helper look like?27try:28 exec "raise TypeError, 'foo'"29except SyntaxError:
6 unmodified lines
36 """Proxies a traceback frame."""3738 def __init__(self, tb):39 self.tb = tb40 self._tb_next = None4142 def _set_tb_next(self, next):43 if tb_set_next is not None:44 tb_set_next(self.tb, next and next.tb or None)
12 unmodified lines
57 def __getattr__(self, name):58 return getattr(self.tb, name)596061def make_frame_proxy(frame):62 proxy = TracebackFrameProxy(frame)63 if tproxy is None:64 return proxy65 def operation_handler(operation, *args, **kwargs):66 return getattr(proxy, operation)(*args, **kwargs)67 return tproxy(TracebackType, operation_handler)686970class ProcessedTraceback(object):71 """Holds a Jinja preprocessed traceback for priting or reraising."""7273 def __init__(self, exc_type, exc_value, frames):
1 unmodified line
75 self.exc_type = exc_type76 self.exc_value = exc_value77 self.frames = frames7862 def chain_frames(self):63 """Chains the frames. Requires ctypes or the debugsupport extension."""79 # newly concatenate the frames (which are proxies)80 prev_tb = None81 for tb in self.frames:82 if prev_tb is not None:83 prev_tb.tb_next = tb
26 unmodified lines
110111 @property112 def standard_exc_info(self):113 """Standard python exc_info for re-raising"""114 tb = self.frames[0]115 # the frame will be an actual traceback (or transparent proxy) if116 # we are on pypy or a python implementation with support for tproxy117 if type(tb) is not TracebackType:118 tb = tb.tb98 return self.exc_type, self.exc_value, self.frames[0].tb119 return self.exc_type, self.exc_value, tb120121122def make_traceback(exc_info, source_hint=None):123 """Creates a processed traceback object from the exc_info."""
48 unmodified lines
172 lineno = template.get_corresponding_lineno(tb.tb_lineno)173 tb = fake_exc_info(exc_info[:2] + (tb,), template.filename,174 lineno)[2]175155 frames.append(TracebackFrameProxy(tb))176 frames.append(make_frame_proxy(tb))177 tb = next178179 # if we don't have any exceptions in the frames left, we have to180 # reraise it unchanged.181 # XXX: can we backup here? when could this happen?182 if not frames:183 raise exc_info[0], exc_info[1], exc_info[2]184164 traceback = ProcessedTraceback(exc_info[0], exc_info[1], frames)165 if tb_set_next is not None:166 traceback.chain_frames()167 return traceback185 return ProcessedTraceback(exc_info[0], exc_info[1], frames)186187188def fake_exc_info(exc_info, filename, lineno):189 """Helper for `translate_exception`."""
66 unmodified lines
256257def _init_ugly_crap():258 """This function implements a few ugly things so that we can patch the259 traceback objects. The function returned allows resetting `tb_next` on242 any python traceback object.260 any python traceback object. Do not attempt to use this on non cpython261 interpreters262 """263 import ctypes264 from types import TracebackType265
49 unmodified lines
315316 return tb_set_next317318300# try to get a tb_set_next implementation301try:302 from jinja2._debugsupport import tb_set_next303except ImportError:319# try to get a tb_set_next implementation if we don't have transparent320# proxies.321tb_set_next = None322if tproxy is None:323 try:324 from jinja2._debugsupport import tb_set_next325 except ImportError:326 try:305 tb_set_next = _init_ugly_crap()306 except:307 tb_set_next = None308del _init_ugly_crap327 tb_set_next = _init_ugly_crap()328 except:329 pass330 del _init_ugly_crap