From 33d15f7c85c1700465bc89f896f2bafc58d2b2b5 Mon Sep 17 00:00:00 2001
From: Antoine Pitrou <solipsis@pitrou.net>
Date: Wed, 25 Jan 2012 18:01:45 +0100
Subject: [PATCH] Port import fixes from 2.7.

---
 Lib/importlib/test/source/test_file_loader.py |  2 +-
 Lib/test/test_import.py                       |  2 +-
 Python/import.c                               | 20 +++++++++----------
 3 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/Lib/importlib/test/source/test_file_loader.py b/Lib/importlib/test/source/test_file_loader.py
index 912e6c3b51..c7a7d8fbca 100644
--- a/Lib/importlib/test/source/test_file_loader.py
+++ b/Lib/importlib/test/source/test_file_loader.py
@@ -138,7 +138,7 @@ class SimpleTest(unittest.TestCase):
             with open(source, 'w') as f:
                 f.write("x = 5")
             try:
-                os.utime(source, (2 ** 33, 2 ** 33))
+                os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5))
             except OverflowError:
                 self.skipTest("cannot set modification time to large integer")
             except OSError as e:
diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py
index 311063505e..48443ea580 100644
--- a/Lib/test/test_import.py
+++ b/Lib/test/test_import.py
@@ -321,7 +321,7 @@ class ImportTests(unittest.TestCase):
             with open(source, 'w') as f:
                 pass
             try:
-                os.utime(source, (2 ** 33, 2 ** 33))
+                os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5))
             except OverflowError:
                 self.skipTest("cannot set modification time to large integer")
             except OSError as e:
diff --git a/Python/import.c b/Python/import.c
index 40b8d0a558..beda7f97d2 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -1226,9 +1226,9 @@ write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat)
         (void) unlink(cpathname);
         return;
     }
-    /* Now write the true mtime */
+    /* Now write the true mtime (as a 32-bit field) */
     fseek(fp, 4L, 0);
-    assert(mtime < LONG_MAX);
+    assert(mtime <= 0xFFFFFFFF);
     PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
     fflush(fp);
     fclose(fp);
@@ -1302,14 +1302,14 @@ load_source_module(char *name, char *pathname, FILE *fp)
                      pathname);
         return NULL;
     }
-#if SIZEOF_TIME_T > 4
-    /* Python's .pyc timestamp handling presumes that the timestamp fits
-       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).
-     */
-    st.st_mtime &= 0xFFFFFFFF;
-#endif
+    if (sizeof st.st_mtime > 4) {
+        /* Python's .pyc timestamp handling presumes that the timestamp fits
+           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).
+         */
+        st.st_mtime &= 0xFFFFFFFF;
+    }
     cpathname = make_compiled_pathname(
         pathname, buf, (size_t)MAXPATHLEN + 1, !Py_OptimizeFlag);
     if (cpathname != NULL &&
-- 
2.40.0