(original) (raw)
changeset: 74597:cb13d8cff050 parent: 74595:75a3d35309cf parent: 74596:a2f3d6986bfa user: Antoine Pitrou solipsis@pitrou.net date: Tue Jan 24 17:45:50 2012 +0100 files: Lib/importlib/test/source/test_file_loader.py Lib/test/test_import.py Misc/NEWS Python/import.c description: Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp. diff -r 75a3d35309cf -r cb13d8cff050 Lib/importlib/test/source/test_file_loader.py --- a/Lib/importlib/test/source/test_file_loader.py Tue Jan 24 09:07:15 2012 -0500 +++ b/Lib/importlib/test/source/test_file_loader.py Tue Jan 24 17:45:50 2012 +0100 @@ -123,6 +123,23 @@ pycache = os.path.dirname(imp.cache_from_source(file_path)) shutil.rmtree(pycache) + def test_timestamp_overflow(self): + # When a modification timestamp is larger than 2**32, it should be + # truncated rather than raise an OverflowError. + with source_util.create_modules('_temp') as mapping: + source = mapping['_temp'] + compiled = imp.cache_from_source(source) + with open(source, 'w') as f: + f.write("x = 5") + os.utime(source, (2 ** 33, 2 ** 33)) + loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp']) + mod = loader.load_module('_temp') + # Sanity checks. + self.assertEqual(mod.__cached__, compiled) + self.assertEqual(mod.x, 5) + # The pyc file was created. + os.stat(compiled) + class BadBytecodeTest(unittest.TestCase): diff -r 75a3d35309cf -r cb13d8cff050 Lib/test/test_import.py --- a/Lib/test/test_import.py Tue Jan 24 09:07:15 2012 -0500 +++ b/Lib/test/test_import.py Tue Jan 24 17:45:50 2012 +0100 @@ -306,6 +306,18 @@ """)) script_helper.assert_python_ok(testfn) + def test_timestamp_overflow(self): + # A modification timestamp larger than 2**32 should not be a problem + # when importing a module (issue #11235). + source = TESTFN + ".py" + compiled = imp.cache_from_source(source) + with open(source, 'w') as f: + pass + os.utime(source, (2 ** 33, 2 ** 33)) + __import__(TESTFN) + # The pyc file was created. + os.stat(compiled) + class PycRewritingTests(unittest.TestCase): # Test that the `co_filename` attribute on code objects always points diff -r 75a3d35309cf -r cb13d8cff050 Misc/NEWS --- a/Misc/NEWS Tue Jan 24 09:07:15 2012 -0500 +++ b/Misc/NEWS Tue Jan 24 17:45:50 2012 +0100 @@ -10,6 +10,9 @@ Core and Builtins ----------------- +- Issue #11235: Fix OverflowError when trying to import a source file whose + modification time doesn't fit in a 32-bit timestamp. + - Issue #12705: A SyntaxError exception is now raised when attempting to compile multiple statements as a single interactive statement. diff -r 75a3d35309cf -r cb13d8cff050 Python/import.c --- a/Python/import.c Tue Jan 24 09:07:15 2012 -0500 +++ b/Python/import.c Tue Jan 24 17:45:50 2012 +0100 @@ -1478,14 +1478,11 @@ } #if SIZEOF_TIME_T > 4 /* Python's .pyc timestamp handling presumes that the timestamp fits - in 4 bytes. This will be fine until sometime in the year 2038, - when a 4-byte signed time_t will overflow. + in 4 bytes. Since the code only does an equality comparison, + ordering is not important and we can safely ignore the higher bits + (collisions are extremely unlikely). */ - if (st.st_mtime >> 32) { - PyErr_SetString(PyExc_OverflowError, - "modification time overflows a 4 byte field"); - goto error; - } + st.st_mtime &= 0xFFFFFFFF; #endif if (PyUnicode_READY(pathname) < 0) return NULL; /solipsis@pitrou.net