Skip to content

gh-117270: Add missed change to COMPARE_OP from 3.12 to dis documentation #117272

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Christopher-Chianelli
Copy link
Contributor

@Christopher-Chianelli Christopher-Chianelli commented Mar 26, 2024

@ghost
Copy link

ghost commented Mar 26, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Mar 26, 2024

GH-117274 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 only security fixes label Mar 26, 2024
Christopher-Chianelli added a commit to Christopher-Chianelli/cpython that referenced this pull request Mar 26, 2024
@@ -1239,6 +1239,9 @@ iterations of the loop.
``cmp_op[opname >> 5]``. If the fifth-lowest bit of ``opname`` is set
(``opname & 16``), the result should be coerced to ``bool``.

.. versionchanged:: 3.12
The cmp_op index is now stored in the four-highest bits of oparg instead of the four-lowest bits of oparg.
Copy link
Member

Choose a reason for hiding this comment

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

I don't believe this is true. The index is not stored in the most significant bits of oparg and to be honest it's not easy to define most significant bits in Python's integer - it's not fixed length.

Copy link
Member

Choose a reason for hiding this comment

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

And I don't think the index is ever stored in the four lowest bits - that indicates we have 16 (or at least 9) operations.

Copy link
Member

Choose a reason for hiding this comment

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

Notice that even for 3.13, it did not mention the version change for the operation name shift. I think we can just leave it there and just fix the 3.12 docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For opcodes, the arg is a byte unless prefixed by EXTENDED_ARG; for 3.13, it the top 3 bits (hence the shift by 5), for 3.12, it the top 4 bits (with the MSB always 0, and hence, the shift by 4).

No strong opinion on including/excluding the versionchanged; I prefer having it as a record so people looking at the latest docs know code that a breaking change occurred on 3.12 for COMPARE_OP.

Copy link
Member

Choose a reason for hiding this comment

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

And that's a C-level implementation detail that's not exposed or documented. "Top X bits" only works when the variable is fixed-size, and when we are discussing in Python scope, opname is not fixed-size because it's an int. Even in C code, oparg is often declared as int or uint16_t in functions, and it would be super confusing when "top X bits" means the top X bits when it is a byte. I would assume that's why the documentation below used "the fifth-lowest bit" instead of "the fourth-highest bit" - counting from lowest is always well defined, but counting from the highest is not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would "The cmp_op index now starts at the fifth-lowest bit of oparg." be a better wording?

Copy link
Member

Choose a reason for hiding this comment

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

"start" is also ambiguous in my opinion. It's not clear whether the data goes "high" or "low" starting from a specific position. (That's why I think cmp_op[opname >> 5] is the best way to describe the fact as it is).

If we have to have a version-changed note, I would prefer something like "The cmp_op index is stored in bit(LSB maybe?) 7 to bit 4" or "The cmp_op index is stored in 8th lowest bit to 5th lowest bit". I believe the index is actually a 3-bit value instead of 4 but that might not be a big issue here. But we do have a 0-index vs 1-index issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe "the cmp_op index is now shifted 4 bits to the left"?

Yhg1s pushed a commit that referenced this pull request Aug 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting core review docs Documentation in the Doc dir skip news
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

3 participants