@@ -745,12 +745,12 @@ def update_template_context(self, context: dict) -> None:
745
745
:param context: the context as a dictionary that is updated in place
746
746
to add extra variables.
747
747
"""
748
- funcs : t .Iterable [
749
- TemplateContextProcessorCallable
750
- ] = self .template_context_processors [None ]
748
+ funcs : t .Iterable [TemplateContextProcessorCallable ] = []
749
+ if None in self . template_context_processors :
750
+ funcs = chain ( funcs , self .template_context_processors [None ])
751
751
reqctx = _request_ctx_stack .top
752
752
if reqctx is not None :
753
- for bp in request .blueprints :
753
+ for bp in reversed ( request .blueprints ) :
754
754
if bp in self .template_context_processors :
755
755
funcs = chain (funcs , self .template_context_processors [bp ])
756
756
orig_ctx = context .copy ()
@@ -1806,7 +1806,9 @@ def inject_url_defaults(self, endpoint: str, values: dict) -> None:
1806
1806
# This is called by url_for, which can be called outside a
1807
1807
# request, can't use request.blueprints.
1808
1808
bps = _split_blueprint_path (endpoint .rpartition ("." )[0 ])
1809
- bp_funcs = chain .from_iterable (self .url_default_functions [bp ] for bp in bps )
1809
+ bp_funcs = chain .from_iterable (
1810
+ self .url_default_functions [bp ] for bp in reversed (bps )
1811
+ )
1810
1812
funcs = chain (funcs , bp_funcs )
1811
1813
1812
1814
for func in funcs :
@@ -1846,19 +1848,17 @@ def preprocess_request(self) -> t.Optional[ResponseReturnValue]:
1846
1848
further request handling is stopped.
1847
1849
"""
1848
1850
1849
- funcs : t .Iterable [URLValuePreprocessorCallable ] = self .url_value_preprocessors [
1850
- None
1851
- ]
1852
- for bp in request .blueprints :
1853
- if bp in self .url_value_preprocessors :
1854
- funcs = chain (funcs , self .url_value_preprocessors [bp ])
1851
+ funcs : t .Iterable [URLValuePreprocessorCallable ] = []
1852
+ for name in chain ([None ], reversed (request .blueprints )):
1853
+ if name in self .url_value_preprocessors :
1854
+ funcs = chain (funcs , self .url_value_preprocessors [name ])
1855
1855
for func in funcs :
1856
1856
func (request .endpoint , request .view_args )
1857
1857
1858
- funcs : t .Iterable [BeforeRequestCallable ] = self . before_request_funcs [ None ]
1859
- for bp in request .blueprints :
1860
- if bp in self .before_request_funcs :
1861
- funcs = chain (funcs , self .before_request_funcs [bp ])
1858
+ funcs : t .Iterable [BeforeRequestCallable ] = [ ]
1859
+ for name in chain ([ None ], reversed ( request .blueprints )) :
1860
+ if name in self .before_request_funcs :
1861
+ funcs = chain (funcs , self .before_request_funcs [name ])
1862
1862
for func in funcs :
1863
1863
rv = self .ensure_sync (func )()
1864
1864
if rv is not None :
@@ -1881,11 +1881,9 @@ def process_response(self, response: Response) -> Response:
1881
1881
"""
1882
1882
ctx = _request_ctx_stack .top
1883
1883
funcs : t .Iterable [AfterRequestCallable ] = ctx ._after_request_functions
1884
- for bp in request .blueprints :
1885
- if bp in self .after_request_funcs :
1886
- funcs = chain (funcs , reversed (self .after_request_funcs [bp ]))
1887
- if None in self .after_request_funcs :
1888
- funcs = chain (funcs , reversed (self .after_request_funcs [None ]))
1884
+ for name in chain (request .blueprints , [None ]):
1885
+ if name in self .after_request_funcs :
1886
+ funcs = chain (funcs , reversed (self .after_request_funcs [name ]))
1889
1887
for handler in funcs :
1890
1888
response = self .ensure_sync (handler )(response )
1891
1889
if not self .session_interface .is_null_session (ctx .session ):
@@ -1917,12 +1915,10 @@ def do_teardown_request(
1917
1915
"""
1918
1916
if exc is _sentinel :
1919
1917
exc = sys .exc_info ()[1 ]
1920
- funcs : t .Iterable [TeardownCallable ] = reversed (
1921
- self .teardown_request_funcs [None ]
1922
- )
1923
- for bp in request .blueprints :
1924
- if bp in self .teardown_request_funcs :
1925
- funcs = chain (funcs , reversed (self .teardown_request_funcs [bp ]))
1918
+ funcs : t .Iterable [TeardownCallable ] = []
1919
+ for name in chain (request .blueprints , [None ]):
1920
+ if name in self .teardown_request_funcs :
1921
+ funcs = chain (funcs , reversed (self .teardown_request_funcs [name ]))
1926
1922
for func in funcs :
1927
1923
self .ensure_sync (func )(exc )
1928
1924
request_tearing_down .send (self , exc = exc )
0 commit comments