diff options
author | Nadeem Vawda <nadeem.vawda@gmail.com> | 2011-05-07 14:35:36 +0200 |
---|---|---|
committer | Nadeem Vawda <nadeem.vawda@gmail.com> | 2011-05-07 14:35:36 +0200 |
commit | 496564fe5be17057671a373f4a1126352823ece5 (patch) | |
tree | d7406a1080f024f81c85b95ef34919e07e508dcc /Lib/test/test_mmap.py | |
parent | 4117f99e46b886e37294835fc4683ea134d92bca (diff) | |
parent | 283036bb327333e0960d2e5009518ccdb44b6183 (diff) | |
download | cpython-496564fe5be17057671a373f4a1126352823ece5.tar.gz |
Merge: Fix potential resource leak in test_mmap.
Diffstat (limited to 'Lib/test/test_mmap.py')
-rw-r--r-- | Lib/test/test_mmap.py | 258 |
1 files changed, 129 insertions, 129 deletions
diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index f7d6c6ff65..712378b4a6 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -1,7 +1,11 @@ from test.support import (TESTFN, run_unittest, import_module, unlink, requires, _2G, _4G) import unittest -import os, re, itertools, socket, sys +import os +import re +import itertools +import socket +import sys # Skip test if we can't import mmap. mmap = import_module('mmap') @@ -117,126 +121,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) # Try writing with PROT_EXEC and without PROT_WRITE prot = mmap.PROT_READ | getattr(mmap, 'PROT_EXEC', 0) @@ -253,14 +250,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): @@ -344,11 +340,8 @@ class MmapTests(unittest.TestCase): f.write((65536 * 2) * b'm') # Arbitrary character with open(TESTFN, "rb") as f: - mf = mmap.mmap(f.fileno(), 0, offset=65536, access=mmap.ACCESS_READ) - try: + with mmap.mmap(f.fileno(), 0, offset=65536, access=mmap.ACCESS_READ) as mf: self.assertRaises(IndexError, mf.__getitem__, 80000) - finally: - mf.close() def test_length_0_large_offset(self): # Issue #10959: test mapping of a file by passing 0 for @@ -533,7 +526,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") @@ -541,11 +535,12 @@ 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" - 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() @@ -620,7 +615,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() @@ -645,6 +641,19 @@ class MmapTests(unittest.TestCase): finally: s.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") class LargeMmapTests(unittest.TestCase): @@ -670,19 +679,13 @@ class LargeMmapTests(unittest.TestCase): def test_large_offset(self): with self._make_test_file(0x14FFFFFFF, b" ") as f: - m = mmap.mmap(f.fileno(), 0, offset=0x140000000, access=mmap.ACCESS_READ) - try: + with mmap.mmap(f.fileno(), 0, offset=0x140000000, access=mmap.ACCESS_READ) as m: self.assertEqual(m[0xFFFFFFF], 32) - finally: - m.close() def test_large_filesize(self): with self._make_test_file(0x17FFFFFFF, b" ") as f: - m = mmap.mmap(f.fileno(), 0x10000, access=mmap.ACCESS_READ) - try: + with mmap.mmap(f.fileno(), 0x10000, access=mmap.ACCESS_READ) as m: self.assertEqual(m.size(), 0x180000000) - finally: - m.close() # Issue 11277: mmap() with large (~4GB) sparse files crashes on OS X. @@ -691,11 +694,8 @@ class LargeMmapTests(unittest.TestCase): start = boundary - len(tail) // 2 end = start + len(tail) with self._make_test_file(start, tail) as f: - m = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) - try: + with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as m: self.assertEqual(m[start:end], tail) - finally: - m.close() @unittest.skipUnless(sys.maxsize > _4G, "test cannot run on 32-bit systems") def test_around_2GB(self): |