Skip to content
Myr

cache blueprint path calculation

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by pgjones · parent c2920e2 · Browse files · 3 files+55 added−29 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+55 added−29 removed

  • other:+55 added−29 removed

3 files+55 added−29 removed

modified:src/flask/app.py+6 added-7 removed
34 unmodified lines
35from .globals import _request_ctx_stack36from .globals import g37from .globals import request38from .globals import session39from .helpers import _split_blueprint_path40from .helpers import get_debug_flag41from .helpers import get_env42from .helpers import get_flashed_messages43from .helpers import get_load_dotenv
1746 unmodified lines
1790 """1791 funcs: t.Iterable[URLDefaultCallable] = self.url_default_functions[None]17921793 if "." in endpoint:1793 bps: t.List[str] = [endpoint.rsplit(".", 1)[0]]1794 # This is called by url_for, which can be called outside a1795 # request, can't use request.blueprints.1796 bps = _split_blueprint_path(endpoint.rpartition(".")[0])1797 bp_funcs = chain.from_iterable(self.url_default_functions[bp] for bp in bps)1798 funcs = chain(funcs, bp_funcs)17991795 while "." in bps[-1]:1796 bps.append(bps[-1].rpartition(".")[0])17971798 for bp in bps:1799 funcs = chain(funcs, self.url_default_functions[bp])18001800 for func in funcs:1801 func(endpoint, values)18021803 def handle_url_build_error(
279 unmodified lines
modified:src/flask/helpers.py+11 added
4 unmodified lines
5import typing as t6import warnings7from datetime import datetime8from datetime import timedelta9from functools import lru_cache10from functools import update_wrapper11from threading import RLock1213import werkzeug.utils
807 unmodified lines
821 else:822 return True823824 return False825826827@lru_cache(maxsize=None)828def _split_blueprint_path(name: str) -> t.List[str]:829 out: t.List[str] = [name]830831 if "." in name:832 out.extend(_split_blueprint_path(name.rpartition(".")[0]))833834 return out
modified:src/flask/wrappers.py+38 added-22 removed
1import typing as t23from werkzeug.exceptions import BadRequest4from werkzeug.utils import cached_property4from werkzeug.wrappers import Request as RequestBase5from werkzeug.wrappers import Response as ResponseBase67from . import json8from .globals import current_app9from .helpers import _split_blueprint_path1011if t.TYPE_CHECKING:12 import typing_extensions as te13 from werkzeug.routing import Rule
45 unmodified lines
59 return None6061 @property62 def endpoint(self) -> t.Optional[str]:63 """The endpoint that matched the request. This in combination with64 :attr:`view_args` can be used to reconstruct the same or a65 modified URL. If an exception happened when matching, this will66 be ``None``.63 """The endpoint that matched the request URL.6465 This will be ``None`` if matching failed or has not been66 performed yet.6768 This in combination with :attr:`view_args` can be used to69 reconstruct the same URL or a modified URL.70 """71 if self.url_rule is not None:72 return self.url_rule.endpoint70 else:71 return None7374 return None7576 @property77 def blueprint(self) -> t.Optional[str]:75 """The name of the current blueprint"""76 if self.url_rule and "." in self.url_rule.endpoint:77 return self.url_rule.endpoint.rsplit(".", 1)[0]78 else:79 return None78 """The registered name of the current blueprint.7981 @cached_property82 def blueprints(self) -> t.List[str]:83 """The names of the current blueprint upwards through parent84 blueprints.80 This will be ``None`` if the endpoint is not part of a81 blueprint, or if URL matching failed or has not been performed82 yet.8384 This does not necessarily match the name the blueprint was85 created with. It may have been nested, or registered with a86 different name.87 """86 if self.blueprint is None:87 return []88 endpoint = self.endpoint8989 bps: t.List[str] = [self.blueprint]90 if endpoint is not None and "." in endpoint:91 return endpoint.rpartition(".")[0]9291 while "." in bps[-1]:92 bps.append(bps[-1].rpartition(".")[0])93 return None9494 return bps95 @property96 def blueprints(self) -> t.List[str]:97 """The registered names of the current blueprint upwards through98 parent blueprints.99100 This will be an empty list if there is no current blueprint, or101 if URL matching failed.102103 .. versionadded:: 2.0.1104 """105 name = self.blueprint106107 if name is None:108 return []109110 return _split_blueprint_path(name)111112 def _load_form_data(self) -> None:113 RequestBase._load_form_data(self)114115 # In debug mode we're replacing the files multidict with an ad-hoc
52 unmodified lines