Skip to content

Commit 82e0ecb

Browse files
committed
bpo-47233: Fix show_caches in dis module
1 parent 1c8b3b5 commit 82e0ecb

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

Lib/dis.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,9 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
413413
labels.add(target)
414414
starts_line = None
415415
for offset, op, arg in _unpack_opargs(code):
416+
# get the next position before we skip a CACHE instruction
417+
# to associate the correct position information.
418+
positions = Positions(*next(co_positions, ()))
416419
if not show_caches and op == CACHE:
417420
continue
418421
if linestarts is not None:
@@ -422,7 +425,6 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
422425
is_jump_target = offset in labels
423426
argval = None
424427
argrepr = ''
425-
positions = Positions(*next(co_positions, ()))
426428
if arg is not None:
427429
# Set argval to the dereferenced value of the argument when
428430
# available, and argrepr to the string representation of argval.

Lib/test/test_dis.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,5 +1618,23 @@ def get_disassembly(self, tb):
16181618
return output.getvalue()
16191619

16201620

1621+
class TestShowCaches(unittest.TestCase):
1622+
def test_show_caches(self):
1623+
def example_code():
1624+
# this code is never executed
1625+
a.b.c(1 * x)
1626+
if x:
1627+
c = 5
1628+
1629+
def get_instructions(show_caches):
1630+
return [
1631+
instr
1632+
for instr in dis.Bytecode(example_code, show_caches=show_caches)
1633+
if instr.opname != "CACHE"
1634+
]
1635+
1636+
self.assertEqual(get_instructions(True), get_instructions(False))
1637+
1638+
16211639
if __name__ == "__main__":
16221640
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix an issue where the default argument ``show_caches=False`` in :mod:`dis`
2+
utilities could result in incorrect position information.

0 commit comments

Comments
 (0)