diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2012-12-24 16:33:18 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2012-12-24 16:33:18 (GMT) |
commit | 9fade768c89e3fbb497ac2851fbf540c3f054757 (patch) | |
tree | 8546c09b93d4f8688f88d7027b442fc248bd6667 /Lib/test | |
parent | d8590ff209369f45a5ed417c79ce255a33f70a1d (diff) | |
download | cpython-9fade768c89e3fbb497ac2851fbf540c3f054757.zip cpython-9fade768c89e3fbb497ac2851fbf540c3f054757.tar.gz cpython-9fade768c89e3fbb497ac2851fbf540c3f054757.tar.bz2 |
Issue #13863: fix incorrect .pyc timestamps on Windows / NTFS (apparently due to buggy fstat)
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_import.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py index edd1869..ea50d34 100644 --- a/Lib/test/test_import.py +++ b/Lib/test/test_import.py @@ -5,6 +5,7 @@ import os import py_compile import random import stat +import struct import sys import unittest import textwrap @@ -350,6 +351,46 @@ class ImportTests(unittest.TestCase): del sys.path[0] remove_files(TESTFN) + def test_pyc_mtime(self): + # Test for issue #13863: .pyc timestamp sometimes incorrect on Windows. + sys.path.insert(0, os.curdir) + try: + # Jan 1, 2012; Jul 1, 2012. + mtimes = 1325376000, 1341100800 + + # Different names to avoid running into import caching. + tails = "spam", "eggs" + for mtime, tail in zip(mtimes, tails): + module = TESTFN + tail + source = module + ".py" + compiled = source + ('c' if __debug__ else 'o') + + # Create a new Python file with the given mtime. + with open(source, 'w') as f: + f.write("# Just testing\nx=1, 2, 3\n") + os.utime(source, (mtime, mtime)) + + # Generate the .pyc/o file; if it couldn't be created + # for some reason, skip the test. + m = __import__(module) + if not os.path.exists(compiled): + unlink(source) + self.skipTest("Couldn't create .pyc/.pyo file.") + + # Actual modification time of .py file. + mtime1 = int(os.stat(source).st_mtime) & 0xffffffff + + # mtime that was encoded in the .pyc file. + with open(compiled, 'rb') as f: + mtime2 = struct.unpack('<L', f.read(8)[4:])[0] + + unlink(compiled) + unlink(source) + + self.assertEqual(mtime1, mtime2) + finally: + sys.path.pop(0) + class PycRewritingTests(unittest.TestCase): # Test that the `co_filename` attribute on code objects always points |