Skip to content
Myr

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

1 file+24 added−10 removed

modified:flask/app.py+24 added-10 removed
6 unmodified lines
78 :copyright: (c) 2015 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""11from collections import ChainMap1211import os12import sys13from threading import Lock14from datetime import timedelta15from itertools import chain16from functools import update_wrapper17from collections import Mapping1819from werkzeug.datastructures import ImmutableDict20from werkzeug.routing import Map, Rule, RequestRedirect, BuildError21from werkzeug.exceptions import HTTPException, InternalServerError, \
10 unmodified lines
32from .templating import DispatchingJinjaLoader, Environment, \33 _default_template_ctx_processor34from .signals import request_started, request_finished, got_request_exception, \35 request_tearing_down, appcontext_tearing_down37from ._compat import reraise, string_types, text_type, integer_types36from ._compat import reraise, string_types, text_type, integer_types, iterkeys3738# a lock used for logger initialization39_logger_lock = Lock()40
33 unmodified lines
74 return error_class_or_instance.code75 return None767779class ExceptionHandlerDict(ChainMap):78class 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 line
84 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 keys
3 unmodified lines
102 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