[New-bugs-announce] [issue39729] stat.S_ISXXX can raise OverflowError for remote file modes

Arnon Yaari report at bugs.python.org
Sun Feb 23 05:08:44 EST 2020

New submission from Arnon Yaari <wiggin15 at gmail.com>:

The C implementation of the "stat" module on Python 3 (_stat) is using the type "mode_t" for file modes, which differs between operating systems. This type can be defined as either "unsigned short" (for example, in macOS, or the definition added specifically for Windows in _stat.c) or "unsigned long" (Linux and other Unix systems such as AIX).
This means that the "stat" module may only work with file modes that come from the same system that Python was compiled for.
It is sometimes desirable to work with file modes on remote systems (for example, when using the "fabric" module to handle remote files - https://github.com/fabric/fabric/blob/1.10/fabric/sftp.py#L42).
With the pure-python "stat" module on Python 2.7, using macros such as "stat.S_ISDIR" with any value used to work (even values that exceed "unsigned short" on macOS, for example) but with the C implementation this can result in an exception on systems with an "unsigned short" mode_t:

    >>> stat.S_ISDIR(0o240755)
    OverflowError: mode out of range

I encountered this exception when trying to "put" files from a macOS system to an AIX system with "fabric" (0o240755 is the st_mode found for "/" on AIX).

For uniform handling of file modes, modes should be handled as unsigned long instead of the system-defined "mode_t".

components: Library (Lib)
messages: 362499
nosy: wiggin15
priority: normal
severity: normal
status: open
title: stat.S_ISXXX can raise OverflowError for remote file modes
type: behavior
versions: Python 3.8, Python 3.9

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list