Switched to userdict for obsolete Python versions
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Phil Schaf · committed by Phil Schaf · parent eae48d9 · Browse files · 1 file·+24 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+24 added−10 removed
- other:+24 added−10 removed
Provenance
- Commit
7126a22- Tree
e012d08- Author
Phil Schaf <flying-sheep@web.de>- Committer
Phil Schaf <flying-sheep@web.de>- Parent
eae48d9
1 file changed+24 added−10 removed
modified:flask/app.py+24 added-10 removed
6 unmodified lines7 8 :copyright: (c) 2015 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10 """11-from collections import ChainMap12-11 import os12 import sys13 from threading import Lock14 from datetime import timedelta15 from itertools import chain16 from functools import update_wrapper17+from collections import Mapping18 19 from werkzeug.datastructures import ImmutableDict20 from werkzeug.routing import Map, Rule, RequestRedirect, BuildError21 from werkzeug.exceptions import HTTPException, InternalServerError, \10 unmodified lines32 from .templating import DispatchingJinjaLoader, Environment, \33 _default_template_ctx_processor34 from .signals import request_started, request_finished, got_request_exception, \35 request_tearing_down, appcontext_tearing_down37-from ._compat import reraise, string_types, text_type, integer_types36+from ._compat import reraise, string_types, text_type, integer_types, iterkeys37 38 # a lock used for logger initialization39 _logger_lock = Lock()40 33 unmodified lines74 return error_class_or_instance.code75 return None76 77 79-class ExceptionHandlerDict(ChainMap):78+class ExceptionHandlerDict(Mapping):79 """A dict storing exception handlers or falling back to the default ones80 81 Designed to be app.error_handler_spec[blueprint_or_none]82 And hold a Exception → handler function mapping.1 unmodified line84 85 Returns None if no handler is defined for blueprint or app86 """87 def __init__(self, app, blueprint):88+ super(ExceptionHandlerDict, self).__init__()89 self.app = app90- init = super(ExceptionHandlerDict, self).__init__90+ self.data = {}91 if blueprint: # fall back to app mapping92- init({}, app.error_handler_spec[None])92+ self.fallback = app.error_handler_spec[None]93 else:94- init({})94+ self.fallback = {}95 96 def get_class(self, exc_class_or_code):97 if isinstance(exc_class_or_code, integer_types):98 # ensure that we register only exceptions as keys3 unmodified lines102 exc_class = exc_class_or_code103 return exc_class104 105 def __contains__(self, e_or_c):106- return super(ExceptionHandlerDict, self).__contains__(self.get_class(e_or_c))106+ clazz = self.get_class(e_or_c)107+ return clazz in self.data or clazz in self.fallback108 109 def __getitem__(self, e_or_c):109- return super(ExceptionHandlerDict, self).__getitem__(self.get_class(e_or_c))110+ clazz = self.get_class(e_or_c)111+ item = self.data.get(clazz)112+ if item is not None:113+ return item114+ elif len(self.fallback):115+ return self.fallback[clazz]116+ else:117+ raise KeyError(e_or_c)118 119 def __setitem__(self, e_or_c, handler):120 assert callable(handler)113- return super(ExceptionHandlerDict, self).__setitem__(self.get_class(e_or_c), handler)121+ self.data[self.get_class(e_or_c)] = handler122 123+ def __iter__(self):124+ return iterkeys(self.data)125+ 126+ def __len__(self):127+ return len(self.data)128+ 129 def find_handler(self, ex_instance):130 assert isinstance(ex_instance, Exception)131 132 for superclass in type(ex_instance).mro():1880 unmodified lines