aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorPaul Sokolovsky2017-10-21 12:13:44 +0300
committerPaul Sokolovsky2017-12-15 20:20:36 +0200
commit63644016669c173190c71d39fb33ad9502fe2b0f (patch)
treed92986fbcfdd6944792e852fc6d5023cb4c724d6 /tests
parentf4ed2dfa942339dc1f174e8a83ff0d41073f1972 (diff)
py/objgenerator: Allow to pend an exception for next execution.
This implements .pend_throw(exc) method, which sets up an exception to be triggered on the next call to generator's .__next__() or .send() method. This is unlike .throw(), which immediately starts to execute the generator to process the exception. This effectively adds Future-like capabilities to generator protocol (exception will be raised in the future). The need for such a method arised to implement uasyncio wait_for() function efficiently (its behavior is clearly "Future" like, and normally would require to introduce an expensive Future wrapper around all native couroutines, like upstream asyncio does). py/objgenerator: pend_throw: Return previous pended value. This effectively allows to store an additional value (not necessary an exception) in a coroutine while it's not being executed. uasyncio has exactly this usecase: to mark a coro waiting in I/O queue (and thus not executed in the normal scheduling queue), for the purpose of implementing wait_for() function (cancellation of such waiting coro by a timeout).
Diffstat (limited to 'tests')
-rw-r--r--tests/basics/generator_pend_throw.py26
-rw-r--r--tests/basics/generator_pend_throw.py.exp4
-rwxr-xr-xtests/run-tests2
3 files changed, 31 insertions, 1 deletions
diff --git a/tests/basics/generator_pend_throw.py b/tests/basics/generator_pend_throw.py
new file mode 100644
index 000000000..949655612
--- /dev/null
+++ b/tests/basics/generator_pend_throw.py
@@ -0,0 +1,26 @@
+def gen():
+ i = 0
+ while 1:
+ yield i
+ i += 1
+
+g = gen()
+
+try:
+ g.pend_throw
+except AttributeError:
+ print("SKIP")
+ raise SystemExit
+
+
+print(next(g))
+print(next(g))
+g.pend_throw(ValueError())
+
+v = None
+try:
+ v = next(g)
+except Exception as e:
+ print("raised", repr(e))
+
+print("ret was:", v)
diff --git a/tests/basics/generator_pend_throw.py.exp b/tests/basics/generator_pend_throw.py.exp
new file mode 100644
index 000000000..f9894a089
--- /dev/null
+++ b/tests/basics/generator_pend_throw.py.exp
@@ -0,0 +1,4 @@
+0
+1
+raised ValueError()
+ret was: None
diff --git a/tests/run-tests b/tests/run-tests
index 45cb1a746..8719befbd 100755
--- a/tests/run-tests
+++ b/tests/run-tests
@@ -337,7 +337,7 @@ def run_tests(pyb, tests, args, base_path="."):
# Some tests are known to fail with native emitter
# Remove them from the below when they work
if args.emit == 'native':
- skip_tests.update({'basics/%s.py' % t for t in 'gen_yield_from gen_yield_from_close gen_yield_from_ducktype gen_yield_from_exc gen_yield_from_iter gen_yield_from_send gen_yield_from_stopped gen_yield_from_throw gen_yield_from_throw2 gen_yield_from_throw3 generator1 generator2 generator_args generator_close generator_closure generator_exc generator_return generator_send'.split()}) # require yield
+ skip_tests.update({'basics/%s.py' % t for t in 'gen_yield_from gen_yield_from_close gen_yield_from_ducktype gen_yield_from_exc gen_yield_from_iter gen_yield_from_send gen_yield_from_stopped gen_yield_from_throw gen_yield_from_throw2 gen_yield_from_throw3 generator1 generator2 generator_args generator_close generator_closure generator_exc generator_pend_throw generator_return generator_send'.split()}) # require yield
skip_tests.update({'basics/%s.py' % t for t in 'bytes_gen class_store_class globals_del string_join'.split()}) # require yield
skip_tests.update({'basics/async_%s.py' % t for t in 'def await await2 for for2 with with2'.split()}) # require yield
skip_tests.update({'basics/%s.py' % t for t in 'try_reraise try_reraise2'.split()}) # require raise_varargs