Skip to content
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

bpo-46724: Fix dis support for overflow args #31285

Merged
merged 15 commits into from
Feb 18, 2022

Conversation

saulshanabrook
Copy link
Contributor

@saulshanabrook saulshanabrook commented Feb 11, 2022

This PR makes dis aware that currently Python's opargs wrap after a certain value. This is used in Python 3.10+ for code which includes negative args to represent negative jumps, like this:

while not (x < y < z):
    pass

which produces this dis output after this PR:

              0 RESUME                   0

  1           2 LOAD_NAME                0 (a)
              4 LOAD_NAME                1 (b)
              6 SWAP                     2
              8 COPY                     2
             10 COMPARE_OP               0 (<)
             12 POP_JUMP_IF_FALSE       11 (to 22)
             14 LOAD_NAME                2 (c)
             16 COMPARE_OP               0 (<)
             18 POP_JUMP_IF_TRUE        28 (to 56)
             20 JUMP_FORWARD             1 (to 24)
        >>   22 POP_TOP
        >>   24 LOAD_NAME                0 (a)
             26 LOAD_NAME                1 (b)
             28 SWAP                     2
             30 COPY                     2
             32 COMPARE_OP               0 (<)
             34 POP_JUMP_IF_FALSE       23 (to 46)
             36 LOAD_NAME                2 (c)
             38 COMPARE_OP               0 (<)
             40 POP_JUMP_IF_FALSE       12 (to 24)
             42 LOAD_CONST               0 (None)
             44 RETURN_VALUE
        >>   46 POP_TOP
             48 EXTENDED_ARG           255
             50 EXTENDED_ARG         65535
             52 EXTENDED_ARG         16777215
             54 JUMP_FORWARD           -16 (to 24)
        >>   56 LOAD_CONST               0 (None)
             58 RETURN_VALUE

Before this PR, the JUMP_FORWARD on line 54 had a huge value that did not match the Python interpreters behavior.

It is unclear to me whether producing this sort of bytecode is intended (I assume it's not), but at least with this fix the dis output more closes matches the current implementation.

https://bugs.python.org/issue46724

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

CLA Missing

Our records indicate the following people have not signed the CLA:

@saulshanabrook

For legal reasons we need all the people listed to sign the CLA before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

If you have recently signed the CLA, please wait at least one business day
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@saulshanabrook saulshanabrook changed the title bpo-46724: Fix dis support for overflow args bpo-46724: Fix dis support for negative args Feb 12, 2022
@saulshanabrook saulshanabrook changed the title bpo-46724: Fix dis support for negative args bpo-46724: Fix dis support for overflow args Feb 12, 2022
saulshanabrook added a commit to metadsl/python-code-data that referenced this pull request Feb 12, 2022
Adds support for negative relative jump args introduced in Python 3.10.

Ports corresponding fix from dis
python/cpython#31285
saulshanabrook added a commit to metadsl/python-code-data that referenced this pull request Feb 12, 2022
Adds support for negative relative jump args introduced in Python 3.10.

Ports corresponding fix from dis
python/cpython#31285
Lib/dis.py Outdated Show resolved Hide resolved
@saulshanabrook
Copy link
Contributor Author

@JelleZijlstra Thanks for taking a look.

Would you be able to approve the actions to run on this PR so I can see if the tests pass in CI?

@JelleZijlstra
Copy link
Member

Sorry, I don't have that power.

Lib/dis.py Outdated Show resolved Hide resolved
Lib/dis.py Outdated Show resolved Hide resolved
Lib/test/test_dis.py Outdated Show resolved Hide resolved
Lib/dis.py Outdated Show resolved Hide resolved
Lib/test/test_dis.py Outdated Show resolved Hide resolved
Lib/test/test_dis.py Outdated Show resolved Hide resolved
Lib/dis.py Outdated
# Rely on C `int` being 32 bits for oparg
_INT_BITS = 32
# Maximum value for a c int
_INT_MAX = 2 ** (_INT_BITS - 1)
Copy link
Member

Choose a reason for hiding this comment

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

This is when it overflows, not the maximum value.
Rather than messing around with subtracting one, I'd recommend renaming this to _INT_OVERFLOW.
The math is sound.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for catching, fixed!

Copy link
Member

@markshannon markshannon left a comment

Choose a reason for hiding this comment

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

One quibble about naming, otherwise looks good.

@JelleZijlstra
Copy link
Member

I removed the review requests. In the future, it's better to merge main into the branch instead of rebasing.

Thanks for updating it, I can take another look at the PR later.

@saulshanabrook
Copy link
Contributor Author

@JelleZijlstra thank you for removing them.

In the future, it's better to merge main into the branch instead of rebasing.

Will do.

Lib/test/test_dis.py Outdated Show resolved Hide resolved
@markshannon markshannon merged commit c3ce778 into python:main Feb 18, 2022
@markshannon
Copy link
Member

Thanks @saulshanabrook

@saulshanabrook saulshanabrook deleted the fix-issue-46724-dis branch February 18, 2022 13:36
@saulshanabrook saulshanabrook mannequin mentioned this pull request Jul 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants