blob: 3ac4041a34315d4b3e128032b86293be62d9f7d3 [file] [log] [blame]
Armin Ronacherba3757b2008-04-16 19:43:16 +02001# -*- coding: utf-8 -*-
2"""
3 jinja2.debug
4 ~~~~~~~~~~~~
5
Armin Ronacher187bde12008-05-01 18:19:16 +02006 Implements the debug interface for Jinja. This module does some pretty
7 ugly stuff with the Python traceback system in order to achieve tracebacks
8 with correct line numbers, locals and contents.
Armin Ronacherba3757b2008-04-16 19:43:16 +02009
Armin Ronacher55494e42010-01-22 09:41:48 +010010 :copyright: (c) 2010 by the Jinja Team.
Armin Ronachera18872d2009-03-05 23:47:00 +010011 :license: BSD, see LICENSE for more details.
Armin Ronacherba3757b2008-04-16 19:43:16 +020012"""
Armin Ronacherba3757b2008-04-16 19:43:16 +020013import sys
Armin Ronachera18872d2009-03-05 23:47:00 +010014import traceback
Armin Ronacher40c593e2010-11-29 10:50:34 +010015from types import TracebackType
Armin Ronacherd416a972009-02-24 22:58:00 +010016from jinja2.utils import CodeType, missing, internal_code
Armin Ronachera18872d2009-03-05 23:47:00 +010017from jinja2.exceptions import TemplateSyntaxError
18
Armin Ronacher40c593e2010-11-29 10:50:34 +010019# on pypy we can take advantage of transparent proxies
20try:
21 from __pypy__ import tproxy
22except ImportError:
23 tproxy = None
24
Armin Ronachera18872d2009-03-05 23:47:00 +010025
Armin Ronacherbd357722009-08-05 20:25:06 +020026# how does the raise helper look like?
27try:
28 exec "raise TypeError, 'foo'"
29except SyntaxError:
30 raise_helper = 'raise __jinja_exception__[1]'
31except TypeError:
32 raise_helper = 'raise __jinja_exception__[0], __jinja_exception__[1]'
33
34
Armin Ronachera18872d2009-03-05 23:47:00 +010035class TracebackFrameProxy(object):
36 """Proxies a traceback frame."""
37
38 def __init__(self, tb):
39 self.tb = tb
Armin Ronacher40c593e2010-11-29 10:50:34 +010040 self._tb_next = None
Armin Ronachera18872d2009-03-05 23:47:00 +010041
Armin Ronacher9e5d0832010-11-29 12:16:17 +010042 @property
43 def tb_next(self):
44 return self._tb_next
45
46 def set_next(self, next):
Armin Ronachera18872d2009-03-05 23:47:00 +010047 if tb_set_next is not None:
Armin Ronacher75ffeb62011-04-18 16:27:14 +020048 try:
49 tb_set_next(self.tb, next and next.tb or None)
50 except Exception:
51 # this function can fail due to all the hackery it does
52 # on various python implementations. We just catch errors
53 # down and ignore them if necessary.
54 pass
Armin Ronachera18872d2009-03-05 23:47:00 +010055 self._tb_next = next
56
Armin Ronachera18872d2009-03-05 23:47:00 +010057 @property
58 def is_jinja_frame(self):
59 return '__jinja_template__' in self.tb.tb_frame.f_globals
60
61 def __getattr__(self, name):
62 return getattr(self.tb, name)
63
64
Armin Ronacher40c593e2010-11-29 10:50:34 +010065def make_frame_proxy(frame):
66 proxy = TracebackFrameProxy(frame)
67 if tproxy is None:
68 return proxy
69 def operation_handler(operation, *args, **kwargs):
Armin Ronacher7ae54822010-11-29 12:24:03 +010070 if operation in ('__getattribute__', '__getattr__'):
71 return getattr(proxy, args[0])
72 elif operation == '__setattr__':
73 proxy.__setattr__(*args, **kwargs)
74 else:
75 return getattr(proxy, operation)(*args, **kwargs)
Armin Ronacher40c593e2010-11-29 10:50:34 +010076 return tproxy(TracebackType, operation_handler)
77
78
Armin Ronachera18872d2009-03-05 23:47:00 +010079class ProcessedTraceback(object):
Armin Ronacher345443f2011-09-26 17:22:22 +020080 """Holds a Jinja preprocessed traceback for printing or reraising."""
Armin Ronachera18872d2009-03-05 23:47:00 +010081
82 def __init__(self, exc_type, exc_value, frames):
83 assert frames, 'no frames for this traceback?'
84 self.exc_type = exc_type
85 self.exc_value = exc_value
86 self.frames = frames
87
Armin Ronacher40c593e2010-11-29 10:50:34 +010088 # newly concatenate the frames (which are proxies)
Armin Ronachera18872d2009-03-05 23:47:00 +010089 prev_tb = None
90 for tb in self.frames:
91 if prev_tb is not None:
Armin Ronacher9e5d0832010-11-29 12:16:17 +010092 prev_tb.set_next(tb)
Armin Ronachera18872d2009-03-05 23:47:00 +010093 prev_tb = tb
Armin Ronacher9e5d0832010-11-29 12:16:17 +010094 prev_tb.set_next(None)
Armin Ronachera18872d2009-03-05 23:47:00 +010095
96 def render_as_text(self, limit=None):
97 """Return a string with the traceback."""
98 lines = traceback.format_exception(self.exc_type, self.exc_value,
99 self.frames[0], limit=limit)
100 return ''.join(lines).rstrip()
101
Armin Ronacher32ed6c92009-04-02 14:04:41 +0200102 def render_as_html(self, full=False):
103 """Return a unicode string with the traceback as rendered HTML."""
104 from jinja2.debugrenderer import render_traceback
105 return u'%s\n\n<!--\n%s\n-->' % (
106 render_traceback(self, full=full),
107 self.render_as_text().decode('utf-8', 'replace')
108 )
109
Armin Ronachera18872d2009-03-05 23:47:00 +0100110 @property
111 def is_template_syntax_error(self):
112 """`True` if this is a template syntax error."""
113 return isinstance(self.exc_value, TemplateSyntaxError)
114
115 @property
116 def exc_info(self):
117 """Exception info tuple with a proxy around the frame objects."""
118 return self.exc_type, self.exc_value, self.frames[0]
119
120 @property
121 def standard_exc_info(self):
122 """Standard python exc_info for re-raising"""
Armin Ronacher40c593e2010-11-29 10:50:34 +0100123 tb = self.frames[0]
124 # the frame will be an actual traceback (or transparent proxy) if
125 # we are on pypy or a python implementation with support for tproxy
126 if type(tb) is not TracebackType:
127 tb = tb.tb
128 return self.exc_type, self.exc_value, tb
Armin Ronachera18872d2009-03-05 23:47:00 +0100129
130
131def make_traceback(exc_info, source_hint=None):
132 """Creates a processed traceback object from the exc_info."""
133 exc_type, exc_value, tb = exc_info
134 if isinstance(exc_value, TemplateSyntaxError):
135 exc_info = translate_syntax_error(exc_value, source_hint)
Armin Ronacher2a791922009-04-16 23:15:22 +0200136 initial_skip = 0
137 else:
138 initial_skip = 1
139 return translate_exception(exc_info, initial_skip)
Armin Ronacherd416a972009-02-24 22:58:00 +0100140
141
142def translate_syntax_error(error, source=None):
143 """Rewrites a syntax error to please traceback systems."""
144 error.source = source
145 error.translated = True
Armin Ronacher821a4232010-02-17 07:59:38 +0100146 exc_info = (error.__class__, error, None)
Armin Ronacherd416a972009-02-24 22:58:00 +0100147 filename = error.filename
148 if filename is None:
149 filename = '<unknown>'
150 return fake_exc_info(exc_info, filename, error.lineno)
Armin Ronacherba3757b2008-04-16 19:43:16 +0200151
152
Armin Ronacher2a791922009-04-16 23:15:22 +0200153def translate_exception(exc_info, initial_skip=0):
Armin Ronacher8e8d0712008-04-16 23:10:49 +0200154 """If passed an exc_info it will automatically rewrite the exceptions
155 all the way down to the correct line numbers and frames.
156 """
Armin Ronacher2a791922009-04-16 23:15:22 +0200157 tb = exc_info[2]
Armin Ronachera18872d2009-03-05 23:47:00 +0100158 frames = []
Armin Ronacher8e8d0712008-04-16 23:10:49 +0200159
Armin Ronacher2a791922009-04-16 23:15:22 +0200160 # skip some internal frames if wanted
161 for x in xrange(initial_skip):
162 if tb is not None:
163 tb = tb.tb_next
164 initial_tb = tb
165
Armin Ronacher8e8d0712008-04-16 23:10:49 +0200166 while tb is not None:
Armin Ronacherd416a972009-02-24 22:58:00 +0100167 # skip frames decorated with @internalcode. These are internal
168 # calls we can't avoid and that are useless in template debugging
169 # output.
Armin Ronachera18872d2009-03-05 23:47:00 +0100170 if tb.tb_frame.f_code in internal_code:
Armin Ronacherd416a972009-02-24 22:58:00 +0100171 tb = tb.tb_next
172 continue
173
Armin Ronachera18872d2009-03-05 23:47:00 +0100174 # save a reference to the next frame if we override the current
175 # one with a faked one.
176 next = tb.tb_next
177
178 # fake template exceptions
Armin Ronacher8e8d0712008-04-16 23:10:49 +0200179 template = tb.tb_frame.f_globals.get('__jinja_template__')
180 if template is not None:
181 lineno = template.get_corresponding_lineno(tb.tb_lineno)
182 tb = fake_exc_info(exc_info[:2] + (tb,), template.filename,
Armin Ronachera18872d2009-03-05 23:47:00 +0100183 lineno)[2]
Armin Ronacher8e8d0712008-04-16 23:10:49 +0200184
Armin Ronacher40c593e2010-11-29 10:50:34 +0100185 frames.append(make_frame_proxy(tb))
Armin Ronachera18872d2009-03-05 23:47:00 +0100186 tb = next
187
188 # if we don't have any exceptions in the frames left, we have to
189 # reraise it unchanged.
190 # XXX: can we backup here? when could this happen?
191 if not frames:
192 raise exc_info[0], exc_info[1], exc_info[2]
193
Armin Ronacher40c593e2010-11-29 10:50:34 +0100194 return ProcessedTraceback(exc_info[0], exc_info[1], frames)
Armin Ronacherba3757b2008-04-16 19:43:16 +0200195
196
Armin Ronachera18872d2009-03-05 23:47:00 +0100197def fake_exc_info(exc_info, filename, lineno):
Armin Ronacher8e8d0712008-04-16 23:10:49 +0200198 """Helper for `translate_exception`."""
Armin Ronacherba3757b2008-04-16 19:43:16 +0200199 exc_type, exc_value, tb = exc_info
200
201 # figure the real context out
Armin Ronacherd416a972009-02-24 22:58:00 +0100202 if tb is not None:
203 real_locals = tb.tb_frame.f_locals.copy()
204 ctx = real_locals.get('context')
205 if ctx:
206 locals = ctx.get_all()
207 else:
208 locals = {}
209 for name, value in real_locals.iteritems():
210 if name.startswith('l_') and value is not missing:
211 locals[name[2:]] = value
212
213 # if there is a local called __jinja_exception__, we get
214 # rid of it to not break the debug functionality.
215 locals.pop('__jinja_exception__', None)
Armin Ronacher203bfcb2008-04-24 21:54:44 +0200216 else:
217 locals = {}
Armin Ronacher18c6ca02008-04-17 10:03:29 +0200218
Armin Ronacherba3757b2008-04-16 19:43:16 +0200219 # assamble fake globals we need
220 globals = {
221 '__name__': filename,
222 '__file__': filename,
Armin Ronacher94ee6aa2009-04-17 11:21:00 +0200223 '__jinja_exception__': exc_info[:2],
224
225 # we don't want to keep the reference to the template around
226 # to not cause circular dependencies, but we mark it as Jinja
227 # frame for the ProcessedTraceback
228 '__jinja_template__': None
Armin Ronacherba3757b2008-04-16 19:43:16 +0200229 }
230
231 # and fake the exception
Armin Ronacherbd357722009-08-05 20:25:06 +0200232 code = compile('\n' * (lineno - 1) + raise_helper, filename, 'exec')
Armin Ronacher32a910f2008-04-26 23:21:03 +0200233
234 # if it's possible, change the name of the code. This won't work
235 # on some python environments such as google appengine
236 try:
Armin Ronacherd416a972009-02-24 22:58:00 +0100237 if tb is None:
Armin Ronacher32a910f2008-04-26 23:21:03 +0200238 location = 'template'
Armin Ronacherd416a972009-02-24 22:58:00 +0100239 else:
240 function = tb.tb_frame.f_code.co_name
241 if function == 'root':
242 location = 'top-level template code'
243 elif function.startswith('block_'):
244 location = 'block "%s"' % function[6:]
245 else:
246 location = 'template'
Armin Ronacher32a910f2008-04-26 23:21:03 +0200247 code = CodeType(0, code.co_nlocals, code.co_stacksize,
248 code.co_flags, code.co_code, code.co_consts,
249 code.co_names, code.co_varnames, filename,
250 location, code.co_firstlineno,
251 code.co_lnotab, (), ())
252 except:
253 pass
254
255 # execute the code and catch the new traceback
Armin Ronacherba3757b2008-04-16 19:43:16 +0200256 try:
257 exec code in globals, locals
258 except:
259 exc_info = sys.exc_info()
Armin Ronacher32a910f2008-04-26 23:21:03 +0200260 new_tb = exc_info[2].tb_next
Armin Ronacherba3757b2008-04-16 19:43:16 +0200261
Armin Ronacher6cc8dd02008-04-16 23:15:15 +0200262 # return without this frame
Armin Ronacher32a910f2008-04-26 23:21:03 +0200263 return exc_info[:2] + (new_tb,)
Armin Ronacherba3757b2008-04-16 19:43:16 +0200264
265
Armin Ronacherba3757b2008-04-16 19:43:16 +0200266def _init_ugly_crap():
267 """This function implements a few ugly things so that we can patch the
268 traceback objects. The function returned allows resetting `tb_next` on
Armin Ronacher40c593e2010-11-29 10:50:34 +0100269 any python traceback object. Do not attempt to use this on non cpython
270 interpreters
Armin Ronacherba3757b2008-04-16 19:43:16 +0200271 """
272 import ctypes
273 from types import TracebackType
274
275 # figure out side of _Py_ssize_t
276 if hasattr(ctypes.pythonapi, 'Py_InitModule4_64'):
277 _Py_ssize_t = ctypes.c_int64
278 else:
279 _Py_ssize_t = ctypes.c_int
280
281 # regular python
282 class _PyObject(ctypes.Structure):
283 pass
284 _PyObject._fields_ = [
285 ('ob_refcnt', _Py_ssize_t),
286 ('ob_type', ctypes.POINTER(_PyObject))
287 ]
288
289 # python with trace
Armin Ronacherda8d68f2010-10-17 16:47:06 +0200290 if hasattr(sys, 'getobjects'):
Armin Ronacherba3757b2008-04-16 19:43:16 +0200291 class _PyObject(ctypes.Structure):
292 pass
293 _PyObject._fields_ = [
294 ('_ob_next', ctypes.POINTER(_PyObject)),
295 ('_ob_prev', ctypes.POINTER(_PyObject)),
296 ('ob_refcnt', _Py_ssize_t),
297 ('ob_type', ctypes.POINTER(_PyObject))
298 ]
299
300 class _Traceback(_PyObject):
301 pass
302 _Traceback._fields_ = [
303 ('tb_next', ctypes.POINTER(_Traceback)),
304 ('tb_frame', ctypes.POINTER(_PyObject)),
305 ('tb_lasti', ctypes.c_int),
306 ('tb_lineno', ctypes.c_int)
307 ]
308
309 def tb_set_next(tb, next):
Armin Ronacher8e8d0712008-04-16 23:10:49 +0200310 """Set the tb_next attribute of a traceback object."""
Armin Ronacherba3757b2008-04-16 19:43:16 +0200311 if not (isinstance(tb, TracebackType) and
Armin Ronacher8e8d0712008-04-16 23:10:49 +0200312 (next is None or isinstance(next, TracebackType))):
Armin Ronacherba3757b2008-04-16 19:43:16 +0200313 raise TypeError('tb_set_next arguments must be traceback objects')
314 obj = _Traceback.from_address(id(tb))
Armin Ronacher8e8d0712008-04-16 23:10:49 +0200315 if tb.tb_next is not None:
316 old = _Traceback.from_address(id(tb.tb_next))
317 old.ob_refcnt -= 1
318 if next is None:
319 obj.tb_next = ctypes.POINTER(_Traceback)()
320 else:
321 next = _Traceback.from_address(id(next))
322 next.ob_refcnt += 1
323 obj.tb_next = ctypes.pointer(next)
Armin Ronacherba3757b2008-04-16 19:43:16 +0200324
325 return tb_set_next
326
327
Armin Ronacher40c593e2010-11-29 10:50:34 +0100328# try to get a tb_set_next implementation if we don't have transparent
329# proxies.
330tb_set_next = None
331if tproxy is None:
Armin Ronacherbd33f112008-04-18 09:17:32 +0200332 try:
Armin Ronacher40c593e2010-11-29 10:50:34 +0100333 from jinja2._debugsupport import tb_set_next
334 except ImportError:
335 try:
336 tb_set_next = _init_ugly_crap()
337 except:
338 pass
339 del _init_ugly_crap