From deba941d61c8f02f84a5d1452f568d7dbf4d85e3 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Tue, 30 Jun 2009 23:06:06 +0000 Subject: convert old fail* assertions to assert* --- Lib/test/test_mmap.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Lib/test/test_mmap.py') diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index dec0980317..eae4157edc 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -501,8 +501,8 @@ class MmapTests(unittest.TestCase): f.close() def test_error(self): - self.assert_(issubclass(mmap.error, EnvironmentError)) - self.assert_("mmap.error" in str(mmap.error)) + self.assertTrue(issubclass(mmap.error, EnvironmentError)) + self.assertTrue("mmap.error" in str(mmap.error)) def test_io_methods(self): data = b"0123456789" -- cgit v1.2.1 From 3e1025652caea5abed08f93c312829d06cd82475 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Tue, 19 Jan 2010 00:09:57 +0000 Subject: use assert[Not]In where appropriate A patch from Dave Malcolm. --- Lib/test/test_mmap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Lib/test/test_mmap.py') diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index eae4157edc..c27b898e30 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -502,7 +502,7 @@ class MmapTests(unittest.TestCase): def test_error(self): self.assertTrue(issubclass(mmap.error, EnvironmentError)) - self.assertTrue("mmap.error" in str(mmap.error)) + self.assertIn("mmap.error", str(mmap.error)) def test_io_methods(self): data = b"0123456789" -- cgit v1.2.1 From 870dcb42c0dde594b39e788f6b510f055e2baf4e Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Sun, 1 Aug 2010 14:50:00 +0000 Subject: #8046: add context manager protocol support to mmap objects. Also add closed property. --- Lib/test/test_mmap.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'Lib/test/test_mmap.py') diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index c27b898e30..68af00e3a2 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -586,6 +586,20 @@ class MmapTests(unittest.TestCase): pass m.close() + def test_context_manager(self): + with mmap.mmap(-1, 10) as m: + self.assertFalse(m.closed) + self.assertTrue(m.closed) + + def test_context_manager_exception(self): + # Test that the IOError gets passed through + with self.assertRaises(Exception) as exc: + with mmap.mmap(-1, 10) as m: + raise IOError + self.assertIsInstance(exc.exception, IOError, + "wrong exception raised in context manager") + self.assertTrue(m.closed, "context manager failed") + def test_main(): run_unittest(MmapTests) -- cgit v1.2.1 From 9111cbebf4aa3fa98769461766da2fbd379e627d Mon Sep 17 00:00:00 2001 From: Brian Curtin Date: Sun, 1 Aug 2010 15:26:26 +0000 Subject: Fix #8105. Add validation to mmap.mmap so invalid file descriptors don't cause a crash on Windows. --- Lib/test/test_mmap.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'Lib/test/test_mmap.py') diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index 68af00e3a2..5906c02ea9 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -1,6 +1,6 @@ from test.support import TESTFN, run_unittest, import_module import unittest -import os, re, itertools +import os, re, itertools, socket # Skip test if we can't import mmap. mmap = import_module('mmap') @@ -586,6 +586,17 @@ class MmapTests(unittest.TestCase): pass m.close() + def test_invalid_descriptor(self): + # socket file descriptors are valid, but out of range + # for _get_osfhandle, causing a crash when validating the + # parameters to _get_osfhandle. + s = socket.socket() + try: + with self.assertRaises(mmap.error): + m = mmap.mmap(s.fileno(), 10) + finally: + s.close() + def test_context_manager(self): with mmap.mmap(-1, 10) as m: self.assertFalse(m.closed) -- cgit v1.2.1 From 042677b4af9d2cbc5500c7de7328771cec8da6b4 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Sun, 31 Oct 2010 01:44:49 +0000 Subject: context managerify --- Lib/test/test_mmap.py | 223 +++++++++++++++++++++++++------------------------- 1 file changed, 111 insertions(+), 112 deletions(-) (limited to 'Lib/test/test_mmap.py') diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index 5906c02ea9..54a43f93df 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -1,6 +1,10 @@ from test.support import TESTFN, run_unittest, import_module import unittest -import os, re, itertools, socket +import os +import re +import itertools +import socket +import sys # Skip test if we can't import mmap. mmap = import_module('mmap') @@ -116,126 +120,119 @@ class MmapTests(unittest.TestCase): def test_access_parameter(self): # Test for "access" keyword parameter mapsize = 10 - open(TESTFN, "wb").write(b"a"*mapsize) - f = open(TESTFN, "rb") - m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_READ) - self.assertEqual(m[:], b'a'*mapsize, "Readonly memory map data incorrect.") + with open(TESTFN, "wb") as fp: + fp.write(b"a"*mapsize) + with open(TESTFN, "rb") as f: + m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_READ) + self.assertEqual(m[:], b'a'*mapsize, "Readonly memory map data incorrect.") - # Ensuring that readonly mmap can't be slice assigned - try: - m[:] = b'b'*mapsize - except TypeError: - pass - else: - self.fail("Able to write to readonly memory map") + # Ensuring that readonly mmap can't be slice assigned + try: + m[:] = b'b'*mapsize + except TypeError: + pass + else: + self.fail("Able to write to readonly memory map") - # Ensuring that readonly mmap can't be item assigned - try: - m[0] = b'b' - except TypeError: - pass - else: - self.fail("Able to write to readonly memory map") + # Ensuring that readonly mmap can't be item assigned + try: + m[0] = b'b' + except TypeError: + pass + else: + self.fail("Able to write to readonly memory map") - # Ensuring that readonly mmap can't be write() to - try: - m.seek(0,0) - m.write(b'abc') - except TypeError: - pass - else: - self.fail("Able to write to readonly memory map") + # Ensuring that readonly mmap can't be write() to + try: + m.seek(0,0) + m.write(b'abc') + except TypeError: + pass + else: + self.fail("Able to write to readonly memory map") - # Ensuring that readonly mmap can't be write_byte() to - try: - m.seek(0,0) - m.write_byte(b'd') - except TypeError: - pass - else: - self.fail("Able to write to readonly memory map") + # Ensuring that readonly mmap can't be write_byte() to + try: + m.seek(0,0) + m.write_byte(b'd') + except TypeError: + pass + else: + self.fail("Able to write to readonly memory map") - # Ensuring that readonly mmap can't be resized - try: - m.resize(2*mapsize) - except SystemError: # resize is not universally supported - pass - except TypeError: - pass - else: - self.fail("Able to resize readonly memory map") - f.close() - del m, f - self.assertEqual(open(TESTFN, "rb").read(), b'a'*mapsize, - "Readonly memory map data file was modified") + # Ensuring that readonly mmap can't be resized + try: + m.resize(2*mapsize) + except SystemError: # resize is not universally supported + pass + except TypeError: + pass + else: + self.fail("Able to resize readonly memory map") + with open(TESTFN, "rb") as fp: + self.assertEqual(fp.read(), b'a'*mapsize, + "Readonly memory map data file was modified") # Opening mmap with size too big - import sys - f = open(TESTFN, "r+b") - try: - m = mmap.mmap(f.fileno(), mapsize+1) - except ValueError: - # we do not expect a ValueError on Windows - # CAUTION: This also changes the size of the file on disk, and - # later tests assume that the length hasn't changed. We need to - # repair that. + with open(TESTFN, "r+b") as f: + try: + m = mmap.mmap(f.fileno(), mapsize+1) + except ValueError: + # we do not expect a ValueError on Windows + # CAUTION: This also changes the size of the file on disk, and + # later tests assume that the length hasn't changed. We need to + # repair that. + if sys.platform.startswith('win'): + self.fail("Opening mmap with size+1 should work on Windows.") + else: + # we expect a ValueError on Unix, but not on Windows + if not sys.platform.startswith('win'): + self.fail("Opening mmap with size+1 should raise ValueError.") + m.close() if sys.platform.startswith('win'): - self.fail("Opening mmap with size+1 should work on Windows.") - else: - # we expect a ValueError on Unix, but not on Windows - if not sys.platform.startswith('win'): - self.fail("Opening mmap with size+1 should raise ValueError.") - m.close() - f.close() - if sys.platform.startswith('win'): - # Repair damage from the resizing test. - f = open(TESTFN, 'r+b') - f.truncate(mapsize) - f.close() + # Repair damage from the resizing test. + with open(TESTFN, 'r+b') as f: + f.truncate(mapsize) # Opening mmap with access=ACCESS_WRITE - f = open(TESTFN, "r+b") - m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_WRITE) - # Modifying write-through memory map - m[:] = b'c'*mapsize - self.assertEqual(m[:], b'c'*mapsize, - "Write-through memory map memory not updated properly.") - m.flush() - m.close() - f.close() - f = open(TESTFN, 'rb') - stuff = f.read() - f.close() + with open(TESTFN, "r+b") as f: + m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_WRITE) + # Modifying write-through memory map + m[:] = b'c'*mapsize + self.assertEqual(m[:], b'c'*mapsize, + "Write-through memory map memory not updated properly.") + m.flush() + m.close() + with open(TESTFN, 'rb') as f: + stuff = f.read() self.assertEqual(stuff, b'c'*mapsize, "Write-through memory map data file not updated properly.") # Opening mmap with access=ACCESS_COPY - f = open(TESTFN, "r+b") - m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_COPY) - # Modifying copy-on-write memory map - m[:] = b'd'*mapsize - self.assertEqual(m[:], b'd' * mapsize, - "Copy-on-write memory map data not written correctly.") - m.flush() - self.assertEqual(open(TESTFN, "rb").read(), b'c'*mapsize, - "Copy-on-write test data file should not be modified.") - # Ensuring copy-on-write maps cannot be resized - self.assertRaises(TypeError, m.resize, 2*mapsize) - f.close() - del m, f + with open(TESTFN, "r+b") as f: + m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_COPY) + # Modifying copy-on-write memory map + m[:] = b'd'*mapsize + self.assertEqual(m[:], b'd' * mapsize, + "Copy-on-write memory map data not written correctly.") + m.flush() + with open(TESTFN, "rb") as fp: + self.assertEqual(fp.read(), b'c'*mapsize, + "Copy-on-write test data file should not be modified.") + # Ensuring copy-on-write maps cannot be resized + self.assertRaises(TypeError, m.resize, 2*mapsize) + m.close() # Ensuring invalid access parameter raises exception - f = open(TESTFN, "r+b") - self.assertRaises(ValueError, mmap.mmap, f.fileno(), mapsize, access=4) - f.close() + with open(TESTFN, "r+b") as f: + self.assertRaises(ValueError, mmap.mmap, f.fileno(), mapsize, access=4) if os.name == "posix": # Try incompatible flags, prot and access parameters. - f = open(TESTFN, "r+b") - self.assertRaises(ValueError, mmap.mmap, f.fileno(), mapsize, - flags=mmap.MAP_PRIVATE, - prot=mmap.PROT_READ, access=mmap.ACCESS_WRITE) - f.close() + with open(TESTFN, "r+b") as f: + self.assertRaises(ValueError, mmap.mmap, f.fileno(), mapsize, + flags=mmap.MAP_PRIVATE, + prot=mmap.PROT_READ, access=mmap.ACCESS_WRITE) def test_bad_file_desc(self): # Try opening a bad file descriptor... @@ -244,14 +241,13 @@ class MmapTests(unittest.TestCase): def test_tougher_find(self): # Do a tougher .find() test. SF bug 515943 pointed out that, in 2.2, # searching for data with embedded \0 bytes didn't work. - f = open(TESTFN, 'wb+') + with open(TESTFN, 'wb+') as f: - data = b'aabaac\x00deef\x00\x00aa\x00' - n = len(data) - f.write(data) - f.flush() - m = mmap.mmap(f.fileno(), n) - f.close() + data = b'aabaac\x00deef\x00\x00aa\x00' + n = len(data) + f.write(data) + f.flush() + m = mmap.mmap(f.fileno(), n) for start in range(n+1): for finish in range(start, n+1): @@ -494,7 +490,8 @@ class MmapTests(unittest.TestCase): if not hasattr(mmap, 'PROT_READ'): return mapsize = 10 - open(TESTFN, "wb").write(b"a"*mapsize) + with open(TESTFN, "wb") as fp: + fp.write(b"a"*mapsize) f = open(TESTFN, "rb") m = mmap.mmap(f.fileno(), mapsize, prot=mmap.PROT_READ) self.assertRaises(TypeError, m.write, "foo") @@ -506,7 +503,8 @@ class MmapTests(unittest.TestCase): def test_io_methods(self): data = b"0123456789" - open(TESTFN, "wb").write(b"x"*len(data)) + with open(TESTFN, "wb") as fp: + fp.write(b"x"*len(data)) f = open(TESTFN, "r+b") m = mmap.mmap(f.fileno(), len(data)) f.close() @@ -572,7 +570,8 @@ class MmapTests(unittest.TestCase): m.close() # Should not crash (Issue 5385) - open(TESTFN, "wb").write(b"x"*10) + with open(TESTFN, "wb") as fp: + fp.write(b"x"*10) f = open(TESTFN, "r+b") m = mmap.mmap(f.fileno(), 0) f.close() -- cgit v1.2.1 From e9602b8f19ae0045625e2eb08ecffd82c2533e86 Mon Sep 17 00:00:00 2001 From: Hirokazu Yamamoto Date: Thu, 4 Nov 2010 12:09:08 +0000 Subject: Issue #5391: mmap.read_byte() should return unsigned value [0, 255] instead of signed value [-127, 128]. --- Lib/test/test_mmap.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'Lib/test/test_mmap.py') diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index 54a43f93df..f78fed95a0 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -534,6 +534,15 @@ class MmapTests(unittest.TestCase): m.seek(8) self.assertRaises(ValueError, m.write, b"bar") + def test_non_ascii_byte(self): + for b in (129, 200, 255): # > 128 + m = mmap.mmap(-1, 1) + m.write_byte(b) + self.assertEquals(m[0], b) + m.seek(0) + self.assertEquals(m.read_byte(), b) + m.close() + if os.name == 'nt': def test_tagname(self): data1 = b"0123456789" -- cgit v1.2.1 From 19449cb010e5ef7c775b582a56f477ef67c57f8c Mon Sep 17 00:00:00 2001 From: Ezio Melotti Date: Sat, 20 Nov 2010 19:04:17 +0000 Subject: #9424: Replace deprecated assert* methods in the Python test suite. --- Lib/test/test_mmap.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'Lib/test/test_mmap.py') diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index f78fed95a0..abfde01ae4 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -413,7 +413,7 @@ class MmapTests(unittest.TestCase): data = bytes(reversed(data)) L[start:stop:step] = data m[start:stop:step] = data - self.assertEquals(m[:], bytes(L)) + self.assertEqual(m[:], bytes(L)) def make_mmap_file (self, f, halfsize): # Write 2 pages worth of data to the file @@ -510,27 +510,27 @@ class MmapTests(unittest.TestCase): f.close() # Test write_byte() for i in range(len(data)): - self.assertEquals(m.tell(), i) + self.assertEqual(m.tell(), i) m.write_byte(data[i]) - self.assertEquals(m.tell(), i+1) + self.assertEqual(m.tell(), i+1) self.assertRaises(ValueError, m.write_byte, b"x"[0]) - self.assertEquals(m[:], data) + self.assertEqual(m[:], data) # Test read_byte() m.seek(0) for i in range(len(data)): - self.assertEquals(m.tell(), i) - self.assertEquals(m.read_byte(), data[i]) - self.assertEquals(m.tell(), i+1) + self.assertEqual(m.tell(), i) + self.assertEqual(m.read_byte(), data[i]) + self.assertEqual(m.tell(), i+1) self.assertRaises(ValueError, m.read_byte) # Test read() m.seek(3) - self.assertEquals(m.read(3), b"345") - self.assertEquals(m.tell(), 6) + self.assertEqual(m.read(3), b"345") + self.assertEqual(m.tell(), 6) # Test write() m.seek(3) m.write(b"bar") - self.assertEquals(m.tell(), 6) - self.assertEquals(m[:], b"012bar6789") + self.assertEqual(m.tell(), 6) + self.assertEqual(m[:], b"012bar6789") m.seek(8) self.assertRaises(ValueError, m.write, b"bar") @@ -538,9 +538,9 @@ class MmapTests(unittest.TestCase): for b in (129, 200, 255): # > 128 m = mmap.mmap(-1, 1) m.write_byte(b) - self.assertEquals(m[0], b) + self.assertEqual(m[0], b) m.seek(0) - self.assertEquals(m.read_byte(), b) + self.assertEqual(m.read_byte(), b) m.close() if os.name == 'nt': @@ -554,8 +554,8 @@ class MmapTests(unittest.TestCase): m1[:] = data1 m2 = mmap.mmap(-1, len(data2), tagname="foo") m2[:] = data2 - self.assertEquals(m1[:], data2) - self.assertEquals(m2[:], data2) + self.assertEqual(m1[:], data2) + self.assertEqual(m2[:], data2) m2.close() m1.close() @@ -564,8 +564,8 @@ class MmapTests(unittest.TestCase): m1[:] = data1 m2 = mmap.mmap(-1, len(data2), tagname="boo") m2[:] = data2 - self.assertEquals(m1[:], data1) - self.assertEquals(m2[:], data2) + self.assertEqual(m1[:], data1) + self.assertEqual(m2[:], data2) m2.close() m1.close() -- cgit v1.2.1 From f112fd7558bb6f1aeff87a3184c63d2a0a8a0697 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Sat, 15 Jan 2011 16:17:07 +0000 Subject: Issue #10916: mmap should not segfault when a file is mapped using 0 as length and a non-zero offset, and an attempt to read past the end of file is made (IndexError is raised instead). Patch by Ross Lagerwall. Requested by Georg. --- Lib/test/test_mmap.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'Lib/test/test_mmap.py') diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index abfde01ae4..c7b8d60d1e 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -320,6 +320,19 @@ class MmapTests(unittest.TestCase): mf.close() f.close() + def test_length_0_offset(self): + # Issue #10916: test mapping of remainder of file by passing 0 for + # map length with an offset doesn't cause a segfault. + if not hasattr(os, "stat"): + self.skipTest("needs os.stat") + with open(TESTFN, "wb+") as f: + f.write(49152 * b'm') # Arbitrary character + + with open(TESTFN, "rb") as f: + mf = mmap.mmap(f.fileno(), 0, offset=40960, access=mmap.ACCESS_READ) + self.assertRaises(IndexError, mf.__getitem__, 45000) + mf.close() + def test_move(self): # make move works everywhere (64-bit format problem earlier) f = open(TESTFN, 'wb+') -- cgit v1.2.1 From 02430a639cdee6139974fb63e4f65f1348446858 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Sat, 15 Jan 2011 17:25:58 +0000 Subject: Fix mmap and test_mmap under Windows too (followup to r88022) --- Lib/test/test_mmap.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'Lib/test/test_mmap.py') diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index c7b8d60d1e..08b0714da0 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -325,13 +325,14 @@ class MmapTests(unittest.TestCase): # map length with an offset doesn't cause a segfault. if not hasattr(os, "stat"): self.skipTest("needs os.stat") - with open(TESTFN, "wb+") as f: - f.write(49152 * b'm') # Arbitrary character + # NOTE: allocation granularity is currently 65536 under Win64, + # and therefore the minimum offset alignment. + with open(TESTFN, "wb") as f: + f.write((65536 * 2) * b'm') # Arbitrary character with open(TESTFN, "rb") as f: - mf = mmap.mmap(f.fileno(), 0, offset=40960, access=mmap.ACCESS_READ) - self.assertRaises(IndexError, mf.__getitem__, 45000) - mf.close() + with mmap.mmap(f.fileno(), 0, offset=65536, access=mmap.ACCESS_READ) as mf: + self.assertRaises(IndexError, mf.__getitem__, 80000) def test_move(self): # make move works everywhere (64-bit format problem earlier) -- cgit v1.2.1 From fcdfa8cbcf50a0fca54b57643c06ea932b6c725c Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Thu, 20 Jan 2011 21:07:24 +0000 Subject: Issue #10955: Fix a potential crash when trying to mmap() a file past its length. Initial patch by Ross Lagerwall. This fixes a regression introduced by r88022. --- Lib/test/test_mmap.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'Lib/test/test_mmap.py') diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index 08b0714da0..8f20d96966 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -334,6 +334,19 @@ class MmapTests(unittest.TestCase): with mmap.mmap(f.fileno(), 0, offset=65536, access=mmap.ACCESS_READ) as mf: self.assertRaises(IndexError, mf.__getitem__, 80000) + def test_length_0_large_offset(self): + # Issue #10959: test mapping of a file by passing 0 for + # map length with a large offset doesn't cause a segfault. + if not hasattr(os, "stat"): + self.skipTest("needs os.stat") + + with open(TESTFN, "wb") as f: + f.write(115699 * b'm') # Arbitrary character + + with open(TESTFN, "w+b") as f: + self.assertRaises(ValueError, mmap.mmap, f.fileno(), 0, + offset=2147418112) + def test_move(self): # make move works everywhere (64-bit format problem earlier) f = open(TESTFN, 'wb+') -- cgit v1.2.1 From 38754a10ac72d224162482f5abfc50a92a8430d4 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Mon, 21 Feb 2011 23:46:27 +0000 Subject: Merged revisions 88486 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ........ r88486 | antoine.pitrou | 2011-02-22 00:41:12 +0100 (mar., 22 févr. 2011) | 5 lines Issue #4681: Allow mmap() to work on file sizes and offsets larger than 4GB, even on 32-bit builds. Initial patch by Ross Lagerwall, adapted for 32-bit Windows. ........ --- Lib/test/test_mmap.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) (limited to 'Lib/test/test_mmap.py') diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index 8f20d96966..cbef37441d 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -1,4 +1,4 @@ -from test.support import TESTFN, run_unittest, import_module +from test.support import TESTFN, run_unittest, import_module, unlink, requires import unittest import os import re @@ -646,9 +646,56 @@ class MmapTests(unittest.TestCase): "wrong exception raised in context manager") self.assertTrue(m.closed, "context manager failed") +class LargeMmapTests(unittest.TestCase): + + def setUp(self): + unlink(TESTFN) + + 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): + 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" ") + + 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" ") + + with open(TESTFN, 'rb') as f: + with mmap.mmap(f.fileno(), 0x10000, access=mmap.ACCESS_READ) as m: + self.assertEqual(m.size(), 0x180000000) + def test_main(): - run_unittest(MmapTests) + run_unittest(MmapTests, LargeMmapTests) if __name__ == '__main__': test_main() -- cgit v1.2.1