Skip to content

Commit b231028

Browse files
committed
bpo-39533: Add documentation on new :func:os.stat properties
1 parent 5d09912 commit b231028

File tree

4 files changed

+84
-18
lines changed

4 files changed

+84
-18
lines changed

Doc/library/os.rst

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,6 +1856,15 @@ features:
18561856
* :data:`stat.UF_OPAQUE`
18571857
* :data:`stat.UF_NOUNLINK`
18581858
* :data:`stat.UF_COMPRESSED`
1859+
* :data:`stat.UF_COMPRESSED`
1860+
* :data:`stat.UF_TRACKED`
1861+
* :data:`stat.UF_SYSTEM`
1862+
* :data:`stat.UF_SPARSE`
1863+
* :data:`stat.UF_OFFLINE`
1864+
* :data:`stat.UF_REPARSE`
1865+
* :data:`stat.UF_ARCHIVE`
1866+
* :data:`stat.UF_READONLY`
1867+
* :data:`stat.UF_ENCRYPTED`
18591868
* :data:`stat.UF_HIDDEN`
18601869
* :data:`stat.SF_ARCHIVED`
18611870
* :data:`stat.SF_IMMUTABLE`
@@ -2786,6 +2795,14 @@ features:
27862795
The size of a symbolic link is the length of the pathname it contains,
27872796
without a terminating null byte.
27882797

2798+
.. attribute:: st_flags
2799+
2800+
User defined flags for file. On some systems this will contain the
2801+
equivalent BSD flags for the more specific flags returned on the given
2802+
platforms: See the :attr:`st_attributes` and :attr:`st_file_attributes`
2803+
fields below for the original values. If the platform does not support
2804+
returning flags this will be set to ``0`` (no flags).
2805+
27892806
Timestamps:
27902807

27912808
.. attribute:: st_atime
@@ -2803,6 +2820,14 @@ features:
28032820
* the time of most recent metadata change on Unix,
28042821
* the time of creation on Windows, expressed in seconds.
28052822

2823+
.. attribute:: st_btime
2824+
2825+
Time of creation ("file birth") expressed in seconds.
2826+
2827+
Identical to :attr:`st_ctime` on Windows. Some platforms, particularily
2828+
builds for pre-4.11 Linux kernels do not support returning this value,
2829+
and will return ``None`` instead.
2830+
28062831
.. attribute:: st_atime_ns
28072832

28082833
Time of most recent access expressed in nanoseconds as an integer.
@@ -2820,27 +2845,43 @@ features:
28202845
* the time of creation on Windows, expressed in nanoseconds as an
28212846
integer.
28222847

2848+
.. attribute:: st_btime_ns
2849+
2850+
Time of creation ("file birth") expressed in nanoseconds.
2851+
2852+
Identical to :attr:`st_ctime_ns` on Windows. Some platforms,
2853+
particularily builds for pre-4.11 Linux kernels do not support returning
2854+
this value, and will return ``None`` instead.
2855+
28232856
.. note::
28242857

28252858
The exact meaning and resolution of the :attr:`st_atime`,
2826-
:attr:`st_mtime`, and :attr:`st_ctime` attributes depend on the operating
2827-
system and the file system. For example, on Windows systems using the FAT
2828-
or FAT32 file systems, :attr:`st_mtime` has 2-second resolution, and
2829-
:attr:`st_atime` has only 1-day resolution. See your operating system
2830-
documentation for details.
2859+
:attr:`st_mtime`, :attr:`st_ctime`, and :attr:`st_btime` attributes depend
2860+
on the operating system and the file system. For example, on Windows
2861+
systems using the FAT or FAT32 file systems, :attr:`st_mtime` has 2-second
2862+
resolution, and :attr:`st_atime` has only 1-day resolution. See your
2863+
operating system documentation for details.
28312864

28322865
Similarly, although :attr:`st_atime_ns`, :attr:`st_mtime_ns`,
2833-
and :attr:`st_ctime_ns` are always expressed in nanoseconds, many
2834-
systems do not provide nanosecond precision. On systems that do
2835-
provide nanosecond precision, the floating-point object used to
2836-
store :attr:`st_atime`, :attr:`st_mtime`, and :attr:`st_ctime`
2837-
cannot preserve all of it, and as such will be slightly inexact.
2838-
If you need the exact timestamps you should always use
2839-
:attr:`st_atime_ns`, :attr:`st_mtime_ns`, and :attr:`st_ctime_ns`.
2866+
:attr:`st_ctime_ns`, and :attr:`st_btime_ns` are always expressed in
2867+
nanoseconds, many systems do not provide nanosecond precision. On
2868+
systems that do provide nanosecond precision, the floating-point object
2869+
used to store :attr:`st_atime`, :attr:`st_mtime`, :attr:`st_ctime`,
2870+
and :attr:`st_btime` cannot preserve all of it, and as such will be
2871+
slightly inexact. If you need the exact timestamps you should always use
2872+
:attr:`st_atime_ns`, :attr:`st_mtime_ns`, :attr:`st_ctime_ns`, and
2873+
:attr:`st_btime_ns`.
28402874

28412875
On some Unix systems (such as Linux), the following attributes may also be
28422876
available:
28432877

2878+
.. attribute:: st_attributes
2879+
.. attribute:: st_attributes_mask
2880+
2881+
Linux file attributes: ``stx_attributes`` and ``stx_attributes_mask``
2882+
members of the :c:type:`statx` structure returned by :c:func:`statx`.
2883+
See the ``STATX_FLAG_*`` constants in the :mod:`stat` module.
2884+
28442885
.. attribute:: st_blocks
28452886

28462887
Number of 512-byte blocks allocated for file.
@@ -2853,11 +2894,8 @@ features:
28532894

28542895
.. attribute:: st_rdev
28552896

2856-
Type of device if an inode device.
2857-
2858-
.. attribute:: st_flags
2859-
2860-
User defined flags for file.
2897+
Type of device (combined major and minor number) if the queried file
2898+
was an inode device.
28612899

28622900
On other Unix systems (such as FreeBSD), the following attributes may be
28632901
available (but may be only filled out if root tries to use them):
@@ -2868,7 +2906,8 @@ features:
28682906

28692907
.. attribute:: st_birthtime
28702908

2871-
Time of file creation.
2909+
Time of file creation. Identical to :attr:`st_btime`, but retained for
2910+
backward compatibility.
28722911

28732912
On Solaris and derivatives, the following attributes may also be
28742913
available:
@@ -2942,6 +2981,13 @@ features:
29422981
files as :const:`S_IFCHR`, :const:`S_IFIFO` or :const:`S_IFBLK`
29432982
as appropriate.
29442983

2984+
.. versionadded:: 3.9
2985+
Added the :attr:`st_attributes` and :attr:`st_attributes_mask` members
2986+
on modern Linux.
2987+
2988+
.. versionadded:: 3.9
2989+
Added the :attr:`st_btime` and :attr:`st_flags` members for all platforms.
2990+
29452991
.. function:: statvfs(path)
29462992

29472993
Perform a :c:func:`statvfs` system call on the given path. The return value is

Doc/library/stat.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,3 +435,17 @@ constants, but are not an exhaustive list.
435435
IO_REPARSE_TAG_APPEXECLINK
436436

437437
.. versionadded:: 3.8
438+
439+
On Linux, the following constants are available for comparing against the
440+
``st_attributes`` and ``st_attributes_mask`` members returned by
441+
:func:`os.stat`. See the relevant man-page on
442+
`statx(2) <http://man7.org/linux/man-pages/man2/statx.2.html>`_ for the exact
443+
meaning of these constants.
444+
445+
.. data:: STATX_ATTR_COMPRESSED
446+
STATX_ATTR_IMMUTABLE
447+
STATX_ATTR_APPEND
448+
STATX_ATTR_NODUMP
449+
STATX_ATTR_ENCRYPTED
450+
451+
.. versionadded:: 3.12

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,7 @@ Andreas Schawo
15831583
Neil Schemenauer
15841584
David Scherer
15851585
Wolfgang Scherer
1586+
Alexander Schlarb
15861587
Hynek Schlawack
15871588
Bob Schmertz
15881589
Gregor Schmid
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Use the ``statx(2)`` system call for :func:`os.stat` under Linux to provide
2+
cross-platform support for btime (time of file creation) and file
3+
attributes. On Windows btime is emulated using ctime and both Linux and
4+
Windows file attributes are converted to their (Free)BSD equivalent were
5+
reasonable.

0 commit comments

Comments
 (0)