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

Use it for the mode arguments in filemode(), S_IMODE(), S_ISDIR(), etc.
This commit is contained in:
Serhiy Storchaka 2025-04-29 19:25:44 +03:00 committed by GitHub
parent acb222ce8f
commit c33efa8735
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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);
}
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) {