diff options
author | Armin Ronacher <armin.ronacher@active-4.com> | 2017-01-02 13:52:27 +0100 |
---|---|---|
committer | Armin Ronacher <armin.ronacher@active-4.com> | 2017-01-03 23:45:29 +0100 |
commit | 9cd8d8e3b07e98ba5b7e87ab4b01ebacd14b9ff7 (patch) | |
tree | 0193efc7fbed4497fc0c1a3f43dc683e5d24348e | |
parent | 1205e641ed9d1c4eb581b50ab3cc5313b9950773 (diff) | |
download | jinja2-9cd8d8e3b07e98ba5b7e87ab4b01ebacd14b9ff7.tar.gz |
Fixed debug support locals for new behavior
-rw-r--r-- | jinja2/debug.py | 37 | ||||
-rw-r--r-- | tests/test_debug.py | 14 |
2 files changed, 41 insertions, 10 deletions
diff --git a/jinja2/debug.py b/jinja2/debug.py index 3252748..59e221c 100644 --- a/jinja2/debug.py +++ b/jinja2/debug.py @@ -195,21 +195,40 @@ def translate_exception(exc_info, initial_skip=0): return ProcessedTraceback(exc_info[0], exc_info[1], frames) +def get_jinja_locals(real_locals): + ctx = real_locals.get('context') + if ctx: + locals = ctx.get_all() + else: + locals = {} + + local_overrides = {} + + for name, value in iteritems(real_locals): + if not name.startswith('l_'): + continue + _, depth, name = name.split('_', 2) + depth = int(depth) + cur_depth = local_overrides.get(name, (-1,))[0] + if cur_depth < depth: + local_overrides[name] = (depth, value) + + for name, (_, value) in local_overrides.iteritems(): + if value is missing: + locals.pop(name, None) + else: + locals[name] = value + + return locals + + 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: - real_locals = tb.tb_frame.f_locals.copy() - ctx = real_locals.get('context') - if ctx: - locals = ctx.get_all() - else: - locals = {} - for name, value in iteritems(real_locals): - if name.startswith('l_') and value is not missing: - locals[name[2:]] = value + 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. diff --git a/tests/test_debug.py b/tests/test_debug.py index d8617ae..afcd826 100644 --- a/tests/test_debug.py +++ b/tests/test_debug.py @@ -27,7 +27,7 @@ def fs_env(filesystem_loader): @pytest.mark.debug -class TestDebug(): +class TestDebug(object): def assert_traceback_matches(self, callback, expected_tb): try: @@ -71,3 +71,15 @@ ZeroDivisionError: (int(eger)? )?division (or modulo )?by zero raise TemplateSyntaxError\('wtf', 42\) (jinja2\.exceptions\.)?TemplateSyntaxError: wtf line 42''') + + def test_local_extraction(self): + from jinja2.debug import get_jinja_locals + from jinja2.runtime import missing + locals = get_jinja_locals({ + 'l_0_foo': 42, + 'l_1_foo': 23, + 'l_2_foo': 13, + 'l_0_bar': 99, + 'l_1_bar': missing + }) + assert locals == {'foo': 13} |