blob: 5506956eb65817cc1bf346b9bc707b2c622936fa [file] [log] [blame]
Georg Brandl31631792006-10-29 19:13:40 +00001from test.test_support import TESTFN, run_unittest
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +00002import mmap
Georg Brandl31631792006-10-29 19:13:40 +00003import unittest
Hirokazu Yamamoto81d4b362009-03-31 20:22:13 +00004import os, re, itertools
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +00005
6PAGESIZE = mmap.PAGESIZE
7
Georg Brandl31631792006-10-29 19:13:40 +00008class MmapTests(unittest.TestCase):
Fred Drake004d5e62000-10-23 17:22:08 +00009
Georg Brandl31631792006-10-29 19:13:40 +000010 def setUp(self):
11 if os.path.exists(TESTFN):
12 os.unlink(TESTFN)
Fred Drake004d5e62000-10-23 17:22:08 +000013
Georg Brandl31631792006-10-29 19:13:40 +000014 def tearDown(self):
Tim Petersfd692082001-05-10 20:03:04 +000015 try:
Fred Drake62787992001-05-11 14:29:21 +000016 os.unlink(TESTFN)
Tim Petersfd692082001-05-10 20:03:04 +000017 except OSError:
18 pass
19
Georg Brandl31631792006-10-29 19:13:40 +000020 def test_basic(self):
21 # Test mmap module on Unix systems and Windows
22
23 # Create a file to be mmap'ed.
24 f = open(TESTFN, 'w+')
25 try:
26 # Write 2 pages worth of data to the file
27 f.write('\0'* PAGESIZE)
28 f.write('foo')
29 f.write('\0'* (PAGESIZE-3) )
30 f.flush()
31 m = mmap.mmap(f.fileno(), 2 * PAGESIZE)
32 f.close()
33
34 # Simple sanity checks
35
36 tp = str(type(m)) # SF bug 128713: segfaulted on Linux
37 self.assertEqual(m.find('foo'), PAGESIZE)
38
39 self.assertEqual(len(m), 2*PAGESIZE)
40
41 self.assertEqual(m[0], '\0')
42 self.assertEqual(m[0:3], '\0\0\0')
43
Hirokazu Yamamoto978bacd2009-02-18 15:11:55 +000044 # Shouldn't crash on boundary (Issue #5292)
45 self.assertRaises(IndexError, m.__getitem__, len(m))
46 self.assertRaises(IndexError, m.__setitem__, len(m), '\0')
47
Georg Brandl31631792006-10-29 19:13:40 +000048 # Modify the file's content
49 m[0] = '3'
50 m[PAGESIZE +3: PAGESIZE +3+3] = 'bar'
51
52 # Check that the modification worked
53 self.assertEqual(m[0], '3')
54 self.assertEqual(m[0:3], '3\0\0')
55 self.assertEqual(m[PAGESIZE-1 : PAGESIZE + 7], '\0foobar\0')
56
57 m.flush()
58
59 # Test doing a regular expression match in an mmap'ed file
60 match = re.search('[A-Za-z]+', m)
61 if match is None:
62 self.fail('regex match on mmap failed!')
63 else:
64 start, end = match.span(0)
65 length = end - start
66
67 self.assertEqual(start, PAGESIZE)
68 self.assertEqual(end, PAGESIZE + 6)
69
70 # test seeking around (try to overflow the seek implementation)
71 m.seek(0,0)
72 self.assertEqual(m.tell(), 0)
73 m.seek(42,1)
74 self.assertEqual(m.tell(), 42)
75 m.seek(0,2)
76 self.assertEqual(m.tell(), len(m))
77
78 # Try to seek to negative position...
79 self.assertRaises(ValueError, m.seek, -1)
80
81 # Try to seek beyond end of mmap...
82 self.assertRaises(ValueError, m.seek, 1, 2)
83
84 # Try to seek to negative position...
85 self.assertRaises(ValueError, m.seek, -len(m)-1, 2)
86
87 # Try resizing map
88 try:
89 m.resize(512)
90 except SystemError:
91 # resize() not supported
92 # No messages are printed, since the output of this test suite
93 # would then be different across platforms.
94 pass
95 else:
96 # resize() is supported
97 self.assertEqual(len(m), 512)
98 # Check that we can no longer seek beyond the new size.
99 self.assertRaises(ValueError, m.seek, 513, 0)
100
101 # Check that the underlying file is truncated too
102 # (bug #728515)
103 f = open(TESTFN)
104 f.seek(0, 2)
105 self.assertEqual(f.tell(), 512)
106 f.close()
107 self.assertEqual(m.size(), 512)
108
109 m.close()
110
111 finally:
112 try:
113 f.close()
114 except OSError:
115 pass
116
117 def test_access_parameter(self):
118 # Test for "access" keyword parameter
Tim Peters5ebfd362001-11-13 23:11:19 +0000119 mapsize = 10
Tim Peters5ebfd362001-11-13 23:11:19 +0000120 open(TESTFN, "wb").write("a"*mapsize)
Tim Peters5ebfd362001-11-13 23:11:19 +0000121 f = open(TESTFN, "rb")
122 m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_READ)
Georg Brandl31631792006-10-29 19:13:40 +0000123 self.assertEqual(m[:], 'a'*mapsize, "Readonly memory map data incorrect.")
Tim Peters5ebfd362001-11-13 23:11:19 +0000124
Georg Brandl31631792006-10-29 19:13:40 +0000125 # Ensuring that readonly mmap can't be slice assigned
Tim Peters5ebfd362001-11-13 23:11:19 +0000126 try:
127 m[:] = 'b'*mapsize
128 except TypeError:
129 pass
130 else:
Georg Brandl31631792006-10-29 19:13:40 +0000131 self.fail("Able to write to readonly memory map")
Tim Peters5ebfd362001-11-13 23:11:19 +0000132
Georg Brandl31631792006-10-29 19:13:40 +0000133 # Ensuring that readonly mmap can't be item assigned
Tim Peters5ebfd362001-11-13 23:11:19 +0000134 try:
135 m[0] = 'b'
136 except TypeError:
137 pass
138 else:
Georg Brandl31631792006-10-29 19:13:40 +0000139 self.fail("Able to write to readonly memory map")
Tim Peters5ebfd362001-11-13 23:11:19 +0000140
Georg Brandl31631792006-10-29 19:13:40 +0000141 # Ensuring that readonly mmap can't be write() to
Tim Peters5ebfd362001-11-13 23:11:19 +0000142 try:
143 m.seek(0,0)
144 m.write('abc')
145 except TypeError:
146 pass
147 else:
Georg Brandl31631792006-10-29 19:13:40 +0000148 self.fail("Able to write to readonly memory map")
Tim Peters5ebfd362001-11-13 23:11:19 +0000149
Georg Brandl31631792006-10-29 19:13:40 +0000150 # Ensuring that readonly mmap can't be write_byte() to
Tim Peters5ebfd362001-11-13 23:11:19 +0000151 try:
152 m.seek(0,0)
153 m.write_byte('d')
154 except TypeError:
155 pass
156 else:
Georg Brandl31631792006-10-29 19:13:40 +0000157 self.fail("Able to write to readonly memory map")
Tim Peters5ebfd362001-11-13 23:11:19 +0000158
Georg Brandl31631792006-10-29 19:13:40 +0000159 # Ensuring that readonly mmap can't be resized
Tim Peters5ebfd362001-11-13 23:11:19 +0000160 try:
161 m.resize(2*mapsize)
162 except SystemError: # resize is not universally supported
163 pass
164 except TypeError:
165 pass
166 else:
Georg Brandl31631792006-10-29 19:13:40 +0000167 self.fail("Able to resize readonly memory map")
Neal Norwitzd48a2f72008-04-01 05:40:43 +0000168 f.close()
Tim Peters5ebfd362001-11-13 23:11:19 +0000169 del m, f
Georg Brandl31631792006-10-29 19:13:40 +0000170 self.assertEqual(open(TESTFN, "rb").read(), 'a'*mapsize,
Tim Peters5ebfd362001-11-13 23:11:19 +0000171 "Readonly memory map data file was modified")
172
Georg Brandl31631792006-10-29 19:13:40 +0000173 # Opening mmap with size too big
Neal Norwitzb5673922002-09-05 21:48:07 +0000174 import sys
175 f = open(TESTFN, "r+b")
176 try:
177 m = mmap.mmap(f.fileno(), mapsize+1)
178 except ValueError:
179 # we do not expect a ValueError on Windows
Tim Peters4f4f4d72002-09-10 20:49:15 +0000180 # CAUTION: This also changes the size of the file on disk, and
181 # later tests assume that the length hasn't changed. We need to
182 # repair that.
Neal Norwitzb5673922002-09-05 21:48:07 +0000183 if sys.platform.startswith('win'):
Georg Brandl31631792006-10-29 19:13:40 +0000184 self.fail("Opening mmap with size+1 should work on Windows.")
Neal Norwitzb5673922002-09-05 21:48:07 +0000185 else:
186 # we expect a ValueError on Unix, but not on Windows
187 if not sys.platform.startswith('win'):
Georg Brandl31631792006-10-29 19:13:40 +0000188 self.fail("Opening mmap with size+1 should raise ValueError.")
Barry Warsawccd9e752002-09-11 02:56:42 +0000189 m.close()
Tim Peters4f4f4d72002-09-10 20:49:15 +0000190 f.close()
191 if sys.platform.startswith('win'):
192 # Repair damage from the resizing test.
193 f = open(TESTFN, 'r+b')
194 f.truncate(mapsize)
195 f.close()
Neal Norwitzb5673922002-09-05 21:48:07 +0000196
Georg Brandl31631792006-10-29 19:13:40 +0000197 # Opening mmap with access=ACCESS_WRITE
Tim Peters5ebfd362001-11-13 23:11:19 +0000198 f = open(TESTFN, "r+b")
199 m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_WRITE)
Georg Brandl31631792006-10-29 19:13:40 +0000200 # Modifying write-through memory map
Tim Peters5ebfd362001-11-13 23:11:19 +0000201 m[:] = 'c'*mapsize
Georg Brandl31631792006-10-29 19:13:40 +0000202 self.assertEqual(m[:], 'c'*mapsize,
Tim Peters5ebfd362001-11-13 23:11:19 +0000203 "Write-through memory map memory not updated properly.")
204 m.flush()
Tim Peters1b5112a2002-09-10 21:19:55 +0000205 m.close()
206 f.close()
Tim Peters4f4f4d72002-09-10 20:49:15 +0000207 f = open(TESTFN, 'rb')
208 stuff = f.read()
209 f.close()
Georg Brandl31631792006-10-29 19:13:40 +0000210 self.assertEqual(stuff, 'c'*mapsize,
Tim Peters5ebfd362001-11-13 23:11:19 +0000211 "Write-through memory map data file not updated properly.")
212
Georg Brandl31631792006-10-29 19:13:40 +0000213 # Opening mmap with access=ACCESS_COPY
Tim Peters5ebfd362001-11-13 23:11:19 +0000214 f = open(TESTFN, "r+b")
215 m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_COPY)
Georg Brandl31631792006-10-29 19:13:40 +0000216 # Modifying copy-on-write memory map
Tim Peters5ebfd362001-11-13 23:11:19 +0000217 m[:] = 'd'*mapsize
Georg Brandl31631792006-10-29 19:13:40 +0000218 self.assertEqual(m[:], 'd' * mapsize,
Tim Peters5ebfd362001-11-13 23:11:19 +0000219 "Copy-on-write memory map data not written correctly.")
220 m.flush()
Georg Brandl31631792006-10-29 19:13:40 +0000221 self.assertEqual(open(TESTFN, "rb").read(), 'c'*mapsize,
Tim Peters5ebfd362001-11-13 23:11:19 +0000222 "Copy-on-write test data file should not be modified.")
Georg Brandl31631792006-10-29 19:13:40 +0000223 # Ensuring copy-on-write maps cannot be resized
224 self.assertRaises(TypeError, m.resize, 2*mapsize)
Neal Norwitzd48a2f72008-04-01 05:40:43 +0000225 f.close()
Tim Peters5ebfd362001-11-13 23:11:19 +0000226 del m, f
Tim Petersabd8a332006-11-03 02:32:46 +0000227
Georg Brandl31631792006-10-29 19:13:40 +0000228 # Ensuring invalid access parameter raises exception
229 f = open(TESTFN, "r+b")
230 self.assertRaises(ValueError, mmap.mmap, f.fileno(), mapsize, access=4)
231 f.close()
Tim Peters5ebfd362001-11-13 23:11:19 +0000232
233 if os.name == "posix":
Tim Peters00cafa02001-11-13 23:39:47 +0000234 # Try incompatible flags, prot and access parameters.
235 f = open(TESTFN, "r+b")
Georg Brandl31631792006-10-29 19:13:40 +0000236 self.assertRaises(ValueError, mmap.mmap, f.fileno(), mapsize,
237 flags=mmap.MAP_PRIVATE,
Tim Peters5ebfd362001-11-13 23:11:19 +0000238 prot=mmap.PROT_READ, access=mmap.ACCESS_WRITE)
Tim Peters5379dea2002-04-18 04:30:18 +0000239 f.close()
Tim Peters5ebfd362001-11-13 23:11:19 +0000240
Georg Brandl31631792006-10-29 19:13:40 +0000241 def test_bad_file_desc(self):
242 # Try opening a bad file descriptor...
243 self.assertRaises(mmap.error, mmap.mmap, -2, 4096)
Neal Norwitz3b4fff82006-01-11 08:54:45 +0000244
Georg Brandl31631792006-10-29 19:13:40 +0000245 def test_tougher_find(self):
246 # Do a tougher .find() test. SF bug 515943 pointed out that, in 2.2,
247 # searching for data with embedded \0 bytes didn't work.
248 f = open(TESTFN, 'w+')
Tim Petersc9ffa062002-03-08 05:43:32 +0000249
Tim Petersc9ffa062002-03-08 05:43:32 +0000250 data = 'aabaac\x00deef\x00\x00aa\x00'
251 n = len(data)
252 f.write(data)
Tim Peters5379dea2002-04-18 04:30:18 +0000253 f.flush()
Tim Petersc9ffa062002-03-08 05:43:32 +0000254 m = mmap.mmap(f.fileno(), n)
255 f.close()
256
257 for start in range(n+1):
258 for finish in range(start, n+1):
259 slice = data[start : finish]
Georg Brandl31631792006-10-29 19:13:40 +0000260 self.assertEqual(m.find(slice), data.find(slice))
261 self.assertEqual(m.find(slice + 'x'), -1)
Tim Petersddc82ea2003-01-13 21:38:45 +0000262 m.close()
Tim Petersc9ffa062002-03-08 05:43:32 +0000263
Andrew M. Kuchling5c60bfc2008-01-19 18:18:41 +0000264 def test_find_end(self):
265 # test the new 'end' parameter works as expected
266 f = open(TESTFN, 'w+')
267 data = 'one two ones'
268 n = len(data)
269 f.write(data)
270 f.flush()
271 m = mmap.mmap(f.fileno(), n)
272 f.close()
273
274 self.assertEqual(m.find('one'), 0)
275 self.assertEqual(m.find('ones'), 8)
276 self.assertEqual(m.find('one', 0, -1), 0)
277 self.assertEqual(m.find('one', 1), 8)
278 self.assertEqual(m.find('one', 1, -1), 8)
279 self.assertEqual(m.find('one', 1, -2), -1)
280
281
282 def test_rfind(self):
283 # test the new 'end' parameter works as expected
284 f = open(TESTFN, 'w+')
285 data = 'one two ones'
286 n = len(data)
287 f.write(data)
288 f.flush()
289 m = mmap.mmap(f.fileno(), n)
290 f.close()
291
292 self.assertEqual(m.rfind('one'), 8)
293 self.assertEqual(m.rfind('one '), 0)
294 self.assertEqual(m.rfind('one', 0, -1), 8)
295 self.assertEqual(m.rfind('one', 0, -2), 0)
296 self.assertEqual(m.rfind('one', 1, -1), 8)
297 self.assertEqual(m.rfind('one', 1, -2), -1)
298
299
Georg Brandl31631792006-10-29 19:13:40 +0000300 def test_double_close(self):
301 # make sure a double close doesn't crash on Solaris (Bug# 665913)
302 f = open(TESTFN, 'w+')
Tim Petersc9ffa062002-03-08 05:43:32 +0000303
Tim Petersddc82ea2003-01-13 21:38:45 +0000304 f.write(2**16 * 'a') # Arbitrary character
Neal Norwitze604c022003-01-10 20:52:16 +0000305 f.close()
306
307 f = open(TESTFN)
Tim Petersddc82ea2003-01-13 21:38:45 +0000308 mf = mmap.mmap(f.fileno(), 2**16, access=mmap.ACCESS_READ)
Neal Norwitze604c022003-01-10 20:52:16 +0000309 mf.close()
310 mf.close()
311 f.close()
312
Georg Brandl31631792006-10-29 19:13:40 +0000313 def test_entire_file(self):
314 # test mapping of entire file by passing 0 for map length
315 if hasattr(os, "stat"):
316 f = open(TESTFN, "w+")
Tim Petersc9ffa062002-03-08 05:43:32 +0000317
Martin v. Löwis7fe60c02005-03-03 11:22:44 +0000318 f.write(2**16 * 'm') # Arbitrary character
319 f.close()
320
321 f = open(TESTFN, "rb+")
Tim Peterseba28be2005-03-28 01:08:02 +0000322 mf = mmap.mmap(f.fileno(), 0)
Georg Brandl31631792006-10-29 19:13:40 +0000323 self.assertEqual(len(mf), 2**16, "Map size should equal file size.")
324 self.assertEqual(mf.read(2**16), 2**16 * "m")
Martin v. Löwis7fe60c02005-03-03 11:22:44 +0000325 mf.close()
326 f.close()
327
Georg Brandl31631792006-10-29 19:13:40 +0000328 def test_move(self):
329 # make move works everywhere (64-bit format problem earlier)
330 f = open(TESTFN, 'w+')
Tim Peterseba28be2005-03-28 01:08:02 +0000331
Neal Norwitz8856fb72005-12-18 03:34:22 +0000332 f.write("ABCDEabcde") # Arbitrary character
333 f.flush()
334
335 mf = mmap.mmap(f.fileno(), 10)
336 mf.move(5, 0, 5)
Georg Brandl31631792006-10-29 19:13:40 +0000337 self.assertEqual(mf[:], "ABCDEABCDE", "Map move should have duplicated front 5")
Neal Norwitz8856fb72005-12-18 03:34:22 +0000338 mf.close()
339 f.close()
340
Hirokazu Yamamoto60098042009-03-31 13:22:01 +0000341 # more excessive test
342 data = "0123456789"
343 for dest in range(len(data)):
344 for src in range(len(data)):
345 for count in range(len(data) - max(dest, src)):
346 expected = data[:dest] + data[src:src+count] + data[dest+count:]
347 m = mmap.mmap(-1, len(data))
348 m[:] = data
349 m.move(dest, src, count)
350 self.assertEqual(m[:], expected)
351 m.close()
352
Hirokazu Yamamoto81d4b362009-03-31 20:22:13 +0000353 # segfault test (Issue 5387)
354 m = mmap.mmap(-1, 100)
355 offsets = [-100, -1, 0, 1, 100]
356 for source, dest, size in itertools.product(offsets, offsets, offsets):
357 try:
358 m.move(source, dest, size)
359 except ValueError:
360 pass
361 self.assertRaises(ValueError, m.move, -1, -1, -1)
362 self.assertRaises(ValueError, m.move, -1, -1, 0)
363 self.assertRaises(ValueError, m.move, -1, 0, -1)
364 self.assertRaises(ValueError, m.move, 0, -1, -1)
365 self.assertRaises(ValueError, m.move, -1, 0, 0)
366 self.assertRaises(ValueError, m.move, 0, -1, 0)
367 self.assertRaises(ValueError, m.move, 0, 0, -1)
Hirokazu Yamamoto60098042009-03-31 13:22:01 +0000368 m.close()
369
Georg Brandl31631792006-10-29 19:13:40 +0000370 def test_anonymous(self):
371 # anonymous mmap.mmap(-1, PAGE)
372 m = mmap.mmap(-1, PAGESIZE)
373 for x in xrange(PAGESIZE):
374 self.assertEqual(m[x], '\0', "anonymously mmap'ed contents should be zero")
Neal Norwitz8856fb72005-12-18 03:34:22 +0000375
Georg Brandl31631792006-10-29 19:13:40 +0000376 for x in xrange(PAGESIZE):
377 m[x] = ch = chr(x & 255)
378 self.assertEqual(m[x], ch)
Neal Norwitz0e6bc8c2006-02-05 05:45:43 +0000379
Thomas Wouters3ccec682007-08-28 15:28:19 +0000380 def test_extended_getslice(self):
381 # Test extended slicing by comparing with list slicing.
382 s = "".join(chr(c) for c in reversed(range(256)))
383 m = mmap.mmap(-1, len(s))
384 m[:] = s
385 self.assertEqual(m[:], s)
386 indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
387 for start in indices:
388 for stop in indices:
389 # Skip step 0 (invalid)
390 for step in indices[1:]:
391 self.assertEqual(m[start:stop:step],
392 s[start:stop:step])
393
394 def test_extended_set_del_slice(self):
395 # Test extended slicing by comparing with list slicing.
396 s = "".join(chr(c) for c in reversed(range(256)))
397 m = mmap.mmap(-1, len(s))
398 indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
399 for start in indices:
400 for stop in indices:
401 # Skip invalid step 0
402 for step in indices[1:]:
403 m[:] = s
404 self.assertEqual(m[:], s)
405 L = list(s)
406 # Make sure we have a slice of exactly the right length,
407 # but with different data.
408 data = L[start:stop:step]
409 data = "".join(reversed(data))
410 L[start:stop:step] = data
411 m[start:stop:step] = data
412 self.assertEquals(m[:], "".join(L))
413
Travis E. Oliphant8feafab2007-10-23 02:40:56 +0000414 def make_mmap_file (self, f, halfsize):
415 # Write 2 pages worth of data to the file
416 f.write ('\0' * halfsize)
417 f.write ('foo')
418 f.write ('\0' * (halfsize - 3))
419 f.flush ()
420 return mmap.mmap (f.fileno(), 0)
421
422 def test_offset (self):
423 f = open (TESTFN, 'w+b')
424
425 try: # unlink TESTFN no matter what
426 halfsize = mmap.ALLOCATIONGRANULARITY
427 m = self.make_mmap_file (f, halfsize)
428 m.close ()
429 f.close ()
430
431 mapsize = halfsize * 2
432 # Try invalid offset
433 f = open(TESTFN, "r+b")
434 for offset in [-2, -1, None]:
435 try:
436 m = mmap.mmap(f.fileno(), mapsize, offset=offset)
437 self.assertEqual(0, 1)
438 except (ValueError, TypeError, OverflowError):
439 pass
440 else:
441 self.assertEqual(0, 0)
442 f.close()
443
444 # Try valid offset, hopefully 8192 works on all OSes
445 f = open(TESTFN, "r+b")
446 m = mmap.mmap(f.fileno(), mapsize - halfsize, offset=halfsize)
447 self.assertEqual(m[0:3], 'foo')
448 f.close()
Hirokazu Yamamoto978bacd2009-02-18 15:11:55 +0000449
450 # Try resizing map
451 try:
452 m.resize(512)
453 except SystemError:
454 pass
455 else:
456 # resize() is supported
457 self.assertEqual(len(m), 512)
458 # Check that we can no longer seek beyond the new size.
459 self.assertRaises(ValueError, m.seek, 513, 0)
460 # Check that the content is not changed
461 self.assertEqual(m[0:3], 'foo')
462
463 # Check that the underlying file is truncated too
464 f = open(TESTFN)
465 f.seek(0, 2)
466 self.assertEqual(f.tell(), halfsize + 512)
467 f.close()
468 self.assertEqual(m.size(), halfsize + 512)
469
Travis E. Oliphant8feafab2007-10-23 02:40:56 +0000470 m.close()
471
472 finally:
473 f.close()
474 try:
475 os.unlink(TESTFN)
476 except OSError:
477 pass
478
Georg Brandld02fc482008-01-22 19:56:03 +0000479 def test_subclass(self):
480 class anon_mmap(mmap.mmap):
481 def __new__(klass, *args, **kwargs):
482 return mmap.mmap.__new__(klass, -1, *args, **kwargs)
483 anon_mmap(PAGESIZE)
484
Christian Heimes7adfad82008-02-15 08:20:11 +0000485 def test_prot_readonly(self):
Amaury Forgeot d'Arc64d68432008-02-16 00:16:50 +0000486 if not hasattr(mmap, 'PROT_READ'):
487 return
Christian Heimes7adfad82008-02-15 08:20:11 +0000488 mapsize = 10
489 open(TESTFN, "wb").write("a"*mapsize)
490 f = open(TESTFN, "rb")
491 m = mmap.mmap(f.fileno(), mapsize, prot=mmap.PROT_READ)
492 self.assertRaises(TypeError, m.write, "foo")
Neal Norwitzd48a2f72008-04-01 05:40:43 +0000493 f.close()
Georg Brandld02fc482008-01-22 19:56:03 +0000494
Facundo Batistae1396882008-02-17 18:59:29 +0000495 def test_error(self):
496 self.assert_(issubclass(mmap.error, EnvironmentError))
497 self.assert_("mmap.error" in str(mmap.error))
498
Hirokazu Yamamotoe363fa02009-02-28 11:39:45 +0000499 def test_io_methods(self):
500 data = "0123456789"
501 open(TESTFN, "wb").write("x"*len(data))
502 f = open(TESTFN, "r+b")
503 m = mmap.mmap(f.fileno(), len(data))
504 f.close()
505 # Test write_byte()
506 for i in xrange(len(data)):
507 self.assertEquals(m.tell(), i)
508 m.write_byte(data[i:i+1])
509 self.assertEquals(m.tell(), i+1)
510 self.assertRaises(ValueError, m.write_byte, "x")
511 self.assertEquals(m[:], data)
512 # Test read_byte()
513 m.seek(0)
514 for i in xrange(len(data)):
515 self.assertEquals(m.tell(), i)
516 self.assertEquals(m.read_byte(), data[i:i+1])
517 self.assertEquals(m.tell(), i+1)
518 self.assertRaises(ValueError, m.read_byte)
519 # Test read()
520 m.seek(3)
521 self.assertEquals(m.read(3), "345")
522 self.assertEquals(m.tell(), 6)
523 # Test write()
524 m.seek(3)
525 m.write("bar")
526 self.assertEquals(m.tell(), 6)
527 self.assertEquals(m[:], "012bar6789")
528 m.seek(8)
529 self.assertRaises(ValueError, m.write, "bar")
530
Hirokazu Yamamoto4b27e962009-02-28 12:42:16 +0000531 if os.name == 'nt':
532 def test_tagname(self):
533 data1 = "0123456789"
534 data2 = "abcdefghij"
535 assert len(data1) == len(data2)
Hirokazu Yamamotoec5294f2009-03-05 14:30:13 +0000536
Hirokazu Yamamoto4b27e962009-02-28 12:42:16 +0000537 # Test same tag
538 m1 = mmap.mmap(-1, len(data1), tagname="foo")
539 m1[:] = data1
540 m2 = mmap.mmap(-1, len(data2), tagname="foo")
541 m2[:] = data2
542 self.assertEquals(m1[:], data2)
543 self.assertEquals(m2[:], data2)
Hirokazu Yamamotoec5294f2009-03-05 14:30:13 +0000544 m2.close()
545 m1.close()
546
Hirokazu Yamamoto4b27e962009-02-28 12:42:16 +0000547 # Test differnt tag
548 m1 = mmap.mmap(-1, len(data1), tagname="foo")
549 m1[:] = data1
550 m2 = mmap.mmap(-1, len(data2), tagname="boo")
551 m2[:] = data2
552 self.assertEquals(m1[:], data1)
553 self.assertEquals(m2[:], data2)
Hirokazu Yamamotoec5294f2009-03-05 14:30:13 +0000554 m2.close()
555 m1.close()
Hirokazu Yamamoto4b27e962009-02-28 12:42:16 +0000556
Hirokazu Yamamotoec5294f2009-03-05 14:30:13 +0000557 def test_crasher_on_windows(self):
Hirokazu Yamamoto4b27e962009-02-28 12:42:16 +0000558 # Should not crash (Issue 1733986)
559 m = mmap.mmap(-1, 1000, tagname="foo")
560 try:
561 mmap.mmap(-1, 5000, tagname="foo")[:] # same tagname, but larger size
562 except:
563 pass
Hirokazu Yamamotoec5294f2009-03-05 14:30:13 +0000564 m.close()
565
566 # Should not crash (Issue 5385)
Hirokazu Yamamotoe2d36e42009-03-05 14:58:34 +0000567 open(TESTFN, "wb").write("x"*10)
568 f = open(TESTFN, "r+b")
569 m = mmap.mmap(f.fileno(), 0)
570 f.close()
Hirokazu Yamamotoec5294f2009-03-05 14:30:13 +0000571 try:
Hirokazu Yamamotoe2d36e42009-03-05 14:58:34 +0000572 m.resize(0) # will raise WindowsError
Hirokazu Yamamotoec5294f2009-03-05 14:30:13 +0000573 except:
574 pass
575 try:
576 m[:]
577 except:
578 pass
579 m.close()
580
Facundo Batistae1396882008-02-17 18:59:29 +0000581
Georg Brandl31631792006-10-29 19:13:40 +0000582def test_main():
583 run_unittest(MmapTests)
Andrew M. Kuchlinge81b9cf2000-03-30 21:15:29 +0000584
Georg Brandl31631792006-10-29 19:13:40 +0000585if __name__ == '__main__':
586 test_main()