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
Provenance
- Commit
67b0b7e- Tree
3d0513d- Author
David Lord <davidism@gmail.com>- Committer
pgjones <philip.graham.jones@googlemail.com>- Parent
c2920e2
3 files changed+55 added−29 removed
modified:src/flask/app.py+6 added-7 removed
34 unmodified lines35 from .globals import _request_ctx_stack36 from .globals import g37 from .globals import request38 from .globals import session39+from .helpers import _split_blueprint_path40 from .helpers import get_debug_flag41 from .helpers import get_env42 from .helpers import get_flashed_messages43 from .helpers import get_load_dotenv1746 unmodified lines1790 """1791 funcs: t.Iterable[URLDefaultCallable] = self.url_default_functions[None]1792 1793 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)1799 1795- while "." in bps[-1]:1796- bps.append(bps[-1].rpartition(".")[0])1797-1798- for bp in bps:1799- funcs = chain(funcs, self.url_default_functions[bp])1800-1800 for func in funcs:1801 func(endpoint, values)1802 1803 def handle_url_build_error(279 unmodified linesmodified:src/flask/helpers.py+11 added
4 unmodified lines5 import typing as t6 import warnings7 from datetime import datetime8 from datetime import timedelta9+from functools import lru_cache10 from functools import update_wrapper11 from threading import RLock12 13 import werkzeug.utils807 unmodified lines821 else:822 return True823 824 return False825+826+827+@lru_cache(maxsize=None)828+def _split_blueprint_path(name: str) -> t.List[str]:829+ out: t.List[str] = [name]830+831+ if "." in name:832+ out.extend(_split_blueprint_path(name.rpartition(".")[0]))833+834+ return outmodified:src/flask/wrappers.py+38 added-22 removed
1 import typing as t2 3 from werkzeug.exceptions import BadRequest4-from werkzeug.utils import cached_property4 from werkzeug.wrappers import Request as RequestBase5 from werkzeug.wrappers import Response as ResponseBase6 7 from . import json8 from .globals import current_app9+from .helpers import _split_blueprint_path10 11 if t.TYPE_CHECKING:12 import typing_extensions as te13 from werkzeug.routing import Rule45 unmodified lines59 return None60 61 @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.64+65+ This will be ``None`` if matching failed or has not been66+ performed yet.67+68+ 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 None73 74+ return None75+76 @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.79 81- @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.83+84+ 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.endpoint89 89- bps: t.List[str] = [self.blueprint]90+ if endpoint is not None and "." in endpoint:91+ return endpoint.rpartition(".")[0]92 91- while "." in bps[-1]:92- bps.append(bps[-1].rpartition(".")[0])93+ return None94 94- return bps95+ @property96+ def blueprints(self) -> t.List[str]:97+ """The registered names of the current blueprint upwards through98+ parent blueprints.99 100+ This will be an empty list if there is no current blueprint, or101+ if URL matching failed.102+103+ .. versionadded:: 2.0.1104+ """105+ name = self.blueprint106+107+ if name is None:108+ return []109+110+ return _split_blueprint_path(name)111+112 def _load_form_data(self) -> None:113 RequestBase._load_form_data(self)114 115 # In debug mode we're replacing the files multidict with an ad-hoc52 unmodified lines