[Python-checkins] cpython (merge 3.1 -> 3.2): Merge: #11277: Add tests for mmap crash when using large sparse files on OS X.

nadeem.vawda python-checkins at python.org
Sat May 7 13:12:42 CEST 2011


http://hg.python.org/cpython/rev/e447a68742e7
changeset:   69891:e447a68742e7
branch:      3.2
parent:      69883:6907c43d1ee7
parent:      69890:d5d4f2967879
user:        Nadeem Vawda <nadeem.vawda at gmail.com>
date:        Sat May 07 13:08:54 2011 +0200
summary:
  Merge: #11277: Add tests for mmap crash when using large sparse files on OS X.

Also, reduce code duplication in LargeMmapTests.

Original patch by Steffen Daode Nurpmeso.

files:
  Lib/test/test_mmap.py |  57 ++++++++++++++++--------------
  1 files changed, 31 insertions(+), 26 deletions(-)


diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py
--- a/Lib/test/test_mmap.py
+++ b/Lib/test/test_mmap.py
@@ -1,4 +1,5 @@
-from test.support import TESTFN, run_unittest, import_module, unlink, requires
+from test.support import (TESTFN, run_unittest, import_module, unlink,
+                          requires, _2G, _4G)
 import unittest
 import os
 import re
@@ -662,45 +663,49 @@
     def tearDown(self):
         unlink(TESTFN)
 
-    def _working_largefile(self):
-        # Only run if the current filesystem supports large files.
-        f = open(TESTFN, 'wb', buffering=0)
-        try:
-            f.seek(0x80000001)
-            f.write(b'x')
-            f.flush()
-        except (IOError, OverflowError):
-            raise unittest.SkipTest("filesystem does not have largefile support")
-        finally:
-            f.close()
-            unlink(TESTFN)
-
-    def test_large_offset(self):
+    def _create_test_file(self, num_zeroes, tail):
         if sys.platform[:3] == 'win' or sys.platform == 'darwin':
             requires('largefile',
                 'test requires %s bytes and a long time to run' % str(0x180000000))
-        self._working_largefile()
         with open(TESTFN, 'wb') as f:
-            f.seek(0x14FFFFFFF)
-            f.write(b" ")
+            try:
+                f.seek(num_zeroes)
+                f.write(tail)
+                f.flush()
+            except (IOError, OverflowError):
+                raise unittest.SkipTest("filesystem does not have largefile support")
 
+    def test_large_offset(self):
+        self._create_test_file(0x14FFFFFFF, b" ")
         with open(TESTFN, 'rb') as f:
             with mmap.mmap(f.fileno(), 0, offset=0x140000000, access=mmap.ACCESS_READ) as m:
                 self.assertEqual(m[0xFFFFFFF], 32)
 
     def test_large_filesize(self):
-        if sys.platform[:3] == 'win' or sys.platform == 'darwin':
-            requires('largefile',
-                'test requires %s bytes and a long time to run' % str(0x180000000))
-        self._working_largefile()
-        with open(TESTFN, 'wb') as f:
-            f.seek(0x17FFFFFFF)
-            f.write(b" ")
-
+        self._create_test_file(0x17FFFFFFF, b" ")
         with open(TESTFN, 'rb') as f:
             with mmap.mmap(f.fileno(), 0x10000, access=mmap.ACCESS_READ) as m:
                 self.assertEqual(m.size(), 0x180000000)
 
+    # Issue 11277: mmap() with large (~4GB) sparse files crashes on OS X.
+
+    def _test_around_boundary(self, boundary):
+        tail = b'  DEARdear  '
+        start = boundary - len(tail) // 2
+        end = start + len(tail)
+        self._create_test_file(start, tail)
+        with open(TESTFN, 'rb') as f:
+            with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as m:
+                self.assertEqual(m[start:end], tail)
+
+    @unittest.skipUnless(sys.maxsize > _4G, "test cannot run on 32-bit systems")
+    def test_around_2GB(self):
+        self._test_around_boundary(_2G)
+
+    @unittest.skipUnless(sys.maxsize > _4G, "test cannot run on 32-bit systems")
+    def test_around_4GB(self):
+        self._test_around_boundary(_4G)
+
 
 def test_main():
     run_unittest(MmapTests, LargeMmapTests)

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


More information about the Python-checkins mailing list