File: exception.py

package info (click to toggle)
jinja 1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 1,408 kB
  • ctags: 1,171
  • sloc: python: 6,438; ansic: 397; makefile: 74
file content (113 lines) | stat: -rw-r--r-- 3,181 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))

import jdebug
from jinja import Environment, DictLoader
from jinja.exceptions import TemplateNotFound
from wsgiref.simple_server import make_server

e = Environment(loader=DictLoader({
    '/': u'''
<html>
  <head>
    <title>Various Broken Templates</title>
    <style type="text/css">
      body {
        margin: 2em;
        font-size: 1.5em;
        font-family: sans-serif
      }
      a {
        color: #d00;
      }
    </style>
  </head>
  <body>
    <h1>Various Broken Templates</h1>
    <p>
      This small WSGI application serves some Jinja templates that
      are just broken. It uses the colubrid traceback middleware to
      render those errors including source code.
    </p>
    <ul>
      <li><a href="syntax_error">syntax error</a></li>
      <li><a href="runtime_error">runtime error</a></li>
      <li><a href="nested_syntax_error">nested syntax error</a></li>
      <li><a href="nested_runtime_error">nested runtime error</a></li>
      <li><a href="code_runtime_error">runtime error in code</a></li>
      <li><a href="syntax_from_string">a syntax error from string</a></li>
      <li><a href="runtime_from_string">runtime error from a string</a></li>
      <li><a href="multiple_templates">multiple templates</a></li>
    </ul>
  </body>
</html>
''',
    '/syntax_error': u'''
{% for item in foo %}
    ...
{% endif %}
    ''',
    '/runtime_error': u'''
{% set foo = 1 / 0 %}
    ''',
    '/nested_runtime_error': u'''
{% include 'runtime_broken' %}
    ''',
    '/nested_syntax_error': u'''
{% include 'syntax_broken' %}
    ''',
    '/code_runtime_error': u'''We have a runtime error here:
    {{ broken() }}''',
    '/multiple_templates': '''\
{{ fire_multiple_broken() }}
''',

    'runtime_broken': '''\
This is an included template
{% set a = 1 / 0 %}''',
    'syntax_broken': '''\
This is an included template
{% raw %}just some foo''',
    'multiple_broken': '''\
Just some context:
{% include 'macro_broken' %}
{{ broken() }}
''',
    'macro_broken': '''\
{% macro broken %}
    {{ 1 / 0 }}
{% endmacro %}
'''
}))
e.globals['fire_multiple_broken'] = lambda: \
    e.get_template('multiple_broken').render()

FAILING_STRING_TEMPLATE = '{{ 1 / 0 }}'
BROKEN_STRING_TEMPLATE = '{% if foo %}...{% endfor %}'


def broken():
    raise RuntimeError("I'm broken")


def test(environ, start_response):
    path = environ.get('PATH_INFO' or '/')
    try:
        tmpl = e.get_template(path)
    except TemplateNotFound:
        if path == '/syntax_from_string':
            tmpl = e.from_string(BROKEN_STRING_TEMPLATE)
        elif path == '/runtime_from_string':
            tmpl = e.from_string(FAILING_STRING_TEMPLATE)
        else:
            start_response('404 NOT FOUND', [('Content-Type', 'text/plain')])
            return ['NOT FOUND']
    start_response('200 OK', [('Content-Type', 'text/html; charset=utf-8')])
    return [tmpl.render(broken=broken).encode('utf-8')]


if __name__ == '__main__':
    from werkzeug.debug import DebuggedApplication
    app = DebuggedApplication(test, True)
    make_server("localhost", 7000, app).serve_forever()