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
Provenance
- Commit
40c593e- Tree
3331158- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
6a3e95d
1 file changed+39 added−17 removed
modified:jinja2/debug.py+39 added-17 removed
10 unmodified lines11 :license: BSD, see LICENSE for more details.12 """13 import sys14 import traceback15+from types import TracebackType16 from jinja2.utils import CodeType, missing, internal_code17 from jinja2.exceptions import TemplateSyntaxError18 19+# on pypy we can take advantage of transparent proxies20+try:21+ from __pypy__ import tproxy22+except ImportError:23+ tproxy = None24 25+26 # how does the raise helper look like?27 try:28 exec "raise TypeError, 'foo'"29 except SyntaxError:6 unmodified lines36 """Proxies a traceback frame."""37 38 def __init__(self, tb):39 self.tb = tb40+ self._tb_next = None41 42 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 lines57 def __getattr__(self, name):58 return getattr(self.tb, name)59 60 61+def 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)68+69+70 class ProcessedTraceback(object):71 """Holds a Jinja preprocessed traceback for priting or reraising."""72 73 def __init__(self, exc_type, exc_value, frames):1 unmodified line75 self.exc_type = exc_type76 self.exc_value = exc_value77 self.frames = frames78 62- 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 = tb26 unmodified lines110 111 @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, tb120 121 122 def make_traceback(exc_info, source_hint=None):123 """Creates a processed traceback object from the exc_info."""48 unmodified lines172 lineno = template.get_corresponding_lineno(tb.tb_lineno)173 tb = fake_exc_info(exc_info[:2] + (tb,), template.filename,174 lineno)[2]175 155- frames.append(TracebackFrameProxy(tb))176+ frames.append(make_frame_proxy(tb))177 tb = next178 179 # 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]184 164- 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)186 187 188 def fake_exc_info(exc_info, filename, lineno):189 """Helper for `translate_exception`."""66 unmodified lines256 257 def _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 lines315 316 return tb_set_next317 318 300-# try to get a tb_set_next implementation301-try:302- from jinja2._debugsupport import tb_set_next303-except ImportError:319+# try to get a tb_set_next implementation if we don't have transparent320+# proxies.321+tb_set_next = None322+if 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 = None308-del _init_ugly_crap327+ tb_set_next = _init_ugly_crap()328+ except:329+ pass330+ del _init_ugly_crap