diff options
Diffstat (limited to 'Lib/test/test_mmap.py')
| -rw-r--r-- | Lib/test/test_mmap.py | 84 |
1 files changed, 36 insertions, 48 deletions
diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index 3e6ecfc956..dd857a0632 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -268,13 +268,12 @@ class MmapTests(unittest.TestCase): def test_find_end(self): # test the new 'end' parameter works as expected - f = open(TESTFN, 'wb+') - data = b'one two ones' - n = len(data) - f.write(data) - f.flush() - m = mmap.mmap(f.fileno(), n) - f.close() + with open(TESTFN, 'wb+') as f: + data = b'one two ones' + n = len(data) + f.write(data) + f.flush() + m = mmap.mmap(f.fileno(), n) self.assertEqual(m.find(b'one'), 0) self.assertEqual(m.find(b'ones'), 8) @@ -287,13 +286,12 @@ class MmapTests(unittest.TestCase): def test_rfind(self): # test the new 'end' parameter works as expected - f = open(TESTFN, 'wb+') - data = b'one two ones' - n = len(data) - f.write(data) - f.flush() - m = mmap.mmap(f.fileno(), n) - f.close() + with open(TESTFN, 'wb+') as f: + data = b'one two ones' + n = len(data) + f.write(data) + f.flush() + m = mmap.mmap(f.fileno(), n) self.assertEqual(m.rfind(b'one'), 8) self.assertEqual(m.rfind(b'one '), 0) @@ -306,30 +304,23 @@ class MmapTests(unittest.TestCase): def test_double_close(self): # make sure a double close doesn't crash on Solaris (Bug# 665913) - f = open(TESTFN, 'wb+') - - f.write(2**16 * b'a') # Arbitrary character - f.close() + with open(TESTFN, 'wb+') as f: + f.write(2**16 * b'a') # Arbitrary character - f = open(TESTFN, 'rb') - mf = mmap.mmap(f.fileno(), 2**16, access=mmap.ACCESS_READ) - mf.close() - mf.close() - f.close() + with open(TESTFN, 'rb') as f: + mf = mmap.mmap(f.fileno(), 2**16, access=mmap.ACCESS_READ) + mf.close() + mf.close() def test_entire_file(self): # test mapping of entire file by passing 0 for map length - f = open(TESTFN, "wb+") + with open(TESTFN, "wb+") as f: + f.write(2**16 * b'm') # Arbitrary character - f.write(2**16 * b'm') # Arbitrary character - f.close() - - f = open(TESTFN, "rb+") - mf = mmap.mmap(f.fileno(), 0) - self.assertEqual(len(mf), 2**16, "Map size should equal file size.") - self.assertEqual(mf.read(2**16), 2**16 * b"m") - mf.close() - f.close() + with open(TESTFN, "rb+") as f, \ + mmap.mmap(f.fileno(), 0) as mf: + self.assertEqual(len(mf), 2**16, "Map size should equal file size.") + self.assertEqual(mf.read(2**16), 2**16 * b"m") def test_length_0_offset(self): # Issue #10916: test mapping of remainder of file by passing 0 for @@ -355,16 +346,15 @@ class MmapTests(unittest.TestCase): def test_move(self): # make move works everywhere (64-bit format problem earlier) - f = open(TESTFN, 'wb+') + with open(TESTFN, 'wb+') as f: - f.write(b"ABCDEabcde") # Arbitrary character - f.flush() + f.write(b"ABCDEabcde") # Arbitrary character + f.flush() - mf = mmap.mmap(f.fileno(), 10) - mf.move(5, 0, 5) - self.assertEqual(mf[:], b"ABCDEABCDE", "Map move should have duplicated front 5") - mf.close() - f.close() + mf = mmap.mmap(f.fileno(), 10) + mf.move(5, 0, 5) + self.assertEqual(mf[:], b"ABCDEABCDE", "Map move should have duplicated front 5") + mf.close() # more excessive test data = b"0123456789" @@ -562,10 +552,9 @@ class MmapTests(unittest.TestCase): mapsize = 10 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") - f.close() + with open(TESTFN, "rb") as f: + m = mmap.mmap(f.fileno(), mapsize, prot=mmap.PROT_READ) + self.assertRaises(TypeError, m.write, "foo") def test_error(self): self.assertIs(mmap.error, OSError) @@ -574,9 +563,8 @@ class MmapTests(unittest.TestCase): data = b"0123456789" 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() + with open(TESTFN, "r+b") as f: + m = mmap.mmap(f.fileno(), len(data)) # Test write_byte() for i in range(len(data)): self.assertEqual(m.tell(), i) |
