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
Open
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
3 changes: 3 additions & 0 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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"?


.. versionchanged:: 3.13
The fifth-lowest bit of the oparg now indicates a forced conversion to
:class:`bool`.
Expand Down