From: Serhiy Storchaka Date: Tue, 29 Apr 2025 16:25:44 +0000 (+0300) Subject: gh-132987: Support __index__() in the stat module (GH-133097) X-Git-Tag: v3.14.0b1~189 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c33efa8735dfdb24011f5754f5e9e991c05f0587;p=thirdparty%2FPython%2Fcpython.git gh-132987: Support __index__() in the stat module (GH-133097) Use it for the mode arguments in filemode(), S_IMODE(), S_ISDIR(), etc. --- diff --git a/Modules/_stat.c b/Modules/_stat.c index 13a2bec252f4..f11ca7d23b44 100644 --- a/Modules/_stat.c +++ b/Modules/_stat.c @@ -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) {