From 88ff49f3c0994356baa3760e19524a250b6f48ab Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Fri, 6 May 2022 09:40:35 -0700 Subject: [PATCH 1/3] Move CACHE handling into _unpack_opargs --- Lib/dis.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/Lib/dis.py b/Lib/dis.py index 53c62694decaa0..7fae5b934ef37b 100644 --- a/Lib/dis.py +++ b/Lib/dis.py @@ -1,5 +1,6 @@ """Disassembler of Python byte code into mnemonics.""" +from distutils.command.build_ext import extension_name_re import sys import types import collections @@ -440,11 +441,7 @@ def _get_instructions_bytes(code, varname_from_oparg=None, for i in range(start, end): labels.add(target) starts_line = None - cache_counter = 0 for offset, op, arg in _unpack_opargs(code): - if cache_counter > 0: - cache_counter -= 1 - continue if linestarts is not None: starts_line = linestarts.get(offset, None) if starts_line is not None: @@ -454,7 +451,6 @@ def _get_instructions_bytes(code, varname_from_oparg=None, argrepr = '' positions = Positions(*next(co_positions, ())) deop = _deoptop(op) - cache_counter = _inline_cache_entries[deop] if arg is not None: # Set argval to the dereferenced value of the argument when # available, and argrepr to the string representation of argval. @@ -497,7 +493,7 @@ def _get_instructions_bytes(code, varname_from_oparg=None, yield Instruction(_all_opname[op], op, arg, argval, argrepr, offset, starts_line, is_jump_target, positions) - if show_caches and cache_counter: + if show_caches and _inline_cache_entries[deop]: for name, caches in _cache_format[opname[deop]].items(): data = code[offset + 2: offset + 2 + caches * 2] argrepr = f"{name}: {int.from_bytes(data, sys.byteorder)}" @@ -586,9 +582,16 @@ def _disassemble_str(source, **kwargs): def _unpack_opargs(code): extended_arg = 0 + caches = 0 for i in range(0, len(code), 2): + # Skip inline CACHE entries: + if caches: + caches -= 1 + continue op = code[i] - if _deoptop(op) >= HAVE_ARGUMENT: + deop = _deoptop(op) + caches = _inline_cache_entries[deop] + if deop >= HAVE_ARGUMENT: arg = code[i+1] | extended_arg extended_arg = (arg << 8) if op == EXTENDED_ARG else 0 # The oparg is stored as a signed integer From 9605dd056d4d1149672e91881487ff2678566568 Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Fri, 6 May 2022 09:44:05 -0700 Subject: [PATCH 2/3] Remove auto-added import --- Lib/dis.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/dis.py b/Lib/dis.py index 7fae5b934ef37b..046013120b000d 100644 --- a/Lib/dis.py +++ b/Lib/dis.py @@ -1,6 +1,5 @@ """Disassembler of Python byte code into mnemonics.""" -from distutils.command.build_ext import extension_name_re import sys import types import collections From 20a8e09197e2d22acb57f377cf6199d757a39975 Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Fri, 6 May 2022 09:48:22 -0700 Subject: [PATCH 3/3] blurb add --- .../next/Library/2022-05-06-09-48-07.gh-issue-90997.4PmCgX.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2022-05-06-09-48-07.gh-issue-90997.4PmCgX.rst diff --git a/Misc/NEWS.d/next/Library/2022-05-06-09-48-07.gh-issue-90997.4PmCgX.rst b/Misc/NEWS.d/next/Library/2022-05-06-09-48-07.gh-issue-90997.4PmCgX.rst new file mode 100644 index 00000000000000..0e683070806c2a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-05-06-09-48-07.gh-issue-90997.4PmCgX.rst @@ -0,0 +1,2 @@ +Fix an issue where :mod:`dis` utilities may interpret populated inline cache +entries as valid instructions.