[Python-checkins] cpython (2.7): Issue #11235: Fix OverflowError when trying to import a source file whose

antoine.pitrou python-checkins at python.org
Tue Jan 24 17:54:36 CET 2012


http://hg.python.org/cpython/rev/0499eed74126
changeset:   74598:0499eed74126
branch:      2.7
parent:      74584:f4f9ab2fd51b
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Tue Jan 24 17:44:06 2012 +0100
summary:
  Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp.

files:
  Lib/test/test_import.py |  13 +++++++++++++
  Misc/NEWS               |   3 +++
  Python/import.c         |  11 ++++-------
  3 files changed, 20 insertions(+), 7 deletions(-)


diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py
--- a/Lib/test/test_import.py
+++ b/Lib/test/test_import.py
@@ -277,6 +277,19 @@
         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
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,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 #11638: Unicode strings in 'name' and 'version' no longer cause
   UnicodeDecodeErrors.
 
diff --git a/Python/import.c b/Python/import.c
--- a/Python/import.c
+++ b/Python/import.c
@@ -981,14 +981,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");
-        return NULL;
-    }
+    st.st_mtime &= 0xFFFFFFFF;
 #endif
     cpathname = make_compiled_pathname(pathname, buf,
                                        (size_t)MAXPATHLEN + 1);

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list