Skip to content

Commit c33efa8

Browse files
gh-132987: Support __index__() in the stat module (GH-133097)
Use it for the mode arguments in filemode(), S_IMODE(), S_ISDIR(), etc.
1 parent acb222c commit c33efa8

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

Modules/_stat.c

+14-2
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,21 @@ _PyLong_AsMode_t(PyObject *op)
295295
unsigned long value;
296296
mode_t mode;
297297

298-
value = PyLong_AsUnsignedLong(op);
299-
if ((value == (unsigned long)-1) && PyErr_Occurred())
298+
if (PyLong_Check(op)) {
299+
value = PyLong_AsUnsignedLong(op);
300+
}
301+
else {
302+
op = PyNumber_Index(op);
303+
if (op == NULL) {
304+
return (mode_t)-1;
305+
}
306+
value = PyLong_AsUnsignedLong(op);
307+
Py_DECREF(op);
308+
}
309+
310+
if ((value == (unsigned long)-1) && PyErr_Occurred()) {
300311
return (mode_t)-1;
312+
}
301313

302314
mode = (mode_t)value;
303315
if ((unsigned long)mode != value) {

0 commit comments

Comments
 (0)