From ab7c34aa8bfcaee51cb2f1779c18ce96a0f7303c Mon Sep 17 00:00:00 2001 From: VanshAgarwal24036 Date: Tue, 20 Jan 2026 11:41:26 +0530 Subject: [PATCH 1/4] Fix stat.filemode pure Python file type detection --- Lib/stat.py | 11 ++++++++--- Lib/test/test_stat.py | 4 ++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Lib/stat.py b/Lib/stat.py index ab1b25b9d6351c..750e2a8a3f0c3f 100644 --- a/Lib/stat.py +++ b/Lib/stat.py @@ -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 (mode & S_IFMT) == bit: + perm.append(char) + break + else: + if mode & bit == bit: + perm.append(char) + break else: if index == 0: # Unknown filetype diff --git a/Lib/test/test_stat.py b/Lib/test/test_stat.py index 5fd25d5012c425..e5a9826d1f2aca 100644 --- a/Lib/test/test_stat.py +++ b/Lib/test/test_stat.py @@ -163,6 +163,10 @@ def test_mode(self): 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(32767)[0], "?") + @os_helper.skip_unless_working_chmod def test_directory(self): os.mkdir(TESTFN) From 21de74d06eb4c9fbeaa4d972b723cb1999bedc3e Mon Sep 17 00:00:00 2001 From: VanshAgarwal24036 Date: Tue, 20 Jan 2026 11:59:42 +0530 Subject: [PATCH 2/4] gh-144050: Fix stat.filemode file type detection in pure Python --- Lib/stat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/stat.py b/Lib/stat.py index 750e2a8a3f0c3f..214c7917b5e048 100644 --- a/Lib/stat.py +++ b/Lib/stat.py @@ -167,7 +167,7 @@ def filemode(mode): for index, table in enumerate(_filemode_table): for bit, char in table: if index == 0: - if (mode & S_IFMT) == bit: + if S_IFMT(mode) == bit: perm.append(char) break else: From e0e74e33f7decfcd3600870a8da64c39a2367a7c Mon Sep 17 00:00:00 2001 From: VanshAgarwal24036 Date: Tue, 20 Jan 2026 16:37:53 +0530 Subject: [PATCH 3/4] Update test case for higher bit and added news --- Lib/test/test_stat.py | 3 ++- .../Library/2026-01-20-16-35-55.gh-issue-144050.0kKFbF.rst | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-01-20-16-35-55.gh-issue-144050.0kKFbF.rst diff --git a/Lib/test/test_stat.py b/Lib/test/test_stat.py index e5a9826d1f2aca..a83f7d076f027e 100644 --- a/Lib/test/test_stat.py +++ b/Lib/test/test_stat.py @@ -165,7 +165,8 @@ def test_mode(self): def test_filemode_does_not_misclassify_random_bits(self): # gh-144050 regression test - self.assertEqual(self.statmod.filemode(32767)[0], "?") + self.assertEqual(self.statmod.filemode(0o77777)[0], "?") + self.assertEqual(self.statmod.filemode(0o177777)[0], "?") @os_helper.skip_unless_working_chmod def test_directory(self): 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 index 00000000000000..28a33990149c1d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-01-20-16-35-55.gh-issue-144050.0kKFbF.rst @@ -0,0 +1,2 @@ +Fix stat.filemode in the pure-Python implementation to avoid misclassifying +invalid mode values as block devices. From b560d6e9b7201f45e2fb7b0da0c3f53ea656aa80 Mon Sep 17 00:00:00 2001 From: VanshAgarwal24036 <148854295+VanshAgarwal24036@users.noreply.github.com> Date: Tue, 20 Jan 2026 17:02:54 +0530 Subject: [PATCH 4/4] Update Misc/NEWS.d/next/Library/2026-01-20-16-35-55.gh-issue-144050.0kKFbF.rst Co-authored-by: Serhiy Storchaka --- .../next/Library/2026-01-20-16-35-55.gh-issue-144050.0kKFbF.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 28a33990149c1d..dfc062d023c8f1 100644 --- 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 @@ -1,2 +1,2 @@ -Fix stat.filemode in the pure-Python implementation to avoid misclassifying +Fix :func:`stat.filemode` in the pure-Python implementation to avoid misclassifying invalid mode values as block devices.