-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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
gh-121115: Skip __index__ in PyLong_AsNativeBytes by default #121118
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Core and Builtins/2024-06-28-10-02-58.gh-issue-121115.EeSLfc.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
:c:func:`PyLong_AsNativeBytes` no longer uses :meth:`~object.__index__` | ||
methods by default. The ``Py_ASNATIVEBYTES_ALLOW_INDEX`` flag has been added | ||
to allow it. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would expect it to be set by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default behaviour is defined as "most like C", and this is unlike C. Having it enabled by default also risks causing crashes, whereas having it not set only results in a TypeError.
But I initially started expecting it to be set by default too. It only took a bit of thinking about it to convince myself otherwise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does "most like C" mean in context of conversion from Python integer to C integer? I expected it to be like most common converters
PyLong_AsLong()
andPyArg_Parse("i")
, but seems I understand it wrong.__index__
should be called by default, because the purpose of the__index__
method is that integer-like object should be able to substitute a real int in almost all cases.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's deliberately vague, but a C cast is not going to trigger other code to run (unlike a C++ cast), and it's going to succeed even if it truncated the value and lost information. There are ways to detect this, and the existing functions force you to detect it, but
AsNativeBytes
deliberately allows you to bypass those if you know what you're doing.As a low level API, this can go in the "almost" situation. If this goes in, then Victor's PR with higher-level APIs for specific types should start passing this flag to get that behaviour.
As I said above, if you call this wrong and the default is to reenter Python, you'll get a hard crash. If you call this wrong and the default is to raise TypeError, you get a safe exception. We should choose the safe path for the default. (I think we should choose it for the other APIs as well, but am willing to concede different logic for high/low level APIs.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My concern is that users which will use this API for concrete C integer type will miss such detail and produce extensions that only work with
int
and subclasses, but not with other integer-like objects.I have no objection if this is the intention.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's the intent. Their users will get a
TypeError
when it matters and can request a change.