https://github.com/python/cpython/commit/c33efa8735dfdb24011f5754f5e9e991c05f0587
commit: c33efa8735dfdb24011f5754f5e9e991c05f0587
branch: main
author: Serhiy Storchaka <storch...@gmail.com>
committer: serhiy-storchaka <storch...@gmail.com>
date: 2025-04-29T19:25:44+03:00
summary:

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

Use it for the mode arguments in filemode(), S_IMODE(), S_ISDIR(), etc.

files:
M Modules/_stat.c

diff --git a/Modules/_stat.c b/Modules/_stat.c
index 13a2bec252f448..f11ca7d23b440d 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) {

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to