Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | """ |
| 3 | jinja2.debug |
| 4 | ~~~~~~~~~~~~ |
| 5 | |
| 6 | Implements the debug interface for Jinja. |
| 7 | |
| 8 | :copyright: Copyright 2008 by Armin Ronacher. |
| 9 | :license: BSD. |
| 10 | """ |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 11 | import sys |
Armin Ronacher | 32a910f | 2008-04-26 23:21:03 +0200 | [diff] [blame] | 12 | from types import CodeType |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 13 | |
| 14 | |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 15 | def translate_exception(exc_info): |
| 16 | """If passed an exc_info it will automatically rewrite the exceptions |
| 17 | all the way down to the correct line numbers and frames. |
| 18 | """ |
| 19 | result_tb = prev_tb = None |
Armin Ronacher | 6cc8dd0 | 2008-04-16 23:15:15 +0200 | [diff] [blame] | 20 | initial_tb = tb = exc_info[2].tb_next |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 21 | |
| 22 | while tb is not None: |
| 23 | template = tb.tb_frame.f_globals.get('__jinja_template__') |
| 24 | if template is not None: |
| 25 | lineno = template.get_corresponding_lineno(tb.tb_lineno) |
| 26 | tb = fake_exc_info(exc_info[:2] + (tb,), template.filename, |
| 27 | lineno, prev_tb)[2] |
| 28 | if result_tb is None: |
| 29 | result_tb = tb |
| 30 | prev_tb = tb |
| 31 | tb = tb.tb_next |
| 32 | |
| 33 | return exc_info[:2] + (result_tb or initial_tb,) |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 34 | |
| 35 | |
| 36 | def fake_exc_info(exc_info, filename, lineno, tb_back=None): |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 37 | """Helper for `translate_exception`.""" |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 38 | exc_type, exc_value, tb = exc_info |
| 39 | |
| 40 | # figure the real context out |
| 41 | real_locals = tb.tb_frame.f_locals.copy() |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 42 | ctx = real_locals.get('context') |
| 43 | if ctx: |
| 44 | locals = ctx.get_all() |
| 45 | else: |
| 46 | locals = {} |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 47 | for name, value in real_locals.iteritems(): |
| 48 | if name.startswith('l_'): |
| 49 | locals[name[2:]] = value |
| 50 | |
Armin Ronacher | 18c6ca0 | 2008-04-17 10:03:29 +0200 | [diff] [blame] | 51 | # if there is a local called __jinja_exception__, we get |
| 52 | # rid of it to not break the debug functionality. |
| 53 | locals.pop('__jinja_exception__', None) |
| 54 | |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 55 | # assamble fake globals we need |
| 56 | globals = { |
| 57 | '__name__': filename, |
| 58 | '__file__': filename, |
| 59 | '__jinja_exception__': exc_info[:2] |
| 60 | } |
| 61 | |
| 62 | # and fake the exception |
| 63 | code = compile('\n' * (lineno - 1) + 'raise __jinja_exception__[0], ' + |
| 64 | '__jinja_exception__[1]', filename, 'exec') |
Armin Ronacher | 32a910f | 2008-04-26 23:21:03 +0200 | [diff] [blame] | 65 | |
| 66 | # if it's possible, change the name of the code. This won't work |
| 67 | # on some python environments such as google appengine |
| 68 | try: |
| 69 | function = tb.tb_frame.f_code.co_name |
| 70 | if function == 'root': |
| 71 | location = 'top-level template code' |
| 72 | elif function.startswith('block_'): |
| 73 | location = 'block "%s"' % function[6:] |
| 74 | else: |
| 75 | location = 'template' |
| 76 | code = CodeType(0, code.co_nlocals, code.co_stacksize, |
| 77 | code.co_flags, code.co_code, code.co_consts, |
| 78 | code.co_names, code.co_varnames, filename, |
| 79 | location, code.co_firstlineno, |
| 80 | code.co_lnotab, (), ()) |
| 81 | except: |
| 82 | pass |
| 83 | |
| 84 | # execute the code and catch the new traceback |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 85 | try: |
| 86 | exec code in globals, locals |
| 87 | except: |
| 88 | exc_info = sys.exc_info() |
Armin Ronacher | 32a910f | 2008-04-26 23:21:03 +0200 | [diff] [blame] | 89 | new_tb = exc_info[2].tb_next |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 90 | |
| 91 | # now we can patch the exc info accordingly |
| 92 | if tb_set_next is not None: |
| 93 | if tb_back is not None: |
Armin Ronacher | 32a910f | 2008-04-26 23:21:03 +0200 | [diff] [blame] | 94 | tb_set_next(tb_back, new_tb) |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 95 | if tb is not None: |
Armin Ronacher | 32a910f | 2008-04-26 23:21:03 +0200 | [diff] [blame] | 96 | tb_set_next(new_tb, tb.tb_next) |
Armin Ronacher | 6cc8dd0 | 2008-04-16 23:15:15 +0200 | [diff] [blame] | 97 | |
| 98 | # return without this frame |
Armin Ronacher | 32a910f | 2008-04-26 23:21:03 +0200 | [diff] [blame] | 99 | return exc_info[:2] + (new_tb,) |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 100 | |
| 101 | |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 102 | def _init_ugly_crap(): |
| 103 | """This function implements a few ugly things so that we can patch the |
| 104 | traceback objects. The function returned allows resetting `tb_next` on |
| 105 | any python traceback object. |
| 106 | """ |
| 107 | import ctypes |
| 108 | from types import TracebackType |
| 109 | |
| 110 | # figure out side of _Py_ssize_t |
| 111 | if hasattr(ctypes.pythonapi, 'Py_InitModule4_64'): |
| 112 | _Py_ssize_t = ctypes.c_int64 |
| 113 | else: |
| 114 | _Py_ssize_t = ctypes.c_int |
| 115 | |
| 116 | # regular python |
| 117 | class _PyObject(ctypes.Structure): |
| 118 | pass |
| 119 | _PyObject._fields_ = [ |
| 120 | ('ob_refcnt', _Py_ssize_t), |
| 121 | ('ob_type', ctypes.POINTER(_PyObject)) |
| 122 | ] |
| 123 | |
| 124 | # python with trace |
| 125 | if object.__basicsize__ != ctypes.sizeof(_PyObject): |
| 126 | class _PyObject(ctypes.Structure): |
| 127 | pass |
| 128 | _PyObject._fields_ = [ |
| 129 | ('_ob_next', ctypes.POINTER(_PyObject)), |
| 130 | ('_ob_prev', ctypes.POINTER(_PyObject)), |
| 131 | ('ob_refcnt', _Py_ssize_t), |
| 132 | ('ob_type', ctypes.POINTER(_PyObject)) |
| 133 | ] |
| 134 | |
| 135 | class _Traceback(_PyObject): |
| 136 | pass |
| 137 | _Traceback._fields_ = [ |
| 138 | ('tb_next', ctypes.POINTER(_Traceback)), |
| 139 | ('tb_frame', ctypes.POINTER(_PyObject)), |
| 140 | ('tb_lasti', ctypes.c_int), |
| 141 | ('tb_lineno', ctypes.c_int) |
| 142 | ] |
| 143 | |
| 144 | def tb_set_next(tb, next): |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 145 | """Set the tb_next attribute of a traceback object.""" |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 146 | if not (isinstance(tb, TracebackType) and |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 147 | (next is None or isinstance(next, TracebackType))): |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 148 | raise TypeError('tb_set_next arguments must be traceback objects') |
| 149 | obj = _Traceback.from_address(id(tb)) |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 150 | if tb.tb_next is not None: |
| 151 | old = _Traceback.from_address(id(tb.tb_next)) |
| 152 | old.ob_refcnt -= 1 |
| 153 | if next is None: |
| 154 | obj.tb_next = ctypes.POINTER(_Traceback)() |
| 155 | else: |
| 156 | next = _Traceback.from_address(id(next)) |
| 157 | next.ob_refcnt += 1 |
| 158 | obj.tb_next = ctypes.pointer(next) |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 159 | |
| 160 | return tb_set_next |
| 161 | |
| 162 | |
Armin Ronacher | bd33f11 | 2008-04-18 09:17:32 +0200 | [diff] [blame] | 163 | # try to get a tb_set_next implementation |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 164 | try: |
Armin Ronacher | bd33f11 | 2008-04-18 09:17:32 +0200 | [diff] [blame] | 165 | from jinja2._speedups import tb_set_next |
| 166 | except ImportError: |
| 167 | try: |
| 168 | tb_set_next = _init_ugly_crap() |
| 169 | except: |
| 170 | tb_set_next = None |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 171 | del _init_ugly_crap |