Skip to content

Commit f8e576b

Browse files
authored
pythonGH-91389: Fix dis position information for CACHEs (pythonGH-93663)
1 parent 4f85cec commit f8e576b

File tree

3 files changed

+58
-12
lines changed

3 files changed

+58
-12
lines changed

Lib/dis.py

+22-10
Original file line numberDiff line numberDiff line change
@@ -497,17 +497,29 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
497497
yield Instruction(_all_opname[op], op,
498498
arg, argval, argrepr,
499499
offset, starts_line, is_jump_target, positions)
500-
if show_caches and _inline_cache_entries[deop]:
501-
for name, caches in _cache_format[opname[deop]].items():
502-
data = code[offset + 2: offset + 2 + caches * 2]
503-
argrepr = f"{name}: {int.from_bytes(data, sys.byteorder)}"
504-
for _ in range(caches):
505-
offset += 2
506-
yield Instruction(
507-
"CACHE", 0, 0, None, argrepr, offset, None, False, None
508-
)
509-
# Only show the actual value for the first cache entry:
500+
caches = _inline_cache_entries[deop]
501+
if not caches:
502+
continue
503+
if not show_caches:
504+
# We still need to advance the co_positions iterator:
505+
for _ in range(caches):
506+
next(co_positions, ())
507+
continue
508+
for name, size in _cache_format[opname[deop]].items():
509+
for i in range(size):
510+
offset += 2
511+
# Only show the fancy argrepr for a CACHE instruction when it's
512+
# the first entry for a particular cache value and the
513+
# instruction using it is actually quickened:
514+
if i == 0 and op != deop:
515+
data = code[offset: offset + 2 * size]
516+
argrepr = f"{name}: {int.from_bytes(data, sys.byteorder)}"
517+
else:
510518
argrepr = ""
519+
yield Instruction(
520+
"CACHE", CACHE, 0, None, argrepr, offset, None, False,
521+
Positions(*next(co_positions, ()))
522+
)
511523

512524
def disassemble(co, lasti=-1, *, file=None, show_caches=False, adaptive=False):
513525
"""Disassemble a code object."""

Lib/test/test_dis.py

+34-2
Original file line numberDiff line numberDiff line change
@@ -1197,8 +1197,10 @@ def test_show_caches(self):
11971197
caches = list(self.get_cached_values(quickened, adaptive))
11981198
for cache in caches:
11991199
self.assertRegex(cache, pattern)
1200-
self.assertEqual(caches.count(""), 8)
1201-
self.assertEqual(len(caches), 22)
1200+
total_caches = 22
1201+
empty_caches = 8 if adaptive and quickened else total_caches
1202+
self.assertEqual(caches.count(""), empty_caches)
1203+
self.assertEqual(len(caches), total_caches)
12021204

12031205

12041206
class DisWithFileTests(DisTests):
@@ -1751,6 +1753,36 @@ def test_co_positions_missing_info(self):
17511753
self.assertIsNone(positions.col_offset)
17521754
self.assertIsNone(positions.end_col_offset)
17531755

1756+
@requires_debug_ranges()
1757+
def test_co_positions_with_lots_of_caches(self):
1758+
def roots(a, b, c):
1759+
d = b**2 - 4 * a * c
1760+
yield (-b - cmath.sqrt(d)) / (2 * a)
1761+
if d:
1762+
yield (-b + cmath.sqrt(d)) / (2 * a)
1763+
code = roots.__code__
1764+
ops = code.co_code[::2]
1765+
cache_opcode = opcode.opmap["CACHE"]
1766+
caches = sum(op == cache_opcode for op in ops)
1767+
non_caches = len(ops) - caches
1768+
# Make sure we have "lots of caches". If not, roots should be changed:
1769+
assert 1 / 3 <= caches / non_caches, "this test needs more caches!"
1770+
for show_caches in (False, True):
1771+
for adaptive in (False, True):
1772+
with self.subTest(f"{adaptive=}, {show_caches=}"):
1773+
co_positions = [
1774+
positions
1775+
for op, positions in zip(ops, code.co_positions(), strict=True)
1776+
if show_caches or op != cache_opcode
1777+
]
1778+
dis_positions = [
1779+
instruction.positions
1780+
for instruction in dis.get_instructions(
1781+
code, adaptive=adaptive, show_caches=show_caches
1782+
)
1783+
]
1784+
self.assertEqual(co_positions, dis_positions)
1785+
17541786
# get_instructions has its own tests above, so can rely on it to validate
17551787
# the object oriented API
17561788
class BytecodeTests(InstructionTestCase, DisTestBase):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix an issue where :mod:`dis` utilities could report missing or incorrect
2+
position information in the presence of ``CACHE`` entries.

0 commit comments

Comments
 (0)