summaryrefslogtreecommitdiffstats
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.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py
index f2232e4..974fde2 100644
--- a/Lib/test/test_mmap.py
+++ b/Lib/test/test_mmap.py
@@ -339,6 +339,50 @@ class MmapTests(unittest.TestCase):
m[start:stop:step] = data
self.assertEquals(m[:], bytes(L))
+ def make_mmap_file (self, f, halfsize):
+ # Write 2 pages worth of data to the file
+ f.write (b'\0' * halfsize)
+ f.write (b'foo')
+ f.write (b'\0' * (halfsize - 3))
+ f.flush ()
+ return mmap.mmap (f.fileno(), 0)
+
+ def test_offset (self):
+ f = open (TESTFN, 'w+b')
+
+ try: # unlink TESTFN no matter what
+ halfsize = mmap.ALLOCATIONGRANULARITY
+ m = self.make_mmap_file (f, halfsize)
+ m.close ()
+ f.close ()
+
+ mapsize = halfsize * 2
+ # Try invalid offset
+ f = open(TESTFN, "r+b")
+ for offset in [-2, -1, None]:
+ try:
+ m = mmap.mmap(f.fileno(), mapsize, offset=offset)
+ self.assertEqual(0, 1)
+ except (ValueError, TypeError, OverflowError):
+ pass
+ else:
+ self.assertEqual(0, 0)
+ f.close()
+
+ # Try valid offset, hopefully 8192 works on all OSes
+ f = open(TESTFN, "r+b")
+ m = mmap.mmap(f.fileno(), mapsize - halfsize, offset=halfsize)
+ self.assertEqual(m[0:3], b'foo')
+ f.close()
+ m.close()
+
+ finally:
+ f.close()
+ try:
+ os.unlink(TESTFN)
+ except OSError:
+ pass
+
def test_main():
run_unittest(MmapTests)