Skip to content

Commit 3f61db4

Browse files
authored
pythongh-90997: Move CACHE handling into _unpack_opargs (python#92409)
* Move CACHE handling into _unpack_opargs * Remove auto-added import * blurb add
1 parent 5021064 commit 3f61db4

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

Lib/dis.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -440,11 +440,7 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
440440
for i in range(start, end):
441441
labels.add(target)
442442
starts_line = None
443-
cache_counter = 0
444443
for offset, op, arg in _unpack_opargs(code):
445-
if cache_counter > 0:
446-
cache_counter -= 1
447-
continue
448444
if linestarts is not None:
449445
starts_line = linestarts.get(offset, None)
450446
if starts_line is not None:
@@ -454,7 +450,6 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
454450
argrepr = ''
455451
positions = Positions(*next(co_positions, ()))
456452
deop = _deoptop(op)
457-
cache_counter = _inline_cache_entries[deop]
458453
if arg is not None:
459454
# Set argval to the dereferenced value of the argument when
460455
# available, and argrepr to the string representation of argval.
@@ -497,7 +492,7 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
497492
yield Instruction(_all_opname[op], op,
498493
arg, argval, argrepr,
499494
offset, starts_line, is_jump_target, positions)
500-
if show_caches and cache_counter:
495+
if show_caches and _inline_cache_entries[deop]:
501496
for name, caches in _cache_format[opname[deop]].items():
502497
data = code[offset + 2: offset + 2 + caches * 2]
503498
argrepr = f"{name}: {int.from_bytes(data, sys.byteorder)}"
@@ -586,9 +581,16 @@ def _disassemble_str(source, **kwargs):
586581

587582
def _unpack_opargs(code):
588583
extended_arg = 0
584+
caches = 0
589585
for i in range(0, len(code), 2):
586+
# Skip inline CACHE entries:
587+
if caches:
588+
caches -= 1
589+
continue
590590
op = code[i]
591-
if _deoptop(op) >= HAVE_ARGUMENT:
591+
deop = _deoptop(op)
592+
caches = _inline_cache_entries[deop]
593+
if deop >= HAVE_ARGUMENT:
592594
arg = code[i+1] | extended_arg
593595
extended_arg = (arg << 8) if op == EXTENDED_ARG else 0
594596
# The oparg is stored as a signed integer
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix an issue where :mod:`dis` utilities may interpret populated inline cache
2+
entries as valid instructions.

0 commit comments

Comments
 (0)