Python 2.5.1 broken os.stat module




I just upgraded from Python 2.4.2 to Python 2.5.1 and have found some
unexpected behavior that appears to be a bug in the os.stat module.

My OS is Windows XP SP2 + all updates.

I have several programs that have worked flawlessly on all previous Python
versions for years and they are now producing incorrect results in the code
that uses os.stat.

Searching through the 2.5.1 release notes I found the following:

Use Win32 API to implement os.stat/fstat. As a result, subsecond
timestamps
are reported, the limit on path name lengths is removed, and stat
reports
WindowsError now (instead of OSError).

*********************
* Overview of the problem:
*********************

Reviewing my code it seems that it should still work with the 2.5.1 os.stat
changes however that does not appear to be the case.

Timestamps reported by os.stat() are no longer correct and the results are
not even consistent.

In my first test case ALL 3 timestamps reported by Python are 1 hour less
than the actual timestamp on the file.

In my second test case the created and last accessed timestamps reported by
Python are correct but the last write timestamp is 1 hour less than the
actual timestamp on the file.

As a third test I looked at the last write timestamps on approximately
21,000 files.
Python determined wrong last write timestamp on approximately 1581 files.

Assuming there is no error in the following code that prints out the
timestamps using the new return value from os.stat() then it would appear
that the 2.5.1 os.stat changes have a bug.

print 'Creation Time: %s' % time.strftime('%m/%d/%Y %H:%M:%S',
time.localtime(file_stats[stat.ST_CTIME]))
print 'Last Access Time: %s' % time.strftime('%m/%d/%Y %H:%M:%S',
time.localtime(file_stats[stat.ST_ATIME]))
print 'Last Write Time: %s' % time.strftime('%m/%d/%Y %H:%M:%S',
time.localtime(file_stats[stat.ST_MTIME]))


*********************
* Detailed test results
*********************

To demonstrate the problem I have created the following test.

Here are the files that will be used in my test and their associated
timestamps as reported the the dir command.

01/02/2003 12:34 PM 0 broke_test
03/06/2007 05:24 PM 3,497,177 broke_test2
05/31/2007 04:35 PM 254 runtest.cmd
05/31/2007 04:31 PM 513 broke.py

The file named broke_test has a timestamp of 01/02/2003 12:34:56 (dir does
not show seconds).

The runtest.cmd script shows the created, last accessed, and last write
timestamps as reported by the dir command (and also verified via right
clicking on the file and selecting properties in Explorer.

------ >>> START runtest.cmd script <<<<
@Echo Off

echo Create TimeStamp reported by dir command
dir /tc %1

echo Last Access TimeStamp reported by dir command
dir /ta %1

echo Last Write TimeStamp reported by dir command
dir /tw %1

echo Python 2.5.1 timestamp info
broke.py %1

------ >>> END runtest.cmd script <<<<

The broke.py script prints out the created, last accessed, last write
timestamps as Python sees them.

------ >>> START broke.py script <<<<
import sys
import os
import stat
import time

file_name = sys.argv[1]

file_stats = os.stat(file_name)

print
print 'File Name : %s' % (file_name)
print
print 'Creation Time: %s' % time.strftime('%m/%d/%Y %H:%M:%S',
time.localtime(file_stats[stat.ST_CTIME]))
print 'Last Access Time: %s' % time.strftime('%m/%d/%Y %H:%M:%S',
time.localtime(file_stats[stat.ST_ATIME]))
print 'Last Write Time: %s' % time.strftime('%m/%d/%Y %H:%M:%S',
time.localtime(file_stats[stat.ST_MTIME]))
print

------ >>> END broke.py script <<<<

#
# Test 1 on file broke_test
#

runtest broke_test

Create TimeStamp reported by dir command

01/02/2003 12:34 PM 0 broke_test

Last Access TimeStamp reported by dir command

01/02/2003 12:34 PM 0 broke_test

Last Write TimeStamp reported by dir command

01/02/2003 12:34 PM 0 broke_test

Python 2.5.1 timestamp info

File Name : broke_test

Creation Time: 01/02/2003 11:34:56 -- Python results are WRONG hours
reported are 1 hour short
Last Access Time: 01/02/2003 11:34:56 -- Python results are WRONG hours
reported are 1 hour short
Last Write Time: 01/02/2003 11:34:56 -- Python results are WRONG hours
reported are 1 hour short

#
# Test 2 on file broke_test2
#

runtest broke_test2

Create TimeStamp reported by dir command

05/31/2007 04:26 PM 3,497,177 broke_test2

Last Access TimeStamp reported by dir command

05/31/2007 04:26 PM 3,497,177 broke_test2

Last Write TimeStamp reported by dir command

03/06/2007 05:24 PM 3,497,177 broke_test2

Python 2.5.1 timestamp info

File Name : broke_test2

Creation Time: 05/31/2007 16:26:36 -- Python results are correct
Last Access Time: 05/31/2007 16:26:38 -- Python results are correct
Last Write Time: 03/06/2007 16:24:21 -- Python results are WRONG hours
reported are 1 hour short




















.



Relevant Pages

  • Re: File Polling (Rereading)
    ... > is the timestamp of the last modification - so you can then skip rereading ... > such thing neither in python, nor the underlying OSses (which is the reason ...
    (comp.lang.python)
  • Re: the Gravity of Python 2
    ... I almost found a reason to move to Python 3 today. ... I discovered that Python 3.3's datetime has a .timestamp() ... naive datetime instance representing UTC time. ...
    (comp.lang.python)
  • Re: How to chain multiple commands in one line? "( cmd1 && cmd2 && cmd3)"
    ... I would like to output the current timestamp when a certain command is ... success or failure). ... echo, not Echo. ...
    (comp.os.linux.misc)
  • Re: Any way to not create .pyc files?
    ... Ulitmately it sounds like a permissions problem. ... The only reason a .pyc file gets written is if the magic number inside doesn't match the one expected by your version of python, or if the timestamp that is stored inside doesn't match the timestamp of the .py file from which it was created. ...
    (comp.lang.python)
  • doubt regarding Conversion of date into timestamp
    ... I am new to Python I want to know how to ... date I want to change as a timestamp. ... If the format of date string is changed. ... India Matrimony: ...
    (comp.lang.python)