Skip to content

gh-91389: Fix show_caches in dis module #32406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Lib/dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
labels.add(target)
starts_line = None
for offset, op, arg in _unpack_opargs(code):
# get the next position before we skip a CACHE instruction
# to associate the correct position information.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the CI is mad about this trailing whitespace. Didn't see it until now:

Suggested change
# to associate the correct position information.
# to associate the correct position information.

positions = Positions(*next(co_positions, ()))
if not show_caches and op == CACHE:
continue
if linestarts is not None:
Expand All @@ -422,7 +425,6 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
is_jump_target = offset in labels
argval = None
argrepr = ''
positions = Positions(*next(co_positions, ()))
if arg is not None:
# Set argval to the dereferenced value of the argument when
# available, and argrepr to the string representation of argval.
Expand Down
18 changes: 18 additions & 0 deletions Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1618,5 +1618,23 @@ def get_disassembly(self, tb):
return output.getvalue()


class TestShowCaches(unittest.TestCase):
def test_show_caches(self):
def example_code():
# this code is never executed
a.b.c(1 * x)
if x:
c = 5

def get_instructions(show_caches):
return [
instr
for instr in dis.Bytecode(example_code, show_caches=show_caches)
if instr.opname != "CACHE"
]

self.assertEqual(get_instructions(True), get_instructions(False))


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix an issue where the default argument ``show_caches=False`` in :mod:`dis`
utilities could result in incorrect position information.