Skip to content

Commit e7f0db3

Browse files
committed
Fix up dis to understand oparg of END_ASYNC_FOR
1 parent 4c20ae6 commit e7f0db3

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

Lib/dis.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
STORE_FAST_STORE_FAST = opmap['STORE_FAST_STORE_FAST']
5353
IS_OP = opmap['IS_OP']
5454
CONTAINS_OP = opmap['CONTAINS_OP']
55+
END_ASYNC_FOR = opmap['END_ASYNC_FOR']
5556

5657
CACHE = opmap["CACHE"]
5758

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

746748
def _is_backward_jump(op):
747749
return opname[op] in ('JUMP_BACKWARD',
748-
'JUMP_BACKWARD_NO_INTERRUPT')
750+
'JUMP_BACKWARD_NO_INTERRUPT',
751+
'END_ASYNC_FOR') # No really a jump, but it has a target
749752

750753
def _get_instructions_bytes(code, linestarts=None, line_offset=0, co_positions=None,
751754
original_code=None, arg_resolver=None):

Lib/test/test_dis.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,15 +1256,21 @@ def test__try_compile_no_context_exc_on_error(self):
12561256
except Exception as e:
12571257
self.assertIsNone(e.__context__)
12581258

1259-
def test_async_for(self):
1259+
def test_async_for_presentation(self):
12601260

12611261
async def afunc():
12621262
async for letter in async_iter1:
12631263
l2
12641264
l3
12651265

1266-
expected = ""
1267-
self.do_disassembly_test(afunc, expected)
1266+
disassembly = self.get_disassembly(afunc)
1267+
for line in disassembly.split("\n"):
1268+
if "END_ASYNC_FOR" in line:
1269+
break
1270+
else:
1271+
self.fail("No END_ASYNC_FOR in disassembly of async for")
1272+
self.assertNotIn("to", line)
1273+
self.assertIn("from", line)
12681274

12691275

12701276
@staticmethod

0 commit comments

Comments
 (0)