]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-132987: Support __index__() in the stat module (GH-133097)
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 29 Apr 2025 16:25:44 +0000 (19:25 +0300)
committerGitHub <noreply@github.com>
Tue, 29 Apr 2025 16:25:44 +0000 (19:25 +0300)
Use it for the mode arguments in filemode(), S_IMODE(), S_ISDIR(), etc.

Modules/_stat.c

index 13a2bec252f44852ed0d0a1497f1a47d26101023..f11ca7d23b440d10b6499e9a4c36c8b5fa055e9f 100644 (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) {