]> granicus.if.org Git - python/commitdiff
Issue #11235: Fix OverflowError when trying to import a source file whose modificatio...
authorAntoine Pitrou <solipsis@pitrou.net>
Tue, 24 Jan 2012 16:44:06 +0000 (17:44 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Tue, 24 Jan 2012 16:44:06 +0000 (17:44 +0100)
Lib/test/test_import.py
Misc/NEWS
Python/import.c

index bdf94438e5cb51966670872f5f55372c43ed19b0..2acc155f1386c352cecd7f8b924a1795db103401 100644 (file)
@@ -277,6 +277,19 @@ class ImportTests(unittest.TestCase):
         finally:
             os.rmdir(source)
 
+    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"
+        self.addCleanup(remove_files, TESTFN)
+        compiled = source + ('c' if __debug__ else 'o')
+        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
index 27e1b65500eedbcf20abb9fb9a1af9e63c032304..6f910be10f1e893d3f24529cadd786988c6e3cdb 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,9 @@ What's New in Python 2.7.3?
 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 #11638: Unicode strings in 'name' and 'version' no longer cause
   UnicodeDecodeErrors.
 
index c60ecfea5e0c1a8ac52590b6f476014ae8d99f60..0e823909b2892dd6d36f17eba36ff47a9c0b4daa 100644 (file)
@@ -981,14 +981,11 @@ load_source_module(char *name, char *pathname, FILE *fp)
     }
 #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");
-        return NULL;
-    }
+    st.st_mtime &= 0xFFFFFFFF;
 #endif
     cpathname = make_compiled_pathname(pathname, buf,
                                        (size_t)MAXPATHLEN + 1);