Skip to content

gh-132987: Support __index__() in the stat module #133097

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

Merged

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Apr 28, 2025

Use it for the mode arguments in filemode(), S_IMODE(), S_ISDIR(), etc.

Use it for the mode arguments in filemode(), S_IMODE(), S_ISDIR(), etc.
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

value = PyLong_AsUnsignedLong(op);
if ((value == (unsigned long)-1) && PyErr_Occurred())
if (PyLong_Check(op)) {
value = PyLong_AsUnsignedLong(op);
Copy link
Member

Choose a reason for hiding this comment

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

This code path looks like a micro-optimization (to avoid PyNumber_Index). I don't think that it's worth it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Not only. Many functions like PyLong_AsUnsignedLong() and PyLong_AsUnsignedLongMask(), many setters and converter, only call __index__() if the argument not int. So there would be a subtle behavior difference when the argument is an instance of the int subclass which defines __index__(). And calling __index__() for int subclass adds overhead.

But this is not completely consistent, so this may not make large difference.

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

@serhiy-storchaka serhiy-storchaka merged commit c33efa8 into python:main Apr 29, 2025
49 checks passed
@serhiy-storchaka serhiy-storchaka deleted the stat-unsigned-int-index branch April 29, 2025 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants