This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eryksun
Recipients eryksun, scrool
Date 2020-05-16.22:09:58
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
There's no reason this can't be generalized to file attributes/flags on other platforms such as st_flags on BSD/macOS and st_attributes on Linux (depending on bpo-39533 -- but statx only returns a small subset of attributes that are available via chattr and lsattr).

Here's a table of approximately corresponding file attributes in BSD, Linux, and Windows:

BSD [1]         Linux                       Windows
UF_NOUNLINK                                 FILE_ATTRIBUTE_READONLY -R [2]
UF_READONLY                                 FILE_ATTRIBUTE_READONLY -R
UF_HIDDEN                                   FILE_ATTRIBUTE_HIDDEN -H
UF_SYSTEM                                   FILE_ATTRIBUTE_SYSTEM -S
UF_ARCHIVE                                  FILE_ATTRIBUTE_ARCHIVE -A
UF_SPARSE                                   FILE_ATTRIBUTE_SPARSE_FILE -P
UF_REPARSE                                  FILE_ATTRIBUTE_REPARSE_POINT -L
UF_OFFLINE                                  FILE_ATTRIBUTE_OFFLINE -O
[1] Not supported on all BSD platforms, including macOS.
[2] Readonly applies to regular file data, not metadata or directory
    contents (index data). Also, it disallows unlink but allows rename.

> not surprisingly I miss all windows attributes. On the top of that I 
> get only values of stat.S_IWRITE and stat.S_IREAD as documented in
> os.chmod().

Windows doesn't implement a direct equivalent of the Unix file mode. But Windows file attributes partially overlap the Unix file mode for the S_IFMT filetype bits. In particular, WinAPI GetFileType classifies an open file based on the device type as one of FILE_TYPE_CHAR (S_IFCHR), FILE_TYPE_PIPE (S_IFIFO, S_IFSOCK), or FILE_TYPE_DISK (S_IFBLK, S_IFREG, S_IFDIR, S_IFLNK). For the latter, FILE_ATTRIBUTE_DIRECTORY and FILE_ATTRIBUTE_REPARSE_POINT distinguish S_IFDIR and reparse points, including S_IFLNK, depending on the reparse tag. The lack of either attribute indicates S_IFREG, and no support for file attributes indicates S_IFBLK. For example:

    >>> stat.filemode(os.stat('//./nul').st_mode) # S_IFCHR
    >>> stat.filemode(os.stat('//./pipe').st_mode) # S_IFIFO
    >>> stat.filemode(os.stat('//./C:').st_mode) # S_IFBLK

(Apparently, we aren't fabricating any bogus permissions for the above cases.)

Free of charge, you also get a hack that sets the execute bit on directories, and also on files that have a file extension in the set {".COM", ".EXE", ".BAT", ".CMD"}. Caveat emptor: this has nothing to do with whether the file or directory actually grants the caller execute access.

    >>> stat.filemode(os.stat('C:/').st_mode) # S_IFDIR
    >>> os.stat('C:/Temp/spam.bat').st_file_attributes & stat.FILE_ATTRIBUTE_READONLY
    >>> os.readlink('C:/Temp/symlink.bat')
    >>> stat.filemode(os.lstat('C:/Temp/symlink.bat').st_mode) # S_IFLNK
    >>> stat.filemode(os.stat('C:/Temp/symlink.bat').st_mode) # S_IFREG

Regarding the permission mode bits, many environments (including Python) misuse FILE_ATTRIBUTE_READONLY as a write permission, i.e. readonly removes the S_IWUSR | S_IWGRP | S_IWOTH bits. Certainly readonly should be a factor in os.access(), but it is not a permission; no one can be granted permission to write to a readonly file. Using it as such is inconsistent with UF_IMMUTABLE in BSD and STATX_ATTR_IMMUTABLE in Linux. It's also inconsistent with how write permission works in Unix, since readonly disallows deleting the file, which has nothing to do with write permission on a file in Unix.
Date User Action Args
2020-05-16 22:09:58eryksunsetrecipients: + eryksun, scrool
2020-05-16 22:09:58eryksunsetmessageid: <>
2020-05-16 22:09:58eryksunlinkissue40644 messages
2020-05-16 22:09:58eryksuncreate