Skip to content

Conversation

@VanshAgarwal24036
Copy link
Contributor

@VanshAgarwal24036 VanshAgarwal24036 commented Jan 20, 2026

This PR fixes a discrepancy between the C and pure Python implementations
of stat.filemode.

The pure Python implementation could misclassify arbitrary bit patterns
(e.g. 32767) as block devices. File type detection now correctly uses
(mode & S_IFMT) for file types, matching the C implementation.

A regression test is added to prevent this issue from recurring.

Fixes #144050

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a NEWS entry.


def test_filemode_does_not_misclassify_random_bits(self):
# gh-144050 regression test
self.assertEqual(self.statmod.filemode(32767)[0], "?")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use octals instead of decimals. Also, you need to test also the highest bit in a 16-bit number, so 0o177777.

if mode & bit == bit:
perm.append(char)
break
if index == 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See https://github.com/python/cpython/pull/108639/changes#r1309034276 . We can use opportunity to refactor this code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Discrepancy between stat.filemode's C and Python implementation

2 participants