don't cache error handlers for exception mro
Before adoption: this commit came in with the history the project adopted, so no landing records it.
closes #2267, closes #1433
authored by David Lord · committed by David Lord · parent 5c12ace · Browse files · 2 files·+15 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+15 added−15 removed
- other:+15 added−15 removed
Provenance
- Commit
b5f4c52- Tree
4c8bd72- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
5c12ace
2 files changed+15 added−15 removed
modified:flask/app.py+11 added-15 removed
1482 unmodified lines1483 self.url_default_functions.setdefault(None, []).append(f)1484 return f1485 1486 def _find_error_handler(self, e):1487- """Find a registered error handler for a request in this order:1487+ """Return a registered error handler for an exception in this order:1488 blueprint handler for a specific code, app handler for a specific code,1489- blueprint generic HTTPException handler, app generic HTTPException handler,1490- and returns None if a suitable handler is not found.1489+ blueprint handler for an exception class, app handler for an exception1490+ class, or ``None`` if a suitable handler is not found.1491 """1492 exc_class, code = self._get_exc_class_and_code(type(e))1493 1494- def find_handler(handler_map):1494+ for name, c in (1495+ (request.blueprint, code), (None, code),1496+ (request.blueprint, None), (None, None)1497+ ):1498+ handler_map = self.error_handler_spec.setdefault(name, {}).get(c)1499+1500 if not handler_map:1496- return1501+ continue1502 1503 for cls in exc_class.__mro__:1504 handler = handler_map.get(cls)1505+1506 if handler is not None:1501- # cache for next time exc_class is raised1502- handler_map[exc_class] = handler1507 return handler1508 1505- # check for any in blueprint or app1506- for name, c in ((request.blueprint, code), (None, code),1507- (request.blueprint, None), (None, None)):1508- handler = find_handler(self.error_handler_spec.get(name, {}).get(c))1509-1510- if handler:1511- return handler1512-1509 def handle_http_exception(self, e):1510 """Handles an HTTP exception. By default this will invoke the1511 registered error handlers and fall back to returning the1512 exception as response.619 unmodified linesmodified:CHANGES+4 added
74 unmodified lines75 (`#2358`_)76 - Extract JSON handling to a mixin applied to both the request and response77 classes used by Flask. This adds the ``is_json`` and ``get_json`` methods to78 the response to make testing JSON response much easier. (`#2358`_)79+- Removed error handler caching because it caused unexpected results for some80+ exception inheritance hierarchies. Register handlers explicitly for each81+ exception if you don't want to traverse the MRO. (`#2362`_)82 83 .. _#1489: https://github.com/pallets/flask/pull/148984 .. _#1621: https://github.com/pallets/flask/pull/162185 .. _#1898: https://github.com/pallets/flask/pull/189814 unmodified lines100 .. _#2348: https://github.com/pallets/flask/pull/2348101 .. _#2352: https://github.com/pallets/flask/pull/2352102 .. _#2354: https://github.com/pallets/flask/pull/2354103 .. _#2358: https://github.com/pallets/flask/pull/2358104+.. _#2362: https://github.com/pallets/flask/pull/2362105 106 Version 0.12.2107 --------------108 617 unmodified lines