Skip to content

Commit

Permalink
Core JIT coroutine implementation
Browse files Browse the repository at this point in the history
Summary:
Adds our custom version of the coroutine types for JIT.

Coroutines are mostly the same as generators in Python, with mostly superficial differences. Particularly they raise slightly differently worded errors or new errors in places where it doesn't make sense to use the languge-level syntax.

As with the generator implementation, we reuse most things from the core Python coroutine implemenation.

Reviewed By: DinoV

Differential Revision: D67425130

fbshipit-source-id: 8734adc88976d67612323d2cf504b0d53387a339
  • Loading branch information
jbower-fb authored and facebook-github-bot committed Dec 19, 2024
1 parent 3d1b81e commit 4f3babd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
12 changes: 8 additions & 4 deletions Lib/test/test_coroutines.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ async def asynciter(iterable):


def run_async(coro):
assert coro.__class__ in {types.GeneratorType, types.CoroutineType}
# TODO(T209747648): Find an upstreamable way to fix this
# assert coro.__class__ in {types.GeneratorType, types.CoroutineType}

buffer = []
result = None
Expand All @@ -50,7 +51,8 @@ def run_async(coro):


def run_async__await__(coro):
assert coro.__class__ is types.CoroutineType
# TODO(T209747648): Find an upstreamable way to fix this
# assert coro.__class__ is types.CoroutineType
aw = coro.__await__()
buffer = []
result = None
Expand Down Expand Up @@ -530,7 +532,8 @@ async def foo():
return 10

f = foo()
self.assertIsInstance(f, types.CoroutineType)
# TODO(T209747648): Find an upstreamable way to fix this
# self.assertIsInstance(f, types.CoroutineType)
self.assertTrue(bool(foo.__code__.co_flags & inspect.CO_COROUTINE))
self.assertFalse(bool(foo.__code__.co_flags & inspect.CO_GENERATOR))
self.assertTrue(bool(f.cr_code.co_flags & inspect.CO_COROUTINE))
Expand Down Expand Up @@ -1124,7 +1127,8 @@ def test_await_14(self):
class Wrapper:
# Forces the interpreter to use CoroutineType.__await__
def __init__(self, coro):
assert coro.__class__ is types.CoroutineType
# TODO(T209747648): Find an upstreamable way to fix this
# assert coro.__class__ is types.CoroutineType
self.coro = coro
def __await__(self):
return self.coro.__await__()
Expand Down
18 changes: 8 additions & 10 deletions Lib/test/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,21 +206,19 @@ def __del__(self):
finally:
gc.set_threshold(*thresholds)

# TODO(T209747648) - These should just be enabled as coroutines and
# async-generators work.
#
# TODO(T209747648) - This should be enabled when async-generators work.
# def test_ag_frame_f_back(self):
# async def f():
# yield
# ag = f()
# self.assertIsNone(ag.ag_frame.f_back)
#
# def test_cr_frame_f_back(self):
# async def f():
# pass
# cr = f()
# self.assertIsNone(cr.cr_frame.f_back)
# cr.close() # Suppress RuntimeWarning.

def test_cr_frame_f_back(self):
async def f():
pass
cr = f()
self.assertIsNone(cr.cr_frame.f_back)
cr.close() # Suppress RuntimeWarning.

def test_gi_frame_f_back(self):
def f():
Expand Down

0 comments on commit 4f3babd

Please sign in to comment.