We found a behavioral discrepancy between CPython and PyPy in stat.filemode via fuzz testing. Upon further inspection, we observed this discrepancy attributes to the differences between CPython's C and Python implementation.
Environment
Python: 3.11.11
OS: Ubuntu 24.04.3 LTS
Observed Discrepancy
Incorrect file type detection in stat.filemode(mode)
CPython's _stat.c determines the file type using (mode & S_IFMT) == S_IFBLK.
For 32767, (mode & S_IFMT) == 0o70000 does not match any known file type, so CPython correctly uses ? as the file-type character.
Its pure-Python implementation instead checks: (mode & S_IFBLK) == S_IFBLK. This check incorrectly evaluates true for 32767, causing Python implementation (which in turn affects PyPy) to misclassify the mode as a block device, yielding the wrong prefix b.
Activity
pythongh-144050: Fix stat.filemode file type detection in pure Python
gh-144050: Fix stat.filemode pure Python file type detection (GH-144059)
pythongh-144050: Fix stat.filemode pure Python file type detection (p…
pythongh-144050: Fix stat.filemode pure Python file type detection (p…
[3.13] gh-144050: Fix stat.filemode pure Python file type detection (G…
[3.14] gh-144050: Fix stat.filemode pure Python file type detection (G…
pythongh-144050: Fix stat.filemode pure Python file type detection (p…