@@ -693,7 +693,9 @@ def test_cli_runner(self, **kwargs: t.Any) -> FlaskCliRunner:
693
693
return cls (self , ** kwargs ) # type: ignore
694
694
695
695
def handle_http_exception (
696
- self , e : HTTPException
696
+ self ,
697
+ e : HTTPException ,
698
+ ctx : RequestContext ,
697
699
) -> HTTPException | ft .ResponseReturnValue :
698
700
"""Handles an HTTP exception. By default this will invoke the
699
701
registered error handlers and fall back to returning the
@@ -722,13 +724,15 @@ def handle_http_exception(
722
724
if isinstance (e , RoutingException ):
723
725
return e
724
726
725
- handler = self ._find_error_handler (e , request .blueprints )
727
+ handler = self ._find_error_handler (e , ctx . request .blueprints )
726
728
if handler is None :
727
729
return e
728
730
return self .ensure_sync (handler )(e )
729
731
730
732
def handle_user_exception (
731
- self , e : Exception
733
+ self ,
734
+ e : Exception ,
735
+ ctx : RequestContext ,
732
736
) -> HTTPException | ft .ResponseReturnValue :
733
737
"""This method is called whenever an exception occurs that
734
738
should be handled. A special case is :class:`~werkzeug
@@ -750,16 +754,16 @@ def handle_user_exception(
750
754
e .show_exception = True
751
755
752
756
if isinstance (e , HTTPException ) and not self .trap_http_exception (e ):
753
- return self .handle_http_exception (e )
757
+ return self .handle_http_exception (e , ctx )
754
758
755
- handler = self ._find_error_handler (e , request .blueprints )
759
+ handler = self ._find_error_handler (e , ctx . request .blueprints )
756
760
757
761
if handler is None :
758
762
raise
759
763
760
764
return self .ensure_sync (handler )(e )
761
765
762
- def handle_exception (self , e : Exception ) -> Response :
766
+ def handle_exception (self , e : Exception , ctx : RequestContext ) -> Response :
763
767
"""Handle an exception that did not have an error handler
764
768
associated with it, or that was raised from an error handler.
765
769
This always causes a 500 ``InternalServerError``.
@@ -802,19 +806,20 @@ def handle_exception(self, e: Exception) -> Response:
802
806
803
807
raise e
804
808
805
- self .log_exception (exc_info )
809
+ self .log_exception (exc_info , ctx )
806
810
server_error : InternalServerError | ft .ResponseReturnValue
807
811
server_error = InternalServerError (original_exception = e )
808
- handler = self ._find_error_handler (server_error , request .blueprints )
812
+ handler = self ._find_error_handler (server_error , ctx . request .blueprints )
809
813
810
814
if handler is not None :
811
815
server_error = self .ensure_sync (handler )(server_error )
812
816
813
- return self .finalize_request (server_error , from_error_handler = True )
817
+ return self .finalize_request (server_error , ctx , from_error_handler = True )
814
818
815
819
def log_exception (
816
820
self ,
817
821
exc_info : (tuple [type , BaseException , TracebackType ] | tuple [None , None , None ]),
822
+ ctx : RequestContext ,
818
823
) -> None :
819
824
"""Logs an exception. This is called by :meth:`handle_exception`
820
825
if debugging is disabled and right before the handler is called.
@@ -824,10 +829,10 @@ def log_exception(
824
829
.. versionadded:: 0.8
825
830
"""
826
831
self .logger .error (
827
- f"Exception on { request .path } [{ request .method } ]" , exc_info = exc_info
832
+ f"Exception on { ctx . request .path } [{ ctx . request .method } ]" , exc_info = exc_info
828
833
)
829
834
830
- def dispatch_request (self ) -> ft .ResponseReturnValue :
835
+ def dispatch_request (self , ctx : RequestContext ) -> ft .ResponseReturnValue :
831
836
"""Does the request dispatching. Matches the URL and returns the
832
837
return value of the view or error handler. This does not have to
833
838
be a response object. In order to convert the return value to a
@@ -837,22 +842,21 @@ def dispatch_request(self) -> ft.ResponseReturnValue:
837
842
This no longer does the exception handling, this code was
838
843
moved to the new :meth:`full_dispatch_request`.
839
844
"""
840
- req = request_ctx .request
841
- if req .routing_exception is not None :
842
- self .raise_routing_exception (req )
843
- rule : Rule = req .url_rule # type: ignore[assignment]
845
+ if ctx .request .routing_exception is not None :
846
+ self .raise_routing_exception (ctx .request )
847
+ rule : Rule = ctx .request .url_rule # type: ignore[assignment]
844
848
# if we provide automatic options for this URL and the
845
849
# request came with the OPTIONS method, reply automatically
846
850
if (
847
851
getattr (rule , "provide_automatic_options" , False )
848
- and req .method == "OPTIONS"
852
+ and ctx . request .method == "OPTIONS"
849
853
):
850
854
return self .make_default_options_response ()
851
855
# otherwise dispatch to the handler for that endpoint
852
- view_args : dict [str , t .Any ] = req .view_args # type: ignore[assignment]
856
+ view_args : dict [str , t .Any ] = ctx . request .view_args # type: ignore[assignment]
853
857
return self .ensure_sync (self .view_functions [rule .endpoint ])(** view_args )
854
858
855
- def full_dispatch_request (self ) -> Response :
859
+ def full_dispatch_request (self , ctx : RequestContext ) -> Response :
856
860
"""Dispatches the request and on top of that performs request
857
861
pre and postprocessing as well as HTTP exception catching and
858
862
error handling.
@@ -863,16 +867,17 @@ def full_dispatch_request(self) -> Response:
863
867
864
868
try :
865
869
request_started .send (self , _async_wrapper = self .ensure_sync )
866
- rv = self .preprocess_request ()
870
+ rv = self .preprocess_request (ctx )
867
871
if rv is None :
868
- rv = self .dispatch_request ()
872
+ rv = self .dispatch_request (ctx )
869
873
except Exception as e :
870
- rv = self .handle_user_exception (e )
871
- return self .finalize_request (rv )
874
+ rv = self .handle_user_exception (e , ctx )
875
+ return self .finalize_request (rv , ctx )
872
876
873
877
def finalize_request (
874
878
self ,
875
879
rv : ft .ResponseReturnValue | HTTPException ,
880
+ ctx : RequestContext ,
876
881
from_error_handler : bool = False ,
877
882
) -> Response :
878
883
"""Given the return value from a view function this finalizes
@@ -889,7 +894,7 @@ def finalize_request(
889
894
"""
890
895
response = self .make_response (rv )
891
896
try :
892
- response = self .process_response (response )
897
+ response = self .process_response (response , ctx )
893
898
request_finished .send (
894
899
self , _async_wrapper = self .ensure_sync , response = response
895
900
)
@@ -1216,7 +1221,7 @@ def make_response(self, rv: ft.ResponseReturnValue) -> Response:
1216
1221
1217
1222
return rv
1218
1223
1219
- def preprocess_request (self ) -> ft .ResponseReturnValue | None :
1224
+ def preprocess_request (self , ctx : RequestContext ) -> ft .ResponseReturnValue | None :
1220
1225
"""Called before the request is dispatched. Calls
1221
1226
:attr:`url_value_preprocessors` registered with the app and the
1222
1227
current blueprint (if any). Then calls :attr:`before_request_funcs`
@@ -1226,12 +1231,12 @@ def preprocess_request(self) -> ft.ResponseReturnValue | None:
1226
1231
value is handled as if it was the return value from the view, and
1227
1232
further request handling is stopped.
1228
1233
"""
1229
- names = (None , * reversed (request .blueprints ))
1234
+ names = (None , * reversed (ctx . request .blueprints ))
1230
1235
1231
1236
for name in names :
1232
1237
if name in self .url_value_preprocessors :
1233
1238
for url_func in self .url_value_preprocessors [name ]:
1234
- url_func (request .endpoint , request .view_args )
1239
+ url_func (ctx . request .endpoint , ctx . request .view_args )
1235
1240
1236
1241
for name in names :
1237
1242
if name in self .before_request_funcs :
@@ -1243,7 +1248,7 @@ def preprocess_request(self) -> ft.ResponseReturnValue | None:
1243
1248
1244
1249
return None
1245
1250
1246
- def process_response (self , response : Response ) -> Response :
1251
+ def process_response (self , response : Response , ctx : RequestContext ) -> Response :
1247
1252
"""Can be overridden in order to modify the response object
1248
1253
before it's sent to the WSGI server. By default this will
1249
1254
call all the :meth:`after_request` decorated functions.
@@ -1256,23 +1261,25 @@ def process_response(self, response: Response) -> Response:
1256
1261
:return: a new response object or the same, has to be an
1257
1262
instance of :attr:`response_class`.
1258
1263
"""
1259
- ctx = request_ctx ._get_current_object () # type: ignore[attr-defined]
1260
-
1261
1264
for func in ctx ._after_request_functions :
1262
1265
response = self .ensure_sync (func )(response )
1263
1266
1264
- for name in chain (request .blueprints , (None ,)):
1267
+ for name in chain (ctx . request .blueprints , (None ,)):
1265
1268
if name in self .after_request_funcs :
1266
1269
for func in reversed (self .after_request_funcs [name ]):
1267
1270
response = self .ensure_sync (func )(response )
1268
1271
1269
1272
if not self .session_interface .is_null_session (ctx .session ):
1270
- self .session_interface .save_session (self , ctx .session , response )
1273
+ self .session_interface .save_session (
1274
+ self , ctx .session , response # type: ignore[arg-type]
1275
+ )
1271
1276
1272
1277
return response
1273
1278
1274
1279
def do_teardown_request (
1275
- self , exc : BaseException | None = _sentinel # type: ignore
1280
+ self ,
1281
+ ctx : RequestContext ,
1282
+ exc : BaseException | None = _sentinel , # type: ignore
1276
1283
) -> None :
1277
1284
"""Called after the request is dispatched and the response is
1278
1285
returned, right before the request context is popped.
@@ -1297,7 +1304,7 @@ def do_teardown_request(
1297
1304
if exc is _sentinel :
1298
1305
exc = sys .exc_info ()[1 ]
1299
1306
1300
- for name in chain (request .blueprints , (None ,)):
1307
+ for name in chain (ctx . request .blueprints , (None ,)):
1301
1308
if name in self .teardown_request_funcs :
1302
1309
for func in reversed (self .teardown_request_funcs [name ]):
1303
1310
self .ensure_sync (func )(exc )
@@ -1452,10 +1459,10 @@ def wsgi_app(self, environ: dict, start_response: t.Callable) -> t.Any:
1452
1459
try :
1453
1460
try :
1454
1461
ctx .push ()
1455
- response = self .full_dispatch_request ()
1462
+ response = self .full_dispatch_request (ctx )
1456
1463
except Exception as e :
1457
1464
error = e
1458
- response = self .handle_exception (e )
1465
+ response = self .handle_exception (e , ctx )
1459
1466
except : # noqa: B001
1460
1467
error = sys .exc_info ()[1 ]
1461
1468
raise
0 commit comments