88"""
99
1010try :
11- from typing import Optional , Dict , Union , Tuple , Callable
11+ from typing import Optional , Dict , Union , Tuple
1212 from socket import socket
1313 from socketpool import SocketPool
1414except ImportError :
2929from .headers import Headers
3030
3131
32- def _prevent_multiple_send_calls (function : Callable ):
33- """
34- Decorator that prevents calling ``send`` or ``send_file`` more than once.
35- """
36-
37- def wrapper (self : "Response" , * args , ** kwargs ):
38- if self ._response_already_sent : # pylint: disable=protected-access
39- raise ResponseAlreadySentError
40-
41- result = function (self , * args , ** kwargs )
42- return result
43-
44- return wrapper
45-
46-
4732class Response :
4833 """
4934 Response to a given `Request`. Use in `Server.route` handler functions.
@@ -162,7 +147,11 @@ def _send_headers(
162147 self .request .connection , response_message_header .encode ("utf-8" )
163148 )
164149
165- @_prevent_multiple_send_calls
150+ def _check_if_not_already_sent (self ) -> None :
151+ """Prevents calling ``send`` or ``send_file`` more than once."""
152+ if self ._response_already_sent :
153+ raise ResponseAlreadySentError
154+
166155 def send (
167156 self ,
168157 body : str = "" ,
@@ -174,6 +163,7 @@ def send(
174163
175164 Should be called **only once** per response.
176165 """
166+ self ._check_if_not_already_sent ()
177167
178168 if getattr (body , "encode" , None ):
179169 encoded_response_message_body = body .encode ("utf-8" )
@@ -214,7 +204,6 @@ def _get_file_length(file_path: str) -> int:
214204 except OSError :
215205 raise FileNotExistsError (file_path ) # pylint: disable=raise-missing-from
216206
217- @_prevent_multiple_send_calls
218207 def send_file ( # pylint: disable=too-many-arguments
219208 self ,
220209 filename : str = "index.html" ,
@@ -230,6 +219,7 @@ def send_file( # pylint: disable=too-many-arguments
230219
231220 Should be called **only once** per response.
232221 """
222+ self ._check_if_not_already_sent ()
233223
234224 if safe :
235225 self ._check_file_path_is_valid (filename )
0 commit comments