summaryrefslogtreecommitdiff
path: root/Lib/test/test_mmap.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_mmap.py')
-rw-r--r--Lib/test/test_mmap.py182
1 files changed, 104 insertions, 78 deletions
diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py
index 4407c5cfc9..899df8d818 100644
--- a/Lib/test/test_mmap.py
+++ b/Lib/test/test_mmap.py
@@ -314,26 +314,25 @@ class MmapTests(unittest.TestCase):
mf.close()
f.close()
+ @unittest.skipUnless(hasattr(os, "stat"), "needs os.stat()")
def test_entire_file(self):
# test mapping of entire file by passing 0 for map length
- if hasattr(os, "stat"):
- f = open(TESTFN, "wb+")
+ f = open(TESTFN, "wb+")
- f.write(2**16 * b'm') # Arbitrary character
- f.close()
+ 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()
+ 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()
+ @unittest.skipUnless(hasattr(os, "stat"), "needs os.stat()")
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")
# NOTE: allocation granularity is currently 65536 under Win64,
# and therefore the minimum offset alignment.
with open(TESTFN, "wb") as f:
@@ -343,12 +342,10 @@ class MmapTests(unittest.TestCase):
with mmap.mmap(f.fileno(), 0, offset=65536, access=mmap.ACCESS_READ) as mf:
self.assertRaises(IndexError, mf.__getitem__, 80000)
+ @unittest.skipUnless(hasattr(os, "stat"), "needs os.stat()")
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
@@ -417,6 +414,35 @@ class MmapTests(unittest.TestCase):
m[x] = b
self.assertEqual(m[x], b)
+ def test_read_all(self):
+ m = mmap.mmap(-1, 16)
+ self.addCleanup(m.close)
+
+ # With no parameters, or None or a negative argument, reads all
+ m.write(bytes(range(16)))
+ m.seek(0)
+ self.assertEqual(m.read(), bytes(range(16)))
+ m.seek(8)
+ self.assertEqual(m.read(), bytes(range(8, 16)))
+ m.seek(16)
+ self.assertEqual(m.read(), b'')
+ m.seek(3)
+ self.assertEqual(m.read(None), bytes(range(3, 16)))
+ m.seek(4)
+ self.assertEqual(m.read(-1), bytes(range(4, 16)))
+ m.seek(5)
+ self.assertEqual(m.read(-2), bytes(range(5, 16)))
+ m.seek(9)
+ self.assertEqual(m.read(-42), bytes(range(9, 16)))
+
+ def test_read_invalid_arg(self):
+ m = mmap.mmap(-1, 16)
+ self.addCleanup(m.close)
+
+ self.assertRaises(TypeError, m.read, 'foo')
+ self.assertRaises(TypeError, m.read, 5.5)
+ self.assertRaises(TypeError, m.read, [1, 2, 3])
+
def test_extended_getslice(self):
# Test extended slicing by comparing with list slicing.
s = bytes(reversed(range(256)))
@@ -531,9 +557,8 @@ class MmapTests(unittest.TestCase):
return mmap.mmap.__new__(klass, -1, *args, **kwargs)
anon_mmap(PAGESIZE)
+ @unittest.skipUnless(hasattr(mmap, 'PROT_READ'), "needs mmap.PROT_READ")
def test_prot_readonly(self):
- if not hasattr(mmap, 'PROT_READ'):
- return
mapsize = 10
with open(TESTFN, "wb") as fp:
fp.write(b"a"*mapsize)
@@ -543,8 +568,7 @@ class MmapTests(unittest.TestCase):
f.close()
def test_error(self):
- self.assertTrue(issubclass(mmap.error, EnvironmentError))
- self.assertIn("mmap.error", str(mmap.error))
+ self.assertIs(mmap.error, OSError)
def test_io_methods(self):
data = b"0123456789"
@@ -588,67 +612,69 @@ class MmapTests(unittest.TestCase):
self.assertEqual(m.read_byte(), b)
m.close()
- if os.name == 'nt':
- def test_tagname(self):
- data1 = b"0123456789"
- data2 = b"abcdefghij"
- assert len(data1) == len(data2)
-
- # Test same tag
- m1 = mmap.mmap(-1, len(data1), tagname="foo")
- m1[:] = data1
- m2 = mmap.mmap(-1, len(data2), tagname="foo")
- m2[:] = data2
- self.assertEqual(m1[:], data2)
- self.assertEqual(m2[:], data2)
- m2.close()
- m1.close()
-
- # Test different tag
- m1 = mmap.mmap(-1, len(data1), tagname="foo")
- m1[:] = data1
- m2 = mmap.mmap(-1, len(data2), tagname="boo")
- m2[:] = data2
- self.assertEqual(m1[:], data1)
- self.assertEqual(m2[:], data2)
- m2.close()
- m1.close()
-
- def test_crasher_on_windows(self):
- # Should not crash (Issue 1733986)
- m = mmap.mmap(-1, 1000, tagname="foo")
- try:
- mmap.mmap(-1, 5000, tagname="foo")[:] # same tagname, but larger size
- except:
- pass
- m.close()
+ @unittest.skipUnless(os.name == 'nt', 'requires Windows')
+ def test_tagname(self):
+ data1 = b"0123456789"
+ data2 = b"abcdefghij"
+ assert len(data1) == len(data2)
+
+ # Test same tag
+ m1 = mmap.mmap(-1, len(data1), tagname="foo")
+ m1[:] = data1
+ m2 = mmap.mmap(-1, len(data2), tagname="foo")
+ m2[:] = data2
+ self.assertEqual(m1[:], data2)
+ self.assertEqual(m2[:], data2)
+ m2.close()
+ m1.close()
+
+ # Test different tag
+ m1 = mmap.mmap(-1, len(data1), tagname="foo")
+ m1[:] = data1
+ m2 = mmap.mmap(-1, len(data2), tagname="boo")
+ m2[:] = data2
+ self.assertEqual(m1[:], data1)
+ self.assertEqual(m2[:], data2)
+ m2.close()
+ m1.close()
+
+ @unittest.skipUnless(os.name == 'nt', 'requires Windows')
+ def test_crasher_on_windows(self):
+ # Should not crash (Issue 1733986)
+ m = mmap.mmap(-1, 1000, tagname="foo")
+ try:
+ mmap.mmap(-1, 5000, tagname="foo")[:] # same tagname, but larger size
+ except:
+ pass
+ m.close()
- # Should not crash (Issue 5385)
- with open(TESTFN, "wb") as fp:
- fp.write(b"x"*10)
- f = open(TESTFN, "r+b")
- m = mmap.mmap(f.fileno(), 0)
- f.close()
- try:
- m.resize(0) # will raise WindowsError
- except:
- pass
- try:
- m[:]
- except:
- pass
- m.close()
+ # Should not crash (Issue 5385)
+ with open(TESTFN, "wb") as fp:
+ fp.write(b"x"*10)
+ f = open(TESTFN, "r+b")
+ m = mmap.mmap(f.fileno(), 0)
+ f.close()
+ try:
+ m.resize(0) # will raise WindowsError
+ except:
+ pass
+ try:
+ m[:]
+ except:
+ 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()
+ @unittest.skipUnless(os.name == 'nt', 'requires Windows')
+ 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: