Merge pull request #3918 from pgjones/defaultdict
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by GitHub · parents 5fea7ca, 83d358d · Browse files · 3 files·+28 added−27 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+28 added−27 removed
- other:+28 added−27 removed
Provenance
- Commit
6d9d79c- Tree
a20f440- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
5fea7ca- Parent 2
83d358d
3 files changed+28 added−27 removed
modified:src/flask/app.py+11 added-12 removed
428 unmodified lines429 #:430 #: .. versionadded:: 0.11431 self.shell_context_processors = []432 433- #: all the attached blueprints in a dictionary by name. Blueprints434- #: can be attached multiple times so this dictionary does not tell435- #: you how often they got attached.433+ #: Maps registered blueprint names to blueprint objects. The434+ #: dict retains the order the blueprints were registered in.435+ #: Blueprints can be registered multiple times, this dict does436+ #: not track how often they were attached.437 #:438 #: .. versionadded:: 0.7439 self.blueprints = {}439- self._blueprint_order = []440 441 #: a place where extensions can store application specific state. For442 #: example this is where an extension could store database engines and443 #: similar things.552 unmodified lines996 f" names."997 )998 else:999 self.blueprints[blueprint.name] = blueprint1000- self._blueprint_order.append(blueprint)1000 first_registration = True1001 1002 blueprint.register(self, options, first_registration)1003 1 unmodified line1005 """Iterates over all blueprints by the order they were registered.1006 1007 .. versionadded:: 0.111008 """1010- return iter(self._blueprint_order)1009+ return self.blueprints.values()1010 1011 @setupmethod1012 def add_url_rule(1013 self,276 unmodified lines1290 (None, code),1291 (request.blueprint, None),1292 (None, None),1293 ):1295- handler_map = self.error_handler_spec.setdefault(name, {}).get(c)1294+ handler_map = self.error_handler_spec[name][c]1295 1296 if not handler_map:1297 continue1298 452 unmodified lines1751 automatically called on URL building.1752 1753 .. versionadded:: 0.71754 """1756- funcs = self.url_default_functions.get(None, ())1755+ funcs = self.url_default_functions[None]1756 if "." in endpoint:1757 bp = endpoint.rsplit(".", 1)[0]1759- funcs = chain(funcs, self.url_default_functions.get(bp, ()))1758+ funcs = chain(funcs, self.url_default_functions[bp])1759 for func in funcs:1760 func(endpoint, values)1761 1762 def handle_url_build_error(self, error, endpoint, values):29 unmodified lines1792 """1793 1794 bp = _request_ctx_stack.top.request.blueprint1795 1797- funcs = self.url_value_preprocessors.get(None, ())1796+ funcs = self.url_value_preprocessors[None]1797 if bp is not None and bp in self.url_value_preprocessors:1798 funcs = chain(funcs, self.url_value_preprocessors[bp])1799 for func in funcs:1800 func(request.endpoint, request.view_args)1801 1803- funcs = self.before_request_funcs.get(None, ())1802+ funcs = self.before_request_funcs[None]1803 if bp is not None and bp in self.before_request_funcs:1804 funcs = chain(funcs, self.before_request_funcs[bp])1805 for func in funcs:1806 rv = func()48 unmodified lines1855 Added the ``exc`` argument.1856 """1857 if exc is _sentinel:1858 exc = sys.exc_info()[1]1860- funcs = reversed(self.teardown_request_funcs.get(None, ()))1859+ funcs = reversed(self.teardown_request_funcs[None])1860 bp = _request_ctx_stack.top.request.blueprint1861 if bp is not None and bp in self.teardown_request_funcs:1862 funcs = chain(funcs, reversed(self.teardown_request_funcs[bp]))1863 for func in funcs:168 unmodified linesmodified:src/flask/blueprints.py+1 added-1 removed
249 unmodified lines250 Values of dict must be lists.251 """252 for key, values in self_dict.items():253 key = self.name if key is None else f"{self.name}.{key}"254- app_dict.setdefault(key, []).extend(values)254+ app_dict[key].extend(values)255 256 def merge_dict_nested(self_dict, app_dict):257 """Merges self_dict into app_dict. Replaces None keys with self.name.258 Values of dict must be dict.207 unmodified linesmodified:src/flask/scaffold.py+16 added-14 removed
1+from collections import defaultdict2 from functools import update_wrapper3 4 from werkzeug.exceptions import default_exceptions5 from werkzeug.exceptions import HTTPException80 unmodified lines86 #: function.87 #:88 #: To register an error handler, use the :meth:`errorhandler`89 #: decorator.89- self.error_handler_spec = {}90+ self.error_handler_spec = defaultdict(lambda: defaultdict(dict))91 92 #: A dictionary with lists of functions that will be called at the93 #: beginning of each request. The key of the dictionary is the name of94 #: the blueprint this function is active for, or ``None`` for all95 #: requests. To register a function, use the :meth:`before_request`96 #: decorator.96- self.before_request_funcs = {}97+ self.before_request_funcs = defaultdict(list)98 99 #: A dictionary with lists of functions that should be called after100 #: each request. The key of the dictionary is the name of the blueprint101 #: this function is active for, ``None`` for all requests. This can for102 #: example be used to close database connections. To register a function103 #: here, use the :meth:`after_request` decorator.103- self.after_request_funcs = {}104+ self.after_request_funcs = defaultdict(list)105 106 #: A dictionary with lists of functions that are called after107 #: each request, even if an exception has occurred. The key of the108 #: dictionary is the name of the blueprint this function is active for,3 unmodified lines112 #: teardown_request function. To register a function here, use the113 #: :meth:`teardown_request` decorator.114 #:115 #: .. versionadded:: 0.7115- self.teardown_request_funcs = {}116+ self.teardown_request_funcs = defaultdict(list)117 118 #: A dictionary with list of functions that are called without argument119 #: to populate the template context. The key of the dictionary is the120 #: name of the blueprint this function is active for, ``None`` for all121 #: requests. Each returns a dictionary that the template context is122 #: updated with. To register a function here, use the123 #: :meth:`context_processor` decorator.123- self.template_context_processors = {None: [_default_template_ctx_processor]}124+ self.template_context_processors = defaultdict(125+ list, {None: [_default_template_ctx_processor]}126+ )127 128 #: A dictionary with lists of functions that are called before the129 #: :attr:`before_request_funcs` functions. The key of the dictionary is130 #: the name of the blueprint this function is active for, or ``None``131 #: for all requests. To register a function, use132 #: :meth:`url_value_preprocessor`.133 #:134 #: .. versionadded:: 0.7132- self.url_value_preprocessors = {}135+ self.url_value_preprocessors = defaultdict(list)136 137 #: A dictionary with lists of functions that can be used as URL value138 #: preprocessors. The key ``None`` here is used for application wide139 #: callbacks, otherwise the key is the name of the blueprint.3 unmodified lines143 #: provide a :meth:`url_defaults` function that adds the parameters144 #: automatically again that were removed that way.145 #:146 #: .. versionadded:: 0.7144- self.url_default_functions = {}147+ self.url_default_functions = defaultdict(list)148 149 def _is_setup_finished(self):150 raise NotImplementedError151 108 unmodified lines260 The function will be called without any arguments. If it returns a261 non-None value, the value is handled as if it was the return value from262 the view, and further request handling is stopped.263 """261- self.before_request_funcs.setdefault(None, []).append(f)264+ self.before_request_funcs[None].append(f)265 return f266 267 @setupmethod268 def after_request(self, f):5 unmodified lines274 275 As of Flask 0.7 this function might not be executed at the end of the276 request in case an unhandled exception occurred.277 """275- self.after_request_funcs.setdefault(None, []).append(f)278+ self.after_request_funcs[None].append(f)279 return f280 281 @setupmethod282 def teardown_request(self, f):30 unmodified lines313 immediately. Instead it will keep it alive so that the interactive314 debugger can still access it. This behavior can be controlled315 by the ``PRESERVE_CONTEXT_ON_EXCEPTION`` configuration variable.316 """314- self.teardown_request_funcs.setdefault(None, []).append(f)317+ self.teardown_request_funcs[None].append(f)318 return f319 320 @setupmethod321 def context_processor(self, f):14 unmodified lines336 337 The function is passed the endpoint name and values dict. The return338 value is ignored.339 """337- self.url_value_preprocessors.setdefault(None, []).append(f)340+ self.url_value_preprocessors[None].append(f)341 return f342 343 @setupmethod344 def url_defaults(self, f):345 """Callback function for URL defaults for all view functions of the346 application. It's called with the endpoint and values and should347 update the values passed in place.348 """346- self.url_default_functions.setdefault(None, []).append(f)349+ self.url_default_functions[None].append(f)350 return f351 352 @setupmethod353 def errorhandler(self, code_or_exception):64 unmodified lines418 " code. Use a subclass of HTTPException with that code"419 " instead."420 )421 419- handlers = self.error_handler_spec.setdefault(key, {}).setdefault(code, {})420- handlers[exc_class] = f422+ self.error_handler_spec[key][code][exc_class] = f423 424 @staticmethod425 def _get_exc_class_and_code(exc_class_or_code):426 """Get the exception class being handled. For HTTP status codes27 unmodified lines