]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-144050: Fix stat.filemode pure Python file type detection (GH-144059) ...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 20 Jan 2026 12:47:21 +0000 (13:47 +0100)
committerGitHub <noreply@github.com>
Tue, 20 Jan 2026 12:47:21 +0000 (12:47 +0000)
(cherry picked from commit fe629262c0db7aa18bad8bf3ac524acd8695739b)

Co-authored-by: VanshAgarwal24036 <148854295+VanshAgarwal24036@users.noreply.github.com>
Lib/stat.py
Lib/test/test_stat.py
Misc/NEWS.d/next/Library/2026-01-20-16-35-55.gh-issue-144050.0kKFbF.rst [new file with mode: 0644]

index 1b4ed1ebc940ef26c20e8a806cbca4aed6fa2bb4..81f694329bf4ff04aeed40a03e43798639fd2ddb 100644 (file)
@@ -166,9 +166,14 @@ def filemode(mode):
     perm = []
     for index, table in enumerate(_filemode_table):
         for bit, char in table:
-            if mode & bit == bit:
-                perm.append(char)
-                break
+            if index == 0:
+                if S_IFMT(mode) == bit:
+                    perm.append(char)
+                    break
+            else:
+                if mode & bit == bit:
+                    perm.append(char)
+                    break
         else:
             if index == 0:
                 # Unknown filetype
index 5fd25d5012c4253083be148f06281cd201934cfb..a83f7d076f027ef67ee73490b2ff561f0970460d 100644 (file)
@@ -163,6 +163,11 @@ class TestFilemode:
                              self.statmod.S_IFREG)
             self.assertEqual(self.statmod.S_IMODE(st_mode), 0o666)
 
+    def test_filemode_does_not_misclassify_random_bits(self):
+        # gh-144050 regression test
+        self.assertEqual(self.statmod.filemode(0o77777)[0], "?")
+        self.assertEqual(self.statmod.filemode(0o177777)[0], "?")
+
     @os_helper.skip_unless_working_chmod
     def test_directory(self):
         os.mkdir(TESTFN)
diff --git a/Misc/NEWS.d/next/Library/2026-01-20-16-35-55.gh-issue-144050.0kKFbF.rst b/Misc/NEWS.d/next/Library/2026-01-20-16-35-55.gh-issue-144050.0kKFbF.rst
new file mode 100644 (file)
index 0000000..dfc062d
--- /dev/null
@@ -0,0 +1,2 @@
+Fix :func:`stat.filemode` in the pure-Python implementation to avoid misclassifying
+invalid mode values as block devices.