Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1 | from test_support import verify, vereq, TESTFN |
Andrew M. Kuchling | e81b9cf | 2000-03-30 21:15:29 +0000 | [diff] [blame] | 2 | import mmap |
Fred Drake | 6278799 | 2001-05-11 14:29:21 +0000 | [diff] [blame] | 3 | import os, re |
Andrew M. Kuchling | e81b9cf | 2000-03-30 21:15:29 +0000 | [diff] [blame] | 4 | |
| 5 | PAGESIZE = mmap.PAGESIZE |
| 6 | |
Guido van Rossum | 767e775 | 2000-03-31 01:09:14 +0000 | [diff] [blame] | 7 | def test_both(): |
| 8 | "Test mmap module on Unix systems and Windows" |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 9 | |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 10 | # Create a file to be mmap'ed. |
Fred Drake | 6278799 | 2001-05-11 14:29:21 +0000 | [diff] [blame] | 11 | if os.path.exists(TESTFN): |
| 12 | os.unlink(TESTFN) |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 13 | f = open(TESTFN, 'w+') |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 14 | |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 15 | try: # unlink TESTFN no matter what |
| 16 | # Write 2 pages worth of data to the file |
| 17 | f.write('\0'* PAGESIZE) |
| 18 | f.write('foo') |
| 19 | f.write('\0'* (PAGESIZE-3) ) |
Guido van Rossum | 767e775 | 2000-03-31 01:09:14 +0000 | [diff] [blame] | 20 | |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 21 | m = mmap.mmap(f.fileno(), 2 * PAGESIZE) |
| 22 | f.close() |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 23 | |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 24 | # Simple sanity checks |
Tim Peters | 2caf8df | 2001-01-14 05:05:51 +0000 | [diff] [blame] | 25 | |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 26 | print type(m) # SF bug 128713: segfaulted on Linux |
| 27 | print ' Position of foo:', m.find('foo') / float(PAGESIZE), 'pages' |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 28 | vereq(m.find('foo'), PAGESIZE) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 29 | |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 30 | print ' Length of file:', len(m) / float(PAGESIZE), 'pages' |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 31 | vereq(len(m), 2*PAGESIZE) |
Andrew M. Kuchling | e81b9cf | 2000-03-30 21:15:29 +0000 | [diff] [blame] | 32 | |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 33 | print ' Contents of byte 0:', repr(m[0]) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 34 | vereq(m[0], '\0') |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 35 | print ' Contents of first 3 bytes:', repr(m[0:3]) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 36 | vereq(m[0:3], '\0\0\0') |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 37 | |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 38 | # Modify the file's content |
| 39 | print "\n Modifying file's content..." |
| 40 | m[0] = '3' |
Fred Drake | 6278799 | 2001-05-11 14:29:21 +0000 | [diff] [blame] | 41 | m[PAGESIZE +3: PAGESIZE +3+3] = 'bar' |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 42 | |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 43 | # Check that the modification worked |
| 44 | print ' Contents of byte 0:', repr(m[0]) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 45 | vereq(m[0], '3') |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 46 | print ' Contents of first 3 bytes:', repr(m[0:3]) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 47 | vereq(m[0:3], '3\0\0') |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 48 | print ' Contents of second page:', repr(m[PAGESIZE-1 : PAGESIZE + 7]) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 49 | vereq(m[PAGESIZE-1 : PAGESIZE + 7], '\0foobar\0') |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 50 | |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 51 | m.flush() |
Andrew M. Kuchling | e81b9cf | 2000-03-30 21:15:29 +0000 | [diff] [blame] | 52 | |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 53 | # Test doing a regular expression match in an mmap'ed file |
Fred Drake | 6278799 | 2001-05-11 14:29:21 +0000 | [diff] [blame] | 54 | match = re.search('[A-Za-z]+', m) |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 55 | if match is None: |
| 56 | print ' ERROR: regex match on mmap failed!' |
| 57 | else: |
| 58 | start, end = match.span(0) |
| 59 | length = end - start |
Andrew M. Kuchling | e81b9cf | 2000-03-30 21:15:29 +0000 | [diff] [blame] | 60 | |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 61 | print ' Regex match on mmap (page start, length of match):', |
| 62 | print start / float(PAGESIZE), length |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 63 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 64 | vereq(start, PAGESIZE) |
| 65 | vereq(end, PAGESIZE + 6) |
Andrew M. Kuchling | cf70ea6 | 2000-06-18 04:47:08 +0000 | [diff] [blame] | 66 | |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 67 | # test seeking around (try to overflow the seek implementation) |
| 68 | m.seek(0,0) |
| 69 | print ' Seek to zeroth byte' |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 70 | vereq(m.tell(), 0) |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 71 | m.seek(42,1) |
| 72 | print ' Seek to 42nd byte' |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 73 | vereq(m.tell(), 42) |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 74 | m.seek(0,2) |
| 75 | print ' Seek to last byte' |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 76 | vereq(m.tell(), len(m)) |
Peter Schneider-Kamp | 7a11671 | 2000-07-11 11:24:41 +0000 | [diff] [blame] | 77 | |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 78 | print ' Try to seek to negative position...' |
Thomas Wouters | a701597 | 2000-07-30 15:38:35 +0000 | [diff] [blame] | 79 | try: |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 80 | m.seek(-1) |
Thomas Wouters | a701597 | 2000-07-30 15:38:35 +0000 | [diff] [blame] | 81 | except ValueError: |
| 82 | pass |
| 83 | else: |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 84 | verify(0, 'expected a ValueError but did not get it') |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 85 | |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 86 | print ' Try to seek beyond end of mmap...' |
| 87 | try: |
| 88 | m.seek(1,2) |
| 89 | except ValueError: |
| 90 | pass |
| 91 | else: |
| 92 | verify(0, 'expected a ValueError but did not get it') |
| 93 | |
| 94 | print ' Try to seek to negative position...' |
| 95 | try: |
| 96 | m.seek(-len(m)-1,2) |
| 97 | except ValueError: |
| 98 | pass |
| 99 | else: |
| 100 | verify(0, 'expected a ValueError but did not get it') |
| 101 | |
| 102 | # Try resizing map |
| 103 | print ' Attempting resize()' |
| 104 | try: |
| 105 | m.resize( 512 ) |
| 106 | except SystemError: |
| 107 | # resize() not supported |
| 108 | # No messages are printed, since the output of this test suite |
| 109 | # would then be different across platforms. |
| 110 | pass |
| 111 | else: |
| 112 | # resize() is supported |
| 113 | verify(len(m) == 512, |
| 114 | "len(m) is %d, but expecting 512" % (len(m),) ) |
| 115 | # Check that we can no longer seek beyond the new size. |
| 116 | try: |
| 117 | m.seek(513,0) |
| 118 | except ValueError: |
| 119 | pass |
| 120 | else: |
| 121 | verify(0, 'Could seek beyond the new size') |
| 122 | |
| 123 | m.close() |
| 124 | |
| 125 | finally: |
| 126 | try: |
| 127 | f.close() |
| 128 | except OSError: |
| 129 | pass |
| 130 | try: |
Fred Drake | 6278799 | 2001-05-11 14:29:21 +0000 | [diff] [blame] | 131 | os.unlink(TESTFN) |
Tim Peters | fd69208 | 2001-05-10 20:03:04 +0000 | [diff] [blame] | 132 | except OSError: |
| 133 | pass |
| 134 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 135 | # Test for "access" keyword parameter |
| 136 | try: |
| 137 | mapsize = 10 |
| 138 | print " Creating", mapsize, "byte test data file." |
| 139 | open(TESTFN, "wb").write("a"*mapsize) |
| 140 | print " Opening mmap with access=ACCESS_READ" |
| 141 | f = open(TESTFN, "rb") |
| 142 | m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_READ) |
| 143 | verify(m[:] == 'a'*mapsize, "Readonly memory map data incorrect.") |
| 144 | |
| 145 | print " Ensuring that readonly mmap can't be slice assigned." |
| 146 | try: |
| 147 | m[:] = 'b'*mapsize |
| 148 | except TypeError: |
| 149 | pass |
| 150 | else: |
| 151 | verify(0, "Able to write to readonly memory map") |
| 152 | |
| 153 | print " Ensuring that readonly mmap can't be item assigned." |
| 154 | try: |
| 155 | m[0] = 'b' |
| 156 | except TypeError: |
| 157 | pass |
| 158 | else: |
| 159 | verify(0, "Able to write to readonly memory map") |
| 160 | |
| 161 | print " Ensuring that readonly mmap can't be write() to." |
| 162 | try: |
| 163 | m.seek(0,0) |
| 164 | m.write('abc') |
| 165 | except TypeError: |
| 166 | pass |
| 167 | else: |
| 168 | verify(0, "Able to write to readonly memory map") |
| 169 | |
| 170 | print " Ensuring that readonly mmap can't be write_byte() to." |
| 171 | try: |
| 172 | m.seek(0,0) |
| 173 | m.write_byte('d') |
| 174 | except TypeError: |
| 175 | pass |
| 176 | else: |
| 177 | verify(0, "Able to write to readonly memory map") |
| 178 | |
| 179 | print " Ensuring that readonly mmap can't be resized." |
| 180 | try: |
| 181 | m.resize(2*mapsize) |
| 182 | except SystemError: # resize is not universally supported |
| 183 | pass |
| 184 | except TypeError: |
| 185 | pass |
| 186 | else: |
| 187 | verify(0, "Able to resize readonly memory map") |
| 188 | del m, f |
| 189 | verify(open(TESTFN, "rb").read() == 'a'*mapsize, |
| 190 | "Readonly memory map data file was modified") |
| 191 | |
| 192 | print " Opening mmap with access=ACCESS_WRITE" |
| 193 | f = open(TESTFN, "r+b") |
| 194 | m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_WRITE) |
| 195 | print " Modifying write-through memory map." |
| 196 | m[:] = 'c'*mapsize |
| 197 | verify(m[:] == 'c'*mapsize, |
| 198 | "Write-through memory map memory not updated properly.") |
| 199 | m.flush() |
| 200 | del m, f |
| 201 | verify(open(TESTFN).read() == 'c'*mapsize, |
| 202 | "Write-through memory map data file not updated properly.") |
| 203 | |
| 204 | print " Opening mmap with access=ACCESS_COPY" |
| 205 | f = open(TESTFN, "r+b") |
| 206 | m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_COPY) |
| 207 | print " Modifying copy-on-write memory map." |
| 208 | m[:] = 'd'*mapsize |
| 209 | verify(m[:] == 'd' * mapsize, |
| 210 | "Copy-on-write memory map data not written correctly.") |
| 211 | m.flush() |
| 212 | verify(open(TESTFN, "rb").read() == 'c'*mapsize, |
| 213 | "Copy-on-write test data file should not be modified.") |
| 214 | try: |
| 215 | print " Ensuring copy-on-write maps cannot be resized." |
| 216 | m.resize(2*mapsize) |
| 217 | except TypeError: |
| 218 | pass |
| 219 | else: |
| 220 | verify(0, "Copy-on-write mmap resize did not raise exception.") |
| 221 | del m, f |
| 222 | try: |
| 223 | print " Ensuring invalid access parameter raises exception." |
| 224 | f = open(TESTFN, "r+b") |
| 225 | m = mmap.mmap(f.fileno(), mapsize, access=4) |
| 226 | except ValueError: |
| 227 | pass |
| 228 | else: |
| 229 | verify(0, "Invalid access code should have raised exception.") |
| 230 | |
| 231 | if os.name == "posix": |
Tim Peters | 00cafa0 | 2001-11-13 23:39:47 +0000 | [diff] [blame] | 232 | # Try incompatible flags, prot and access parameters. |
| 233 | f = open(TESTFN, "r+b") |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 234 | try: |
| 235 | m = mmap.mmap(f.fileno(), mapsize, flags=mmap.MAP_PRIVATE, |
| 236 | prot=mmap.PROT_READ, access=mmap.ACCESS_WRITE) |
| 237 | except ValueError: |
| 238 | pass |
| 239 | else: |
| 240 | verify(0, "Incompatible parameters should raise ValueError.") |
| 241 | finally: |
| 242 | try: |
| 243 | os.unlink(TESTFN) |
| 244 | except OSError: |
| 245 | pass |
| 246 | |
Tim Peters | c9ffa06 | 2002-03-08 05:43:32 +0000 | [diff] [blame] | 247 | # Do a tougher .find() test. SF bug 515943 pointed out that, in 2.2, |
| 248 | # searching for data with embedded \0 bytes didn't work. |
| 249 | f = open(TESTFN, 'w+') |
| 250 | |
| 251 | try: # unlink TESTFN no matter what |
| 252 | data = 'aabaac\x00deef\x00\x00aa\x00' |
| 253 | n = len(data) |
| 254 | f.write(data) |
| 255 | m = mmap.mmap(f.fileno(), n) |
| 256 | f.close() |
| 257 | |
| 258 | for start in range(n+1): |
| 259 | for finish in range(start, n+1): |
| 260 | slice = data[start : finish] |
| 261 | vereq(m.find(slice), data.find(slice)) |
| 262 | vereq(m.find(slice + 'x'), -1) |
| 263 | |
| 264 | finally: |
| 265 | try: |
| 266 | os.unlink(TESTFN) |
| 267 | except OSError: |
| 268 | pass |
| 269 | |
| 270 | |
| 271 | |
Andrew M. Kuchling | e81b9cf | 2000-03-30 21:15:29 +0000 | [diff] [blame] | 272 | print ' Test passed' |
| 273 | |
Guido van Rossum | 767e775 | 2000-03-31 01:09:14 +0000 | [diff] [blame] | 274 | test_both() |