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
Merged
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
16 changes: 14 additions & 2 deletions Modules/_stat.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,21 @@ _PyLong_AsMode_t(PyObject *op)
unsigned long value;
mode_t mode;

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.

}
else {
op = PyNumber_Index(op);
if (op == NULL) {
return (mode_t)-1;
}
value = PyLong_AsUnsignedLong(op);
Py_DECREF(op);
}

if ((value == (unsigned long)-1) && PyErr_Occurred()) {
return (mode_t)-1;
}

mode = (mode_t)value;
if ((unsigned long)mode != value) {
Expand Down
Loading