X Tutup
The Wayback Machine - https://web.archive.org/web/20220220175749/https://github.com/python/cpython/pull/31285
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

@the-knights-who-say-ni the-knights-who-say-ni commented Feb 11, 2022

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 issue 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 issue 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

@saulshanabrook saulshanabrook commented Feb 13, 2022

@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

@JelleZijlstra JelleZijlstra commented Feb 13, 2022

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
Contributor

@markshannon markshannon Feb 16, 2022

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

@saulshanabrook saulshanabrook Feb 16, 2022

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
Contributor

@markshannon markshannon left a comment

One quibble about naming, otherwise looks good.

@saulshanabrook saulshanabrook force-pushed the fix-issue-46724-dis branch from 8ad2b32 to 2ababec Feb 16, 2022
@JelleZijlstra
Copy link
Member

@JelleZijlstra JelleZijlstra commented Feb 17, 2022

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

@saulshanabrook saulshanabrook commented Feb 17, 2022

@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
12 checks passed
@markshannon
Copy link
Contributor

@markshannon markshannon commented Feb 18, 2022

Thanks @saulshanabrook

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
X Tutup