Browse Source

Error handler deprecation

pull/262/head
Armin Ronacher 14 years ago
parent
commit
99bb902cc3
  1. 10
      scripts/flask-07-upgrade.py
  2. 5
      scripts/testproj/test.py

10
scripts/flask-07-upgrade.py

@ -35,21 +35,23 @@ except ImportError:
TEMPLATE_LOOKAHEAD = 4096 TEMPLATE_LOOKAHEAD = 4096
_app_re_part = r'((?:[a-zA-Z_][a-zA-Z0-9_]*app)|app|application)'
_from_import_re = re.compile(r'^\s*from flask import\s+')
_string_re_part = r"('([^'\\]*(?:\\.[^'\\]*)*)'" \ _string_re_part = r"('([^'\\]*(?:\\.[^'\\]*)*)'" \
r'|"([^"\\]*(?:\\.[^"\\]*)*)")' r'|"([^"\\]*(?:\\.[^"\\]*)*)")'
_from_import_re = re.compile(r'^\s*from flask import\s+')
_url_for_re = re.compile(r'\b(url_for\()(%s)' % _string_re_part) _url_for_re = re.compile(r'\b(url_for\()(%s)' % _string_re_part)
_render_template_re = re.compile(r'\b(render_template\()(%s)' % _string_re_part) _render_template_re = re.compile(r'\b(render_template\()(%s)' % _string_re_part)
_after_request_re = re.compile(r'((?:@\S+\.(?:app_)?))(after_request)(\b\s*$)(?m)') _after_request_re = re.compile(r'((?:@\S+\.(?:app_)?))(after_request)(\b\s*$)(?m)')
_module_constructor_re = re.compile(r'([a-zA-Z0-9_][a-zA-Z0-9_]*)\s*=\s*Module' _module_constructor_re = re.compile(r'([a-zA-Z0-9_][a-zA-Z0-9_]*)\s*=\s*Module'
r'\(__name__\s*(?:,\s*(%s))?' % r'\(__name__\s*(?:,\s*(%s))?' %
_string_re_part) _string_re_part)
_error_handler_re = re.compile(r'%s\.error_handlers\[\s*(\d+)\s*\]' % _app_re_part)
_mod_route_re = re.compile(r'([a-zA-Z0-9_][a-zA-Z0-9_]*)\.route') _mod_route_re = re.compile(r'([a-zA-Z0-9_][a-zA-Z0-9_]*)\.route')
_blueprint_related = [ _blueprint_related = [
(re.compile(r'request\.module'), 'request.blueprint'), (re.compile(r'request\.module'), 'request.blueprint'),
(re.compile(r'register_module'), 'register_blueprint'), (re.compile(r'register_module'), 'register_blueprint'),
(re.compile(r'(app|application)\.modules'), '\\1.blueprints') (re.compile(r'%s\.modules' % _app_re_part), '\\1.blueprints')
] ]
@ -235,6 +237,8 @@ def upgrade_python_file(filename, contents, teardown, template_bundles):
new_contents = fix_teardown_funcs(new_contents) new_contents = fix_teardown_funcs(new_contents)
new_contents = rewrite_for_blueprints(new_contents, filename, new_contents = rewrite_for_blueprints(new_contents, filename,
template_bundles) template_bundles)
new_contents = _error_handler_re.sub('\\1.error_handler_spec[None][\\2]',
new_contents)
make_diff(filename, contents, new_contents) make_diff(filename, contents, new_contents)

5
scripts/testproj/test.py

@ -9,6 +9,11 @@ app.register_module(mod)
app.register_module(mod2) app.register_module(mod2)
def handle_404(error):
return 'Testing', 404
app.error_handlers[404] = handle_404
@app.after_request @app.after_request
def after_request(response): def after_request(response):
g.db.close() g.db.close()

Loading…
Cancel
Save