8000 GH-128534: Fix behavior of branch monitoring for `async for` by markshannon · Pull Request #130847 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-128534: Fix behavior of branch monitoring for async for #130847

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 7, 2025
Prev Previous commit
Next Next commit
Fix up dis to understand oparg of END_ASYNC_FOR
  • Loading branch information
markshannon committed Mar 4, 2025
commit e7f0db3e04a0c6a3a87b7a8bc92a5af6d73f14cc
7 changes: 5 additions & 2 deletions Lib/dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
STORE_FAST_STORE_FAST = opmap['STORE_FAST_STORE_FAST']
IS_OP = opmap['IS_OP']
CONTAINS_OP = opmap['CONTAINS_OP']
END_ASYNC_FOR = opmap['END_ASYNC_FOR']

CACHE = opmap["CACHE"]

Expand Down Expand Up @@ -605,7 +606,8 @@ def get_argval_argrepr(self, op, arg, offset):
argval = self.offset_from_jump_arg(op, arg, offset)
lbl = self.get_label_for_offset(argval)
assert lbl is not None
argrepr = f"to L{lbl}"
preposition = "from" if deop == END_ASYNC_FOR else "to"
argrepr = f"{preposition} L{lbl}"
elif deop in (LOAD_FAST_LOAD_FAST, STORE_FAST_LOAD_FAST, STORE_FAST_STORE_FAST):
arg1 = arg >> 4
arg2 = arg & 15
Expand Down Expand Up @@ -745,7 +747,8 @@ def _parse_exception_table(code):

def _is_backward_jump(op):
return opname[op] in ('JUMP_BACKWARD',
'JUMP_BACKWARD_NO_INTERRUPT')
'JUMP_BACKWARD_NO_INTERRUPT',
'END_ASYNC_FOR') # No really a jump, but it has a target

def _get_instructions_bytes(code, linestarts=None, line_offset=0, co_positions=None,
original_code=None, arg_resolver=None):
Expand Down
12 changes: 9 additions & 3 deletions Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1256,15 +1256,21 @@ def test__try_compile_no_context_exc_on_error(self):
except Exception as e:
self.assertIsNone(e.__context__)

def test_async_for(self):
def test_async_for_presentation(self):

async def afunc():
async for letter in async_iter1:
l2
l3

expected = ""
self.do_disassembly_test(afunc, expected)
disassembly = self.get_disassembly(afunc)
for line in disassembly.split("\n"):
if "END_ASYNC_FOR" in line:
break
else:
self.fail("No END_ASYNC_FOR in disassembly of async for")
self.assertNotIn("to", line)
self.assertIn("from", line)


@staticmethod
Expand Down
0