summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Lord <davidism@gmail.com>2019-11-27 13:42:50 -0800
committerDavid Lord <davidism@gmail.com>2019-11-27 13:42:50 -0800
commit225295075f23fe8b884c8e327c67654b57738ad1 (patch)
tree26cd884df3caefd295ec46301fac2bee8d2632e5
parent6c1a62f7776edac2e4402ceafa0e6d9ba12bcedd (diff)
downloadjinja2-patch-traceback.tar.gz
-rw-r--r--jinja2/asyncsupport.py10
-rw-r--r--jinja2/debug.py467
-rw-r--r--jinja2/environment.py53
-rw-r--r--jinja2/nativetypes.py5
4 files changed, 157 insertions, 378 deletions
diff --git a/jinja2/asyncsupport.py b/jinja2/asyncsupport.py
index 7d457e3..aa55e05 100644
--- a/jinja2/asyncsupport.py
+++ b/jinja2/asyncsupport.py
@@ -11,7 +11,6 @@
"""
import asyncio
import inspect
-import sys
from functools import update_wrapper
from jinja2.environment import TemplateModule
@@ -37,10 +36,8 @@ async def generate_async(self, *args, **kwargs):
async for event in self.root_render_func(self.new_context(vars)):
yield event
except Exception:
- exc_info = sys.exc_info()
- else:
- return
- yield self.environment.handle_exception(exc_info, True)
+ yield self.environment.handle_exception()
+
def wrap_generate_func(original_generate):
@@ -69,8 +66,7 @@ async def render_async(self, *args, **kwargs):
try:
return await concat_async(self.root_render_func(ctx))
except Exception:
- exc_info = sys.exc_info()
- return self.environment.handle_exception(exc_info, True)
+ return self.environment.handle_exception()
def wrap_render_func(original_render):
diff --git a/jinja2/debug.py b/jinja2/debug.py
index d3c1a3a..e0f0fc5 100644
--- a/jinja2/debug.py
+++ b/jinja2/debug.py
@@ -1,378 +1,195 @@
-# -*- coding: utf-8 -*-
-"""
- jinja2.debug
- ~~~~~~~~~~~~
-
- Implements the debug interface for Jinja. This module does some pretty
- ugly stuff with the Python traceback system in order to achieve tracebacks
- with correct line numbers, locals and contents.
-
- :copyright: (c) 2017 by the Jinja Team.
- :license: BSD, see LICENSE for more details.
-"""
import sys
-import traceback
-from types import TracebackType, CodeType
-from jinja2.utils import missing, internal_code
-from jinja2.exceptions import TemplateSyntaxError
-from jinja2._compat import iteritems, reraise, PY2
+from types import CodeType
-# on pypy we can take advantage of transparent proxies
-try:
- from __pypy__ import tproxy
-except ImportError:
- tproxy = None
+from jinja2 import TemplateSyntaxError
+from jinja2._compat import PYPY
+from jinja2.utils import internal_code
+from jinja2.utils import missing
-# how does the raise helper look like?
-try:
- exec("raise TypeError, 'foo'")
-except SyntaxError:
- raise_helper = 'raise __jinja_exception__[1]'
-except TypeError:
- raise_helper = 'raise __jinja_exception__[0], __jinja_exception__[1]'
+def rewrite_traceback_stack(source=None):
+ exc_type, exc_value, tb = sys.exc_info()
+ stack = []
-
-class TracebackFrameProxy(object):
- """Proxies a traceback frame."""
-
- def __init__(self, tb):
- self.tb = tb
- self._tb_next = None
-
- @property
- def tb_next(self):
- return self._tb_next
-
- def set_next(self, next):
- if tb_set_next is not None:
- try:
- tb_set_next(self.tb, next and next.tb or None)
- except Exception:
- # this function can fail due to all the hackery it does
- # on various python implementations. We just catch errors
- # down and ignore them if necessary.
- pass
- self._tb_next = next
-
- @property
- def is_jinja_frame(self):
- return '__jinja_template__' in self.tb.tb_frame.f_globals
-
- def __getattr__(self, name):
- return getattr(self.tb, name)
-
-
-def make_frame_proxy(frame):
- proxy = TracebackFrameProxy(frame)
- if tproxy is None:
- return proxy
- def operation_handler(operation, *args, **kwargs):
- if operation in ('__getattribute__', '__getattr__'):
- return getattr(proxy, args[0])
- elif operation == '__setattr__':
- proxy.__setattr__(*args, **kwargs)
- else:
- return getattr(proxy, operation)(*args, **kwargs)
- return tproxy(TracebackType, operation_handler)
-
-
-class ProcessedTraceback(object):
- """Holds a Jinja preprocessed traceback for printing or reraising."""
-
- def __init__(self, exc_type, exc_value, frames):
- assert frames, 'no frames for this traceback?'
- self.exc_type = exc_type
- self.exc_value = exc_value
- self.frames = frames
-
- # newly concatenate the frames (which are proxies)
- prev_tb = None
- for tb in self.frames:
- if prev_tb is not None:
- prev_tb.set_next(tb)
- prev_tb = tb
- prev_tb.set_next(None)
-
- def render_as_text(self, limit=None):
- """Return a string with the traceback."""
- lines = traceback.format_exception(self.exc_type, self.exc_value,
- self.frames[0], limit=limit)
- return ''.join(lines).rstrip()
-
- def render_as_html(self, full=False):
- """Return a unicode string with the traceback as rendered HTML."""
- from jinja2.debugrenderer import render_traceback
- return u'%s\n\n<!--\n%s\n-->' % (
- render_traceback(self, full=full),
- self.render_as_text().decode('utf-8', 'replace')
- )
-
- @property
- def is_template_syntax_error(self):
- """`True` if this is a template syntax error."""
- return isinstance(self.exc_value, TemplateSyntaxError)
-
- @property
- def exc_info(self):
- """Exception info tuple with a proxy around the frame objects."""
- return self.exc_type, self.exc_value, self.frames[0]
-
- @property
- def standard_exc_info(self):
- """Standard python exc_info for re-raising"""
- tb = self.frames[0]
- # the frame will be an actual traceback (or transparent proxy) if
- # we are on pypy or a python implementation with support for tproxy
- if type(tb) is not TracebackType:
- tb = tb.tb
- return self.exc_type, self.exc_value, tb
-
-
-def make_traceback(exc_info, source_hint=None):
- """Creates a processed traceback object from the exc_info."""
- exc_type, exc_value, tb = exc_info
if isinstance(exc_value, TemplateSyntaxError):
- exc_info = translate_syntax_error(exc_value, source_hint)
- initial_skip = 0
+ exc_value.source = source
+ exc_value.translated = True
+ tb = retarget_traceback(
+ exc_value, tb, exc_value.filename or "<unknown>", exc_value.lineno
+ )
else:
- initial_skip = 1
- return translate_exception(exc_info, initial_skip)
-
-
-def translate_syntax_error(error, source=None):
- """Rewrites a syntax error to please traceback systems."""
- error.source = source
- error.translated = True
- exc_info = (error.__class__, error, None)
- filename = error.filename
- if filename is None:
- filename = '<unknown>'
- return fake_exc_info(exc_info, filename, error.lineno)
-
-
-def translate_exception(exc_info, initial_skip=0):
- """If passed an exc_info it will automatically rewrite the exceptions
- all the way down to the correct line numbers and frames.
- """
- tb = exc_info[2]
- frames = []
-
- # skip some internal frames if wanted
- for x in range(initial_skip):
- if tb is not None:
- tb = tb.tb_next
- initial_tb = tb
+ tb = tb.tb_next
while tb is not None:
- # skip frames decorated with @internalcode. These are internal
- # calls we can't avoid and that are useless in template debugging
- # output.
if tb.tb_frame.f_code in internal_code:
tb = tb.tb_next
continue
- # save a reference to the next frame if we override the current
- # one with a faked one.
- next = tb.tb_next
+ template = tb.tb_frame.f_globals.get("__jinja_template__")
- # fake template exceptions
- template = tb.tb_frame.f_globals.get('__jinja_template__')
if template is not None:
lineno = template.get_corresponding_lineno(tb.tb_lineno)
- tb = fake_exc_info(exc_info[:2] + (tb,), template.filename,
- lineno)[2]
+ fake_tb = retarget_traceback(exc_value, tb, template.filename, lineno)
+ stack.append(fake_tb)
+ else:
+ stack.append(tb)
+
+ tb = tb.tb_next
+
+ tb_next = None
+
+ for tb in reversed(stack):
+ tb_next = tb_set_next(tb, tb_next)
+
+ return exc_type, exc_value, tb_next
+
+
+def retarget_traceback(exc_value, tb, filename, lineno):
+ if tb is not None:
+ locals = get_jinja_locals(tb.tb_frame.f_locals)
+ locals.pop("__jinja_exception__", None)
+ else:
+ locals = {}
+
+ globals = {
+ "__name__": filename,
+ "__file__": filename,
+ "__jinja_exception__": exc_value,
+ }
+ code = compile('\n' * (lineno - 1) + "raise __jinja_exception__", filename, "exec")
- frames.append(make_frame_proxy(tb))
- tb = next
+ try:
+ location = "template"
- # if we don't have any exceptions in the frames left, we have to
- # reraise it unchanged.
- # XXX: can we backup here? when could this happen?
- if not frames:
- reraise(exc_info[0], exc_info[1], exc_info[2])
+ if tb is not None:
+ function = tb.tb_frame.f_code.co_name
- return ProcessedTraceback(exc_info[0], exc_info[1], frames)
+ if function == "root":
+ location = "top-level template code"
+ elif function.startswith("block_"):
+ location = 'block "%s"' % function[6:]
+
+ code_args = []
+
+ for attr in (
+ "argcount",
+ "posonlyargcount",
+ "kwonlyargcount",
+ "nlocals",
+ "stacksize",
+ "flags",
+ "code", # codestring
+ "consts", # constants
+ "names",
+ "varnames",
+ ("filename", filename),
+ ("name", location),
+ "firstlineno",
+ "lnotab",
+ "freevars",
+ "cellvars",
+ ):
+ if isinstance(attr, tuple):
+ code_args.append(attr[1])
+ continue
+
+ try:
+ code_args.append(getattr(code, "co_" + attr))
+ except AttributeError:
+ continue
+
+ code = CodeType(*code_args)
+ except Exception:
+ pass
+
+ try:
+ exec(code, globals, locals)
+ except:
+ return sys.exc_info()[2].tb_next
def get_jinja_locals(real_locals):
- ctx = real_locals.get('context')
+ ctx = real_locals.get("context")
+
if ctx:
- locals = ctx.get_all().copy()
+ data = ctx.get_all().copy()
else:
- locals = {}
+ data = {}
local_overrides = {}
- for name, value in iteritems(real_locals):
- if not name.startswith('l_') or value is missing:
+ for name, value in real_locals.items():
+ if not name.startswith("l_") or value is missing:
continue
+
try:
- _, depth, name = name.split('_', 2)
+ _, depth, name = name.split("_", 2)
depth = int(depth)
except ValueError:
continue
+
cur_depth = local_overrides.get(name, (-1,))[0]
+
if cur_depth < depth:
local_overrides[name] = (depth, value)
- for name, (_, value) in iteritems(local_overrides):
+ for name, (_, value) in local_overrides.items():
if value is missing:
- locals.pop(name, None)
+ data.pop(name, None)
else:
- locals[name] = value
+ data[name] = value
- return locals
+ return data
-def fake_exc_info(exc_info, filename, lineno):
- """Helper for `translate_exception`."""
- exc_type, exc_value, tb = exc_info
-
- # figure the real context out
- if tb is not None:
- locals = get_jinja_locals(tb.tb_frame.f_locals)
-
- # if there is a local called __jinja_exception__, we get
- # rid of it to not break the debug functionality.
- locals.pop('__jinja_exception__', None)
- else:
- locals = {}
-
- # assamble fake globals we need
- globals = {
- '__name__': filename,
- '__file__': filename,
- '__jinja_exception__': exc_info[:2],
-
- # we don't want to keep the reference to the template around
- # to not cause circular dependencies, but we mark it as Jinja
- # frame for the ProcessedTraceback
- '__jinja_template__': None
- }
-
- # and fake the exception
- code = compile('\n' * (lineno - 1) + raise_helper, filename, 'exec')
-
- # if it's possible, change the name of the code. This won't work
- # on some python environments such as google appengine
+if sys.version_info >= (3, 7):
+ def tb_set_next(tb, tb_next):
+ tb.tb_next = tb_next
+ return tb
+elif PYPY:
try:
- if tb is None:
- location = 'template'
- else:
- function = tb.tb_frame.f_code.co_name
- if function == 'root':
- location = 'top-level template code'
- elif function.startswith('block_'):
- location = 'block "%s"' % function[6:]
- else:
- location = 'template'
-
- if PY2:
- code = CodeType(0, code.co_nlocals, code.co_stacksize,
- code.co_flags, code.co_code, code.co_consts,
- code.co_names, code.co_varnames, filename,
- location, code.co_firstlineno,
- code.co_lnotab, (), ())
- else:
- code = CodeType(0, code.co_kwonlyargcount,
- code.co_nlocals, code.co_stacksize,
- code.co_flags, code.co_code, code.co_consts,
- code.co_names, code.co_varnames, filename,
- location, code.co_firstlineno,
- code.co_lnotab, (), ())
- except Exception as e:
- pass
-
- # execute the code and catch the new traceback
- try:
- exec(code, globals, locals)
- except:
- exc_info = sys.exc_info()
- new_tb = exc_info[2].tb_next
-
- # return without this frame
- return exc_info[:2] + (new_tb,)
+ import tputil
+ except ImportError:
+ def tb_set_next(tb, tb_next):
+ return tb
+ else:
+ def tb_set_next(tb, tb_next):
+ def controller(op):
+ if op.args[0] == "tb_next":
+ return tb_next
+ return op.delegate()
-def _init_ugly_crap():
- """This function implements a few ugly things so that we can patch the
- traceback objects. The function returned allows resetting `tb_next` on
- any python traceback object. Do not attempt to use this on non cpython
- interpreters
- """
+ return tputil.make_proxy(controller, obj=tb)
+else:
import ctypes
- from types import TracebackType
-
- if PY2:
- # figure out size of _Py_ssize_t for Python 2:
- if hasattr(ctypes.pythonapi, 'Py_InitModule4_64'):
- _Py_ssize_t = ctypes.c_int64
- else:
- _Py_ssize_t = ctypes.c_int
- else:
- # platform ssize_t on Python 3
- _Py_ssize_t = ctypes.c_ssize_t
- # regular python
- class _PyObject(ctypes.Structure):
+ class _CTraceback(ctypes.Structure):
pass
- _PyObject._fields_ = [
- ('ob_refcnt', _Py_ssize_t),
- ('ob_type', ctypes.POINTER(_PyObject))
- ]
- # python with trace
- if hasattr(sys, 'getobjects'):
- class _PyObject(ctypes.Structure):
- pass
- _PyObject._fields_ = [
- ('_ob_next', ctypes.POINTER(_PyObject)),
- ('_ob_prev', ctypes.POINTER(_PyObject)),
- ('ob_refcnt', _Py_ssize_t),
- ('ob_type', ctypes.POINTER(_PyObject))
- ]
-
- class _Traceback(_PyObject):
- pass
- _Traceback._fields_ = [
- ('tb_next', ctypes.POINTER(_Traceback)),
- ('tb_frame', ctypes.POINTER(_PyObject)),
- ('tb_lasti', ctypes.c_int),
- ('tb_lineno', ctypes.c_int)
+ _c_traceback_p = ctypes.POINTER(_CTraceback)
+ _CTraceback._fields_ = [
+ # Extra PyObject slots when compiled with Py_TRACE_REFS.
+ ("PyObject_HEAD", ctypes.c_byte * (32 if hasattr(sys, "getobjects") else 16)),
+ ("tb_next", _c_traceback_p),
+ ("tb_frame", ctypes.c_void_p),
+ ("tb_lasti", ctypes.c_int),
+ ("tb_lineno", ctypes.c_int),
]
- def tb_set_next(tb, next):
- """Set the tb_next attribute of a traceback object."""
- if not (isinstance(tb, TracebackType) and
- (next is None or isinstance(next, TracebackType))):
- raise TypeError('tb_set_next arguments must be traceback objects')
- obj = _Traceback.from_address(id(tb))
- if tb.tb_next is not None:
- old = _Traceback.from_address(id(tb.tb_next))
- old.ob_refcnt -= 1
- if next is None:
- obj.tb_next = ctypes.POINTER(_Traceback)()
- else:
- next = _Traceback.from_address(id(next))
- next.ob_refcnt += 1
- obj.tb_next = ctypes.pointer(next)
+ def tb_set_next(tb, tb_next):
+ c_tb = _CTraceback.from_address(id(tb))
- return tb_set_next
+ # Clear out the old tb_next.
+ if tb.tb_next is not None:
+ c_old_tb = _CTraceback.from_address(id(tb.tb_next))
+ c_tb.tb_next = _c_traceback_p()
+ ctypes.pythonapi.Py_DecRef(c_old_tb)
+ # Assign the new tb_next.
+ if tb_next is not None:
+ c_tb_next = _CTraceback.from_address(id(tb_next))
+ ctypes.pythonapi.Py_IncRef(c_tb_next)
+ c_tb.tb_next = _c_traceback_p(c_tb_next)
-# try to get a tb_set_next implementation if we don't have transparent
-# proxies.
-tb_set_next = None
-if tproxy is None:
- # traceback.tb_next can be modified since CPython 3.7
- if sys.version_info >= (3, 7):
- def tb_set_next(tb, next):
- tb.tb_next = next
- else:
- # On Python 3.6 and older, use ctypes
- try:
- tb_set_next = _init_ugly_crap()
- except Exception:
- pass
-del _init_ugly_crap
+ return tb
diff --git a/jinja2/environment.py b/jinja2/environment.py
index 974479f..2bfc018 100644
--- a/jinja2/environment.py
+++ b/jinja2/environment.py
@@ -36,10 +36,6 @@ from jinja2._compat import imap, ifilter, string_types, iteritems, \
# for direct template usage we have up to ten living environments
_spontaneous_environments = LRUCache(10)
-# the function to create jinja traceback objects. This is dynamically
-# imported on the first exception in the exception handler.
-_make_traceback = None
-
def get_spontaneous_environment(cls, *args):
"""Return a new spontaneous environment. A spontaneous environment
@@ -251,10 +247,6 @@ class Environment(object):
#: must not be modified
shared = False
- #: these are currently EXPERIMENTAL undocumented features.
- exception_handler = None
- exception_formatter = None
-
#: the class that is used for code generation. See
#: :class:`~jinja2.compiler.CodeGenerator` for more information.
code_generator_class = CodeGenerator
@@ -493,8 +485,7 @@ class Environment(object):
try:
return self._parse(source, name, filename)
except TemplateSyntaxError:
- exc_info = sys.exc_info()
- self.handle_exception(exc_info, source_hint=source)
+ self.handle_exception(source=source)
def _parse(self, source, name, filename):
"""Internal parsing function used by `parse` and `compile`."""
@@ -514,8 +505,7 @@ class Environment(object):
try:
return self.lexer.tokeniter(source, name, filename)
except TemplateSyntaxError:
- exc_info = sys.exc_info()
- self.handle_exception(exc_info, source_hint=source)
+ self.handle_exception(source=source)
def preprocess(self, source, name=None, filename=None):
"""Preprocesses the source with all extensions. This is automatically
@@ -591,8 +581,7 @@ class Environment(object):
filename = encode_filename(filename)
return self._compile(source, filename)
except TemplateSyntaxError:
- exc_info = sys.exc_info()
- self.handle_exception(exc_info, source_hint=source_hint)
+ self.handle_exception(source=source_hint)
def compile_expression(self, source, undefined_to_none=True):
"""A handy helper method that returns a callable that accepts keyword
@@ -623,7 +612,6 @@ class Environment(object):
.. versionadded:: 2.1
"""
parser = Parser(self, source, state='variable')
- exc_info = None
try:
expr = parser.parse_expression()
if not parser.stream.eos:
@@ -632,9 +620,9 @@ class Environment(object):
None, None)
expr.set_environment(self)
except TemplateSyntaxError:
- exc_info = sys.exc_info()
- if exc_info is not None:
- self.handle_exception(exc_info, source_hint=source)
+ if sys.exc_info() is not None:
+ self.handle_exception(source=source)
+
body = [nodes.Assign(nodes.Name('result', 'store'), expr, lineno=1)]
template = self.from_string(nodes.Template(body, lineno=1))
return TemplateExpression(template, undefined_to_none)
@@ -761,27 +749,12 @@ class Environment(object):
x = list(ifilter(filter_func, x))
return x
- def handle_exception(self, exc_info=None, rendered=False, source_hint=None):
+ def handle_exception(self, source=None):
"""Exception handling helper. This is used internally to either raise
rewritten exceptions or return a rendered traceback for the template.
"""
- global _make_traceback
- if exc_info is None:
- exc_info = sys.exc_info()
-
- # the debugging module is imported when it's used for the first time.
- # we're doing a lot of stuff there and for applications that do not
- # get any exceptions in template rendering there is no need to load
- # all of that.
- if _make_traceback is None:
- from jinja2.debug import make_traceback as _make_traceback
- traceback = _make_traceback(exc_info, source_hint)
- if rendered and self.exception_formatter is not None:
- return self.exception_formatter(traceback)
- if self.exception_handler is not None:
- self.exception_handler(traceback)
- exc_type, exc_value, tb = traceback.standard_exc_info
- reraise(exc_type, exc_value, tb)
+ from jinja2.debug import rewrite_traceback_stack
+ reraise(*rewrite_traceback_stack(source=source))
def join_path(self, template, parent):
"""Join a template with the parent. By default all the lookups are
@@ -1013,8 +986,7 @@ class Template(object):
try:
return concat(self.root_render_func(self.new_context(vars)))
except Exception:
- exc_info = sys.exc_info()
- return self.environment.handle_exception(exc_info, True)
+ self.environment.handle_exception()
def render_async(self, *args, **kwargs):
"""This works similar to :meth:`render` but returns a coroutine
@@ -1048,10 +1020,7 @@ class Template(object):
for event in self.root_render_func(self.new_context(vars)):
yield event
except Exception:
- exc_info = sys.exc_info()
- else:
- return
- yield self.environment.handle_exception(exc_info, True)
+ yield self.environment.handle_exception()
def generate_async(self, *args, **kwargs):
"""An async version of :meth:`generate`. Works very similarly but
diff --git a/jinja2/nativetypes.py b/jinja2/nativetypes.py
index 7128a33..e0de277 100644
--- a/jinja2/nativetypes.py
+++ b/jinja2/nativetypes.py
@@ -1,4 +1,3 @@
-import sys
import types
from ast import literal_eval
from itertools import islice, chain
@@ -102,9 +101,7 @@ class NativeTemplate(Template):
self.root_render_func(self.new_context(vars)), preserve_quotes=False
)
except Exception:
- exc_info = sys.exc_info()
-
- return self.environment.handle_exception(exc_info, True)
+ return self.environment.handle_exception()
NativeEnvironment.template_class = NativeTemplate