Skip to content
Myr

exception.py

This file

Path

tests/runtime/exception.py

113 lines · 3.1 KiB · Python

Last changed by

[svn] merged newparser into trunkArmin Ronacherbefore adoption1cc232c

History of this file

Take it

1import os2import sys3sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))45import jdebug6from jinja import Environment, DictLoader7from jinja.exceptions import TemplateNotFound8from wsgiref.simple_server import make_server910e = Environment(loader=DictLoader({11    '/': u'''12<html>13  <head>14    <title>Various Broken Templates</title>15    <style type="text/css">16      body {17        margin: 2em;18        font-size: 1.5em;19        font-family: sans-serif20      }21      a {22        color: #d00;23      }24    </style>25  </head>26  <body>27    <h1>Various Broken Templates</h1>28    <p>29      This small WSGI application serves some Jinja templates that30      are just broken. It uses the colubrid traceback middleware to31      render those errors including source code.32    </p>33    <ul>34      <li><a href="syntax_error">syntax error</a></li>35      <li><a href="runtime_error">runtime error</a></li>36      <li><a href="nested_syntax_error">nested syntax error</a></li>37      <li><a href="nested_runtime_error">nested runtime error</a></li>38      <li><a href="code_runtime_error">runtime error in code</a></li>39      <li><a href="syntax_from_string">a syntax error from string</a></li>40      <li><a href="runtime_from_string">runtime error from a string</a></li>41      <li><a href="multiple_templates">multiple templates</a></li>42    </ul>43  </body>44</html>45''',46    '/syntax_error': u'''47{% for item in foo %}48    ...49{% endif %}50    ''',51    '/runtime_error': u'''52{% set foo = 1 / 0 %}53    ''',54    '/nested_runtime_error': u'''55{% include 'runtime_broken' %}56    ''',57    '/nested_syntax_error': u'''58{% include 'syntax_broken' %}59    ''',60    '/code_runtime_error': u'''We have a runtime error here:61    {{ broken() }}''',62    '/multiple_templates': '''\63{{ fire_multiple_broken() }}64''',6566    'runtime_broken': '''\67This is an included template68{% set a = 1 / 0 %}''',69    'syntax_broken': '''\70This is an included template71{% raw %}just some foo''',72    'multiple_broken': '''\73Just some context:74{% include 'macro_broken' %}75{{ broken() }}76''',77    'macro_broken': '''\78{% macro broken %}79    {{ 1 / 0 }}80{% endmacro %}81'''82}))83e.globals['fire_multiple_broken'] = lambda: \84    e.get_template('multiple_broken').render()8586FAILING_STRING_TEMPLATE = '{{ 1 / 0 }}'87BROKEN_STRING_TEMPLATE = '{% if foo %}...{% endfor %}'888990def broken():91    raise RuntimeError("I'm broken")929394def test(environ, start_response):95    path = environ.get('PATH_INFO' or '/')96    try:97        tmpl = e.get_template(path)98    except TemplateNotFound:99        if path == '/syntax_from_string':100            tmpl = e.from_string(BROKEN_STRING_TEMPLATE)101        elif path == '/runtime_from_string':102            tmpl = e.from_string(FAILING_STRING_TEMPLATE)103        else:104            start_response('404 NOT FOUND', [('Content-Type', 'text/plain')])105            return ['NOT FOUND']106    start_response('200 OK', [('Content-Type', 'text/html; charset=utf-8')])107    return [tmpl.render(broken=broken).encode('utf-8')]108109110if __name__ == '__main__':111    from werkzeug.debug import DebuggedApplication112    app = DebuggedApplication(test, True)113    make_server("localhost", 7000, app).serve_forever()