File tree 2 files changed +31
-1
lines changed
2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -353,11 +353,18 @@ class CallableRequest(_typing.Generic[_core.T]):
353
353
_C2 = _typing .Callable [[CallableRequest [_typing .Any ]], _typing .Any ]
354
354
355
355
class _IterWithReturn :
356
+ """ Utility class to capture return statements from a generator """
356
357
def __init__ (self , iterable ):
357
358
self .iterable = iterable
358
359
359
360
def __iter__ (self ):
360
- self .value = yield from self .iterable
361
+ try :
362
+ self .value = yield from self .iterable
363
+ except RuntimeError as e :
364
+ if isinstance (e .__cause__ , StopIteration ):
365
+ self .value = e .__cause__ .value
366
+ else :
367
+ raise
361
368
362
369
def _on_call_handler (func : _C2 , request : Request ,
363
370
enforce_app_check : bool ) -> Response :
Original file line number Diff line number Diff line change @@ -148,6 +148,15 @@ def yield_thrower(req: https_fn.CallableRequest[int]):
148
148
"Can't read anymore"
149
149
)
150
150
151
+ @https_fn .on_call ()
152
+ def legacy_yielder (req : https_fn .CallableRequest [int ]):
153
+ yield from range (req .data )
154
+ # Prior to Python 3.3, this was the way "return" was handled
155
+ # Python 3.5 made this messy however because it converts
156
+ # raised StopIteration into a RuntimeError
157
+ # pylint: disable=stop-iteration-return
158
+ raise StopIteration ("OK" )
159
+
151
160
with app .test_request_context ("/" ):
152
161
environ = EnvironBuilder (
153
162
method = "POST" ,
@@ -162,6 +171,20 @@ def yield_thrower(req: https_fn.CallableRequest[int]):
162
171
self .assertEqual (response .status_code , 200 )
163
172
self .assertEqual (response .get_json (), { "result" : "OK" })
164
173
174
+ with app .test_request_context ("/" ):
175
+ environ = EnvironBuilder (
176
+ method = "POST" ,
177
+ json = {
178
+ "data" : 5
179
+ }
180
+ ).get_environ ()
181
+
182
+ request = Request (environ )
183
+ response = legacy_yielder (request )
184
+
185
+ self .assertEqual (response .status_code , 200 )
186
+ self .assertEqual (response .get_json (), { "result" : "OK" })
187
+
165
188
with app .test_request_context ("/" ):
166
189
environ = EnvironBuilder (
167
190
method = "POST" ,
You can’t perform that action at this time.
0 commit comments