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

gh-121115: Skip __index__ in PyLong_AsNativeBytes by default #121118

Merged
merged 4 commits into from
Jun 28, 2024

Conversation

zooba
Copy link
Member

@zooba zooba commented Jun 28, 2024

Co-authored-by: Nice Zombies <nineteendo19d0@gmail.com>
passed, its :meth:`~object.__index__` method will be called first. This may
result in Python code executing and other threads being allowed to run, which
could cause changes to other objects or values in use. When *flags* is
``-1``, this option is not set, and non-integer values will raise
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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() and PyArg_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.

Copy link
Member Author

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?

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.

__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.

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.)

Copy link
Member

@serhiy-storchaka serhiy-storchaka Jun 28, 2024

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.

Copy link
Member Author

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.

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM

@zooba zooba merged commit 2894aa1 into python:main Jun 28, 2024
38 checks passed
@zooba zooba deleted the gh-121115 branch June 28, 2024 15:26
@zooba zooba added the needs backport to 3.13 bugs and security fixes label Jun 28, 2024
@miss-islington-app
Copy link

Thanks @zooba for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 28, 2024
…ythonGH-121118)

(cherry picked from commit 2894aa1)

Co-authored-by: Steve Dower <steve.dower@python.org>
@bedevere-app
Copy link

bedevere-app bot commented Jun 28, 2024

GH-121133 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Jun 28, 2024
zooba added a commit that referenced this pull request Jun 28, 2024
(cherry picked from commit 2894aa1)

Co-authored-by: Steve Dower <steve.dower@python.org>
noahbkim pushed a commit to hudson-trading/cpython that referenced this pull request Jul 11, 2024
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.

4 participants