Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1 | from test.test_support import TESTFN, run_unittest |
Andrew M. Kuchling | e81b9cf | 2000-03-30 21:15:29 +0000 | [diff] [blame] | 2 | import mmap |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3 | import unittest |
Fred Drake | 6278799 | 2001-05-11 14:29:21 +0000 | [diff] [blame] | 4 | import os, re |
Andrew M. Kuchling | e81b9cf | 2000-03-30 21:15:29 +0000 | [diff] [blame] | 5 | |
| 6 | PAGESIZE = mmap.PAGESIZE |
| 7 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8 | class MmapTests(unittest.TestCase): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 9 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 10 | def setUp(self): |
| 11 | if os.path.exists(TESTFN): |
| 12 | os.unlink(TESTFN) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 13 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 14 | def tearDown(self): |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 15 | try: |
Fred Drake | 6278799 | 2001-05-11 14:29:21 +0000 | [diff] [blame] | 16 | os.unlink(TESTFN) |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 17 | except OSError: |
| 18 | pass |
| 19 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 20 | def test_basic(self): |
| 21 | # Test mmap module on Unix systems and Windows |
| 22 | |
| 23 | # Create a file to be mmap'ed. |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 24 | f = open(TESTFN, 'bw+') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 25 | try: |
| 26 | # Write 2 pages worth of data to the file |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 27 | f.write(b'\0'* PAGESIZE) |
| 28 | f.write(b'foo') |
| 29 | f.write(b'\0'* (PAGESIZE-3) ) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 30 | f.flush() |
| 31 | m = mmap.mmap(f.fileno(), 2 * PAGESIZE) |
Guido van Rossum | 456fe5d | 2007-07-16 19:42:05 +0000 | [diff] [blame] | 32 | finally: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 33 | f.close() |
| 34 | |
Guido van Rossum | 456fe5d | 2007-07-16 19:42:05 +0000 | [diff] [blame] | 35 | # Simple sanity checks |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 456fe5d | 2007-07-16 19:42:05 +0000 | [diff] [blame] | 37 | tp = str(type(m)) # SF bug 128713: segfaulted on Linux |
| 38 | self.assertEqual(m.find('foo'), PAGESIZE) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 39 | |
Guido van Rossum | 456fe5d | 2007-07-16 19:42:05 +0000 | [diff] [blame] | 40 | self.assertEqual(len(m), 2*PAGESIZE) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 42 | self.assertEqual(m[0], 0) |
Guido van Rossum | 456fe5d | 2007-07-16 19:42:05 +0000 | [diff] [blame] | 43 | self.assertEqual(m[0:3], b'\0\0\0') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | 456fe5d | 2007-07-16 19:42:05 +0000 | [diff] [blame] | 45 | # Modify the file's content |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 46 | m[0] = b'3'[0] |
Guido van Rossum | 456fe5d | 2007-07-16 19:42:05 +0000 | [diff] [blame] | 47 | m[PAGESIZE +3: PAGESIZE +3+3] = b'bar' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | 456fe5d | 2007-07-16 19:42:05 +0000 | [diff] [blame] | 49 | # Check that the modification worked |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 50 | self.assertEqual(m[0], b'3'[0]) |
Guido van Rossum | 456fe5d | 2007-07-16 19:42:05 +0000 | [diff] [blame] | 51 | self.assertEqual(m[0:3], b'3\0\0') |
| 52 | self.assertEqual(m[PAGESIZE-1 : PAGESIZE + 7], b'\0foobar\0') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | 456fe5d | 2007-07-16 19:42:05 +0000 | [diff] [blame] | 54 | m.flush() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | 456fe5d | 2007-07-16 19:42:05 +0000 | [diff] [blame] | 56 | # Test doing a regular expression match in an mmap'ed file |
| 57 | match = re.search('[A-Za-z]+', m) |
| 58 | if match is None: |
| 59 | self.fail('regex match on mmap failed!') |
| 60 | else: |
| 61 | start, end = match.span(0) |
| 62 | length = end - start |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | 456fe5d | 2007-07-16 19:42:05 +0000 | [diff] [blame] | 64 | self.assertEqual(start, PAGESIZE) |
| 65 | self.assertEqual(end, PAGESIZE + 6) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | 456fe5d | 2007-07-16 19:42:05 +0000 | [diff] [blame] | 67 | # test seeking around (try to overflow the seek implementation) |
| 68 | m.seek(0,0) |
| 69 | self.assertEqual(m.tell(), 0) |
| 70 | m.seek(42,1) |
| 71 | self.assertEqual(m.tell(), 42) |
| 72 | m.seek(0,2) |
| 73 | self.assertEqual(m.tell(), len(m)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 456fe5d | 2007-07-16 19:42:05 +0000 | [diff] [blame] | 75 | # Try to seek to negative position... |
| 76 | self.assertRaises(ValueError, m.seek, -1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | 456fe5d | 2007-07-16 19:42:05 +0000 | [diff] [blame] | 78 | # Try to seek beyond end of mmap... |
| 79 | self.assertRaises(ValueError, m.seek, 1, 2) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | 456fe5d | 2007-07-16 19:42:05 +0000 | [diff] [blame] | 81 | # Try to seek to negative position... |
| 82 | self.assertRaises(ValueError, m.seek, -len(m)-1, 2) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | 456fe5d | 2007-07-16 19:42:05 +0000 | [diff] [blame] | 84 | # Try resizing map |
| 85 | try: |
| 86 | m.resize(512) |
| 87 | except SystemError: |
| 88 | # resize() not supported |
| 89 | # No messages are printed, since the output of this test suite |
| 90 | # would then be different across platforms. |
| 91 | pass |
| 92 | else: |
| 93 | # resize() is supported |
| 94 | self.assertEqual(len(m), 512) |
| 95 | # Check that we can no longer seek beyond the new size. |
| 96 | self.assertRaises(ValueError, m.seek, 513, 0) |
| 97 | |
| 98 | # Check that the underlying file is truncated too |
| 99 | # (bug #728515) |
| 100 | f = open(TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 101 | try: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 102 | f.seek(0, 2) |
| 103 | self.assertEqual(f.tell(), 512) |
Guido van Rossum | 456fe5d | 2007-07-16 19:42:05 +0000 | [diff] [blame] | 104 | finally: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 105 | f.close() |
Guido van Rossum | 456fe5d | 2007-07-16 19:42:05 +0000 | [diff] [blame] | 106 | self.assertEqual(m.size(), 512) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 107 | |
Guido van Rossum | 456fe5d | 2007-07-16 19:42:05 +0000 | [diff] [blame] | 108 | m.close() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 109 | |
| 110 | def test_access_parameter(self): |
| 111 | # Test for "access" keyword parameter |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 112 | mapsize = 10 |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 113 | open(TESTFN, "wb").write(b"a"*mapsize) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 114 | f = open(TESTFN, "rb") |
| 115 | m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_READ) |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 116 | self.assertEqual(m[:], b'a'*mapsize, "Readonly memory map data incorrect.") |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 117 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 118 | # Ensuring that readonly mmap can't be slice assigned |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 119 | try: |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 120 | m[:] = b'b'*mapsize |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 121 | except TypeError: |
| 122 | pass |
| 123 | else: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 124 | self.fail("Able to write to readonly memory map") |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 125 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 126 | # Ensuring that readonly mmap can't be item assigned |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 127 | try: |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 128 | m[0] = b'b' |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 129 | except TypeError: |
| 130 | pass |
| 131 | else: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 132 | self.fail("Able to write to readonly memory map") |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 133 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 134 | # Ensuring that readonly mmap can't be write() to |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 135 | try: |
| 136 | m.seek(0,0) |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 137 | m.write(b'abc') |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 138 | except TypeError: |
| 139 | pass |
| 140 | else: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 141 | self.fail("Able to write to readonly memory map") |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 142 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 143 | # Ensuring that readonly mmap can't be write_byte() to |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 144 | try: |
| 145 | m.seek(0,0) |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 146 | m.write_byte(b'd') |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 147 | except TypeError: |
| 148 | pass |
| 149 | else: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 150 | self.fail("Able to write to readonly memory map") |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 151 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 152 | # Ensuring that readonly mmap can't be resized |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 153 | try: |
| 154 | m.resize(2*mapsize) |
| 155 | except SystemError: # resize is not universally supported |
| 156 | pass |
| 157 | except TypeError: |
| 158 | pass |
| 159 | else: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 160 | self.fail("Able to resize readonly memory map") |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 161 | del m, f |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 162 | self.assertEqual(open(TESTFN, "rb").read(), b'a'*mapsize, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 163 | "Readonly memory map data file was modified") |
| 164 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 165 | # Opening mmap with size too big |
Neal Norwitz | b567392 | 2002-09-05 21:48:07 +0000 | [diff] [blame] | 166 | import sys |
| 167 | f = open(TESTFN, "r+b") |
| 168 | try: |
| 169 | m = mmap.mmap(f.fileno(), mapsize+1) |
| 170 | except ValueError: |
| 171 | # we do not expect a ValueError on Windows |
Tim Peters | 4f4f4d7 | 2002-09-10 20:49:15 +0000 | [diff] [blame] | 172 | # CAUTION: This also changes the size of the file on disk, and |
| 173 | # later tests assume that the length hasn't changed. We need to |
| 174 | # repair that. |
Neal Norwitz | b567392 | 2002-09-05 21:48:07 +0000 | [diff] [blame] | 175 | if sys.platform.startswith('win'): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 176 | self.fail("Opening mmap with size+1 should work on Windows.") |
Neal Norwitz | b567392 | 2002-09-05 21:48:07 +0000 | [diff] [blame] | 177 | else: |
| 178 | # we expect a ValueError on Unix, but not on Windows |
| 179 | if not sys.platform.startswith('win'): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 180 | self.fail("Opening mmap with size+1 should raise ValueError.") |
Barry Warsaw | ccd9e75 | 2002-09-11 02:56:42 +0000 | [diff] [blame] | 181 | m.close() |
Tim Peters | 4f4f4d7 | 2002-09-10 20:49:15 +0000 | [diff] [blame] | 182 | f.close() |
| 183 | if sys.platform.startswith('win'): |
| 184 | # Repair damage from the resizing test. |
| 185 | f = open(TESTFN, 'r+b') |
| 186 | f.truncate(mapsize) |
| 187 | f.close() |
Neal Norwitz | b567392 | 2002-09-05 21:48:07 +0000 | [diff] [blame] | 188 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 189 | # Opening mmap with access=ACCESS_WRITE |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 190 | f = open(TESTFN, "r+b") |
| 191 | m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_WRITE) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 192 | # Modifying write-through memory map |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 193 | m[:] = b'c'*mapsize |
| 194 | self.assertEqual(m[:], b'c'*mapsize, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 195 | "Write-through memory map memory not updated properly.") |
| 196 | m.flush() |
Tim Peters | 1b5112a | 2002-09-10 21:19:55 +0000 | [diff] [blame] | 197 | m.close() |
| 198 | f.close() |
Tim Peters | 4f4f4d7 | 2002-09-10 20:49:15 +0000 | [diff] [blame] | 199 | f = open(TESTFN, 'rb') |
| 200 | stuff = f.read() |
| 201 | f.close() |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 202 | self.assertEqual(stuff, b'c'*mapsize, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 203 | "Write-through memory map data file not updated properly.") |
| 204 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 205 | # Opening mmap with access=ACCESS_COPY |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 206 | f = open(TESTFN, "r+b") |
| 207 | m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_COPY) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 208 | # Modifying copy-on-write memory map |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 209 | m[:] = b'd'*mapsize |
| 210 | self.assertEqual(m[:], b'd' * mapsize, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 211 | "Copy-on-write memory map data not written correctly.") |
| 212 | m.flush() |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 213 | self.assertEqual(open(TESTFN, "rb").read(), b'c'*mapsize, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 214 | "Copy-on-write test data file should not be modified.") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 215 | # Ensuring copy-on-write maps cannot be resized |
| 216 | self.assertRaises(TypeError, m.resize, 2*mapsize) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 217 | del m, f |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 218 | |
| 219 | # Ensuring invalid access parameter raises exception |
| 220 | f = open(TESTFN, "r+b") |
| 221 | self.assertRaises(ValueError, mmap.mmap, f.fileno(), mapsize, access=4) |
| 222 | f.close() |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 223 | |
| 224 | if os.name == "posix": |
Tim Peters | 00cafa0 | 2001-11-13 23:39:47 +0000 | [diff] [blame] | 225 | # Try incompatible flags, prot and access parameters. |
| 226 | f = open(TESTFN, "r+b") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 227 | self.assertRaises(ValueError, mmap.mmap, f.fileno(), mapsize, |
| 228 | flags=mmap.MAP_PRIVATE, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 229 | prot=mmap.PROT_READ, access=mmap.ACCESS_WRITE) |
Tim Peters | 5379dea | 2002-04-18 04:30:18 +0000 | [diff] [blame] | 230 | f.close() |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 231 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 232 | def test_bad_file_desc(self): |
| 233 | # Try opening a bad file descriptor... |
| 234 | self.assertRaises(mmap.error, mmap.mmap, -2, 4096) |
Neal Norwitz | 3b4fff8 | 2006-01-11 08:54:45 +0000 | [diff] [blame] | 235 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 236 | def test_tougher_find(self): |
| 237 | # Do a tougher .find() test. SF bug 515943 pointed out that, in 2.2, |
| 238 | # searching for data with embedded \0 bytes didn't work. |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 239 | f = open(TESTFN, 'wb+') |
Tim Peters | c9ffa06 | 2002-03-08 05:43:32 +0000 | [diff] [blame] | 240 | |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 241 | data = b'aabaac\x00deef\x00\x00aa\x00' |
Tim Peters | c9ffa06 | 2002-03-08 05:43:32 +0000 | [diff] [blame] | 242 | n = len(data) |
| 243 | f.write(data) |
Tim Peters | 5379dea | 2002-04-18 04:30:18 +0000 | [diff] [blame] | 244 | f.flush() |
Tim Peters | c9ffa06 | 2002-03-08 05:43:32 +0000 | [diff] [blame] | 245 | m = mmap.mmap(f.fileno(), n) |
| 246 | f.close() |
| 247 | |
| 248 | for start in range(n+1): |
| 249 | for finish in range(start, n+1): |
| 250 | slice = data[start : finish] |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 251 | self.assertEqual(m.find(slice), data.find(slice)) |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 252 | self.assertEqual(m.find(slice + b'x'), -1) |
Tim Peters | ddc82ea | 2003-01-13 21:38:45 +0000 | [diff] [blame] | 253 | m.close() |
Tim Peters | c9ffa06 | 2002-03-08 05:43:32 +0000 | [diff] [blame] | 254 | |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 255 | def test_find_end(self): |
| 256 | # test the new 'end' parameter works as expected |
| 257 | f = open(TESTFN, 'w+') |
| 258 | data = 'one two ones' |
| 259 | n = len(data) |
| 260 | f.write(data) |
| 261 | f.flush() |
| 262 | m = mmap.mmap(f.fileno(), n) |
| 263 | f.close() |
| 264 | |
| 265 | self.assertEqual(m.find('one'), 0) |
| 266 | self.assertEqual(m.find('ones'), 8) |
| 267 | self.assertEqual(m.find('one', 0, -1), 0) |
| 268 | self.assertEqual(m.find('one', 1), 8) |
| 269 | self.assertEqual(m.find('one', 1, -1), 8) |
| 270 | self.assertEqual(m.find('one', 1, -2), -1) |
| 271 | |
| 272 | |
| 273 | def test_rfind(self): |
| 274 | # test the new 'end' parameter works as expected |
| 275 | f = open(TESTFN, 'w+') |
| 276 | data = 'one two ones' |
| 277 | n = len(data) |
| 278 | f.write(data) |
| 279 | f.flush() |
| 280 | m = mmap.mmap(f.fileno(), n) |
| 281 | f.close() |
| 282 | |
| 283 | self.assertEqual(m.rfind('one'), 8) |
| 284 | self.assertEqual(m.rfind('one '), 0) |
| 285 | self.assertEqual(m.rfind('one', 0, -1), 8) |
| 286 | self.assertEqual(m.rfind('one', 0, -2), 0) |
| 287 | self.assertEqual(m.rfind('one', 1, -1), 8) |
| 288 | self.assertEqual(m.rfind('one', 1, -2), -1) |
| 289 | |
| 290 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 291 | def test_double_close(self): |
| 292 | # make sure a double close doesn't crash on Solaris (Bug# 665913) |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 293 | f = open(TESTFN, 'wb+') |
Tim Peters | c9ffa06 | 2002-03-08 05:43:32 +0000 | [diff] [blame] | 294 | |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 295 | f.write(2**16 * b'a') # Arbitrary character |
Neal Norwitz | e604c02 | 2003-01-10 20:52:16 +0000 | [diff] [blame] | 296 | f.close() |
| 297 | |
| 298 | f = open(TESTFN) |
Tim Peters | ddc82ea | 2003-01-13 21:38:45 +0000 | [diff] [blame] | 299 | mf = mmap.mmap(f.fileno(), 2**16, access=mmap.ACCESS_READ) |
Neal Norwitz | e604c02 | 2003-01-10 20:52:16 +0000 | [diff] [blame] | 300 | mf.close() |
| 301 | mf.close() |
| 302 | f.close() |
| 303 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 304 | def test_entire_file(self): |
| 305 | # test mapping of entire file by passing 0 for map length |
| 306 | if hasattr(os, "stat"): |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 307 | f = open(TESTFN, "wb+") |
Tim Peters | c9ffa06 | 2002-03-08 05:43:32 +0000 | [diff] [blame] | 308 | |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 309 | f.write(2**16 * b'm') # Arbitrary character |
Martin v. Löwis | 7fe60c0 | 2005-03-03 11:22:44 +0000 | [diff] [blame] | 310 | f.close() |
| 311 | |
| 312 | f = open(TESTFN, "rb+") |
Tim Peters | eba28be | 2005-03-28 01:08:02 +0000 | [diff] [blame] | 313 | mf = mmap.mmap(f.fileno(), 0) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 314 | self.assertEqual(len(mf), 2**16, "Map size should equal file size.") |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 315 | self.assertEqual(mf.read(2**16), 2**16 * b"m") |
Martin v. Löwis | 7fe60c0 | 2005-03-03 11:22:44 +0000 | [diff] [blame] | 316 | mf.close() |
| 317 | f.close() |
| 318 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 319 | def test_move(self): |
| 320 | # make move works everywhere (64-bit format problem earlier) |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 321 | f = open(TESTFN, 'wb+') |
Tim Peters | eba28be | 2005-03-28 01:08:02 +0000 | [diff] [blame] | 322 | |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 323 | f.write(b"ABCDEabcde") # Arbitrary character |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 324 | f.flush() |
| 325 | |
| 326 | mf = mmap.mmap(f.fileno(), 10) |
| 327 | mf.move(5, 0, 5) |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 328 | self.assertEqual(mf[:], b"ABCDEABCDE", "Map move should have duplicated front 5") |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 329 | mf.close() |
| 330 | f.close() |
| 331 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 332 | def test_anonymous(self): |
| 333 | # anonymous mmap.mmap(-1, PAGE) |
| 334 | m = mmap.mmap(-1, PAGESIZE) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 335 | for x in range(PAGESIZE): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 336 | self.assertEqual(m[x], 0, |
| 337 | "anonymously mmap'ed contents should be zero") |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 338 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 339 | for x in range(PAGESIZE): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 340 | b = x & 0xff |
Guido van Rossum | b358a2c | 2007-07-16 19:29:02 +0000 | [diff] [blame] | 341 | m[x] = b |
| 342 | self.assertEqual(m[x], b) |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 343 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 344 | def test_extended_getslice(self): |
| 345 | # Test extended slicing by comparing with list slicing. |
| 346 | s = bytes(reversed(range(256))) |
| 347 | m = mmap.mmap(-1, len(s)) |
| 348 | m[:] = s |
| 349 | self.assertEqual(m[:], s) |
| 350 | indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300) |
| 351 | for start in indices: |
| 352 | for stop in indices: |
| 353 | # Skip step 0 (invalid) |
| 354 | for step in indices[1:]: |
| 355 | self.assertEqual(m[start:stop:step], |
| 356 | s[start:stop:step]) |
| 357 | |
| 358 | def test_extended_set_del_slice(self): |
| 359 | # Test extended slicing by comparing with list slicing. |
| 360 | s = bytes(reversed(range(256))) |
| 361 | m = mmap.mmap(-1, len(s)) |
| 362 | indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300) |
| 363 | for start in indices: |
| 364 | for stop in indices: |
| 365 | # Skip invalid step 0 |
| 366 | for step in indices[1:]: |
| 367 | m[:] = s |
| 368 | self.assertEqual(m[:], s) |
| 369 | L = list(s) |
| 370 | # Make sure we have a slice of exactly the right length, |
| 371 | # but with different data. |
| 372 | data = L[start:stop:step] |
| 373 | data = bytes(reversed(data)) |
| 374 | L[start:stop:step] = data |
| 375 | m[start:stop:step] = data |
| 376 | self.assertEquals(m[:], bytes(L)) |
| 377 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 378 | def make_mmap_file (self, f, halfsize): |
| 379 | # Write 2 pages worth of data to the file |
| 380 | f.write (b'\0' * halfsize) |
| 381 | f.write (b'foo') |
| 382 | f.write (b'\0' * (halfsize - 3)) |
| 383 | f.flush () |
| 384 | return mmap.mmap (f.fileno(), 0) |
| 385 | |
| 386 | def test_offset (self): |
| 387 | f = open (TESTFN, 'w+b') |
| 388 | |
| 389 | try: # unlink TESTFN no matter what |
| 390 | halfsize = mmap.ALLOCATIONGRANULARITY |
| 391 | m = self.make_mmap_file (f, halfsize) |
| 392 | m.close () |
| 393 | f.close () |
| 394 | |
| 395 | mapsize = halfsize * 2 |
| 396 | # Try invalid offset |
| 397 | f = open(TESTFN, "r+b") |
| 398 | for offset in [-2, -1, None]: |
| 399 | try: |
| 400 | m = mmap.mmap(f.fileno(), mapsize, offset=offset) |
| 401 | self.assertEqual(0, 1) |
| 402 | except (ValueError, TypeError, OverflowError): |
| 403 | pass |
| 404 | else: |
| 405 | self.assertEqual(0, 0) |
| 406 | f.close() |
| 407 | |
| 408 | # Try valid offset, hopefully 8192 works on all OSes |
| 409 | f = open(TESTFN, "r+b") |
| 410 | m = mmap.mmap(f.fileno(), mapsize - halfsize, offset=halfsize) |
| 411 | self.assertEqual(m[0:3], b'foo') |
| 412 | f.close() |
| 413 | m.close() |
| 414 | |
| 415 | finally: |
| 416 | f.close() |
| 417 | try: |
| 418 | os.unlink(TESTFN) |
| 419 | except OSError: |
| 420 | pass |
| 421 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 422 | def test_subclass(self): |
| 423 | class anon_mmap(mmap.mmap): |
| 424 | def __new__(klass, *args, **kwargs): |
| 425 | return mmap.mmap.__new__(klass, -1, *args, **kwargs) |
| 426 | anon_mmap(PAGESIZE) |
| 427 | |
| 428 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 429 | def test_main(): |
| 430 | run_unittest(MmapTests) |
Andrew M. Kuchling | e81b9cf | 2000-03-30 21:15:29 +0000 | [diff] [blame] | 431 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 432 | if __name__ == '__main__': |
| 433 | test_main() |