8
8
"""
9
9
10
10
try :
11
- from typing import Optional , Dict , Union , Tuple , Callable
11
+ from typing import Optional , Dict , Union , Tuple
12
12
from socket import socket
13
13
from socketpool import SocketPool
14
14
except ImportError :
29
29
from .headers import Headers
30
30
31
31
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
-
47
32
class Response :
48
33
"""
49
34
Response to a given `Request`. Use in `Server.route` handler functions.
@@ -162,7 +147,11 @@ def _send_headers(
162
147
self .request .connection , response_message_header .encode ("utf-8" )
163
148
)
164
149
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
+
166
155
def send (
167
156
self ,
168
157
body : str = "" ,
@@ -174,6 +163,7 @@ def send(
174
163
175
164
Should be called **only once** per response.
176
165
"""
166
+ self ._check_if_not_already_sent ()
177
167
178
168
if getattr (body , "encode" , None ):
179
169
encoded_response_message_body = body .encode ("utf-8" )
@@ -214,7 +204,6 @@ def _get_file_length(file_path: str) -> int:
214
204
except OSError :
215
205
raise FileNotExistsError (file_path ) # pylint: disable=raise-missing-from
216
206
217
- @_prevent_multiple_send_calls
218
207
def send_file ( # pylint: disable=too-many-arguments
219
208
self ,
220
209
filename : str = "index.html" ,
@@ -230,6 +219,7 @@ def send_file( # pylint: disable=too-many-arguments
230
219
231
220
Should be called **only once** per response.
232
221
"""
222
+ self ._check_if_not_already_sent ()
233
223
234
224
if safe :
235
225
self ._check_file_path_is_valid (filename )
0 commit comments