Skip to content
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