Skip to content

Add show_positions keyword argument to dis.dis and related functions #123165

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
markshannon opened this issue Aug 20, 2024 · 2 comments
Closed

Add show_positions keyword argument to dis.dis and related functions #123165

markshannon opened this issue Aug 20, 2024 · 2 comments
Assignees
Labels
easy stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@markshannon
Copy link
Member

markshannon commented Aug 20, 2024

Feature or enhancement

Proposal:

dis.dis is a useful debugging tool when trying to debug minor bytecode compiler errors.
However it lacks one important feature, the ability to see the exact positions attached to instructions. It can only show line numbers.

We should add a show_positions keyword argument to show positions.
For example, the function:

def foo(x):
    if x == 2:
        return 1

disassembles to:


  2           RESUME                   0

  3           LOAD_FAST                0 (x)
              LOAD_CONST               1 (2)
              COMPARE_OP              88 (bool(==))
              POP_JUMP_IF_FALSE        1 (to L1)

  4           RETURN_CONST             2 (1)

  3   L1:     RETURN_CONST             0 (None)

with show_positions it would disassemble to something like:

2:0-2:0                RESUME                   0

3:7-3:8                LOAD_FAST                0 (x)
3:12-3:13              LOAD_CONST               1 (2)
3:7-3:13               COMPARE_OP              88 (bool(==))
3:7-3:13               POP_JUMP_IF_FALSE        1 (to L1)

4:15-4:16              RETURN_CONST             2 (1)

3:7-3:13       L1:     RETURN_CONST             0 (None)

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

@markshannon markshannon added the type-feature A feature request or enhancement label Aug 20, 2024
@markshannon
Copy link
Member Author

@iritkatriel

@iritkatriel iritkatriel added easy stdlib Python modules in the Lib dir labels Aug 20, 2024
@picnixz
Copy link
Member

picnixz commented Aug 20, 2024

Since it's marked as an easy feature, could I perhaps work on this? (I took upon the task of doing it, so it will be ready in a few hours I think)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
easy stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants