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.

classification
Title: os.stat() tuple access vs named attribute access int vs float
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: gregory.p.smith, serhiy.storchaka, vstinner
Priority: normal Keywords:

Created on 2015-03-03 01:16 by gregory.p.smith, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg237099 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2015-03-03 01:16
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
>>> import os, stat
>>> os.stat('/')
posix.stat_result(st_mode=16877, st_ino=2, st_dev=64513L, st_nlink=29, st_uid=0, st_gid=0, st_size=4096, st_atime=1425341751, st_mtime=1424824650, st_ctime=1424824650)
>>> x =os.stat('/')
>>> x.st_mtime
1424824650.653934
>>> x[stat.ST_MTIME]
1424824650
>>> os.stat_result
<type 'posix.stat_result'>


I have also confirmed the same behavior in 3.4.2.

This may be intentional.  Tuple [stat.ST_XXX] access to the stat return value is the old legacy way to access these prior to Python 2.2 when the stat_result object was introduced.  At that point they were likely all ints.

The os.stat_float_times() API exists to change the behavior of stat_result objects to return ints instead of floats by default, but the behavior of the tuple indexed values is always ints.

We need to explicitly document this behavior difference.  Changing the legacy tuple indexing behavior to return a float would likely break code.

Why file this?  We just ran into a bug due to code comparing a [stat.ST_MTIME] value to a stored stat_result.st_mtime thus continually reporting a difference due to the precision difference between the two but the numeric comparison doing its job regardless.
msg237103 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2015-03-03 02:31
It's already documented:

https://docs.python.org/dev/library/os.html#os.stat_result

"For compatibility with older Python versions, accessing stat_result as a tuple always returns integers."
msg237104 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2015-03-03 02:38
I missed that because i was looking for it to be called out under 2.7 os.stat() docs rather than under 2.7's os.stat_float_times() which is a method nobody is likely to read the documentation for as floats have been the default since 2.5.

The 2.7 docs are much less organized around this than the 3.x.

Adding that note in the same paragraph on 2.7's os.stat() docs would be sufficient.

"For backward compatibility, the return value of stat() is also accessible as a tuple of at least 10 integers" implies it, but doesn't explicitly call out the integer [ST_XXX] vs float .st_xxx difference as adding that sentence does.
msg370475 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-05-31 14:52
Python 2.7 is no longer supported.
History
Date User Action Args
2022-04-11 14:58:13adminsetgithub: 67755
2020-05-31 14:52:23serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg370475

resolution: out of date
stage: resolved
2015-03-03 02:38:50gregory.p.smithsetmessages: + msg237104
versions: - Python 3.4, Python 3.5
2015-03-03 02:31:35vstinnersetnosy: + vstinner
messages: + msg237103
2015-03-03 01:16:11gregory.p.smithcreate