Skip to content

Commit

Permalink
fixup! ZEO.asyncio: switch to async/await style
Browse files Browse the repository at this point in the history
@d-maurer notes at #218 (comment) :

    Why do we need this special __await__ logic? It is not present in #195 and nevertheless all uvloop tests passed.

and he is right - in 7f574ec I missed to do the full removal of py2
support code from CoroutineExecutor:

in py2 it was

	@coroutine
	def dothing1():
	    yield dothing2()

this way the yield for dothing2 was working this way:

- dothing2() called - it returns a coroutine object
- yield yields that coroutine object, not objects that second coroutine
  object would yield by itself. This is the difference in beween yield
  and `yield from`.
- CoroutineExecutor was noticing such yield and handling `yield coro()`
  with the same semantic as if it was `yield from coro()` by manually
  creating another AsyncTask.

but on py3 await has semantic of `yield from` and so we do not need to
keep all that `yield from` emulating code. 7f574ec dropped part of
that, but did not drop special handling of `yield coro()` if coro is
non-native custom coroutine function.

Let that code go as well.
  • Loading branch information
navytux committed Jan 19, 2023
1 parent e07be0a commit 32091b5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 44 deletions.
26 changes: 4 additions & 22 deletions src/ZEO/asyncio/_futures.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -272,29 +272,11 @@ cdef class CoroutineExecutor:
result._asyncio_future_blocking = False
await_next = result

# bad await
else:
# object with __await__ - e.g. @cython.iterable_coroutine used by uvloop
risawaitable = True
try:
rawait = result.__await__()
except AttributeError:
risawaitable = False
else:
# cython.iterable_coroutine returns `coroutine_wrapper` that mimics
# iterator/generator but does not inherit from types.GeneratorType .
await_next = AsyncTask(rawait, self.task.get_loop())

if not risawaitable:
# bare yield
if result is None:
await_next = Future(self.task.get_loop())
await_next.set_result(None)

# bad yield
else:
await_next = Future(self.task.get_loop())
await_next.set_exception(
RuntimeError("Task got bad yield: %r" % (result,)))
await_next = Future(self.task.get_loop())
await_next.set_exception(
RuntimeError("Task got bad await: %r" % (result,)))

if self.cancel_requested:
_cancel_future(await_next, self.cancel_msg)
Expand Down
26 changes: 4 additions & 22 deletions src/ZEO/asyncio/futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,29 +253,11 @@ def step(self):
result._asyncio_future_blocking = False
await_next = result

# bad await
else:
# object with __await__ - e.g. @cython.iterable_coroutine used by uvloop
risawaitable = True
try:
rawait = result.__await__()
except AttributeError:
risawaitable = False
else:
# cython.iterable_coroutine returns `coroutine_wrapper` that mimics
# iterator/generator but does not inherit from types.GeneratorType .
await_next = AsyncTask(rawait, self.task.get_loop())

if not risawaitable:
# bare yield
if result is None:
await_next = Future(self.task.get_loop())
await_next.set_result(None)

# bad yield
else:
await_next = Future(self.task.get_loop())
await_next.set_exception(
RuntimeError("Task got bad yield: %r" % (result,)))
await_next = Future(self.task.get_loop())
await_next.set_exception(
RuntimeError("Task got bad await: %r" % (result,)))

if self.cancel_requested:
_cancel_future(await_next, self.cancel_msg)
Expand Down

0 comments on commit 32091b5

Please sign in to comment.