PageRenderTime 752ms CodeModel.GetById 161ms app.highlight 396ms RepoModel.GetById 188ms app.codeStats 0ms

/Lib/test/test_mmap.py

http://unladen-swallow.googlecode.com/
Python | 586 lines | 475 code | 59 blank | 52 comment | 24 complexity | 9256735477a1eddb682d89cb09c5c4c2 MD5 | raw file
  1from test.test_support import TESTFN, run_unittest
  2import mmap
  3import unittest
  4import os, re, itertools
  5
  6PAGESIZE = mmap.PAGESIZE
  7
  8class MmapTests(unittest.TestCase):
  9
 10    def setUp(self):
 11        if os.path.exists(TESTFN):
 12            os.unlink(TESTFN)
 13
 14    def tearDown(self):
 15        try:
 16            os.unlink(TESTFN)
 17        except OSError:
 18            pass
 19
 20    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
 44            # 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
 48            # 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
119        mapsize = 10
120        open(TESTFN, "wb").write("a"*mapsize)
121        f = open(TESTFN, "rb")
122        m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_READ)
123        self.assertEqual(m[:], 'a'*mapsize, "Readonly memory map data incorrect.")
124
125        # Ensuring that readonly mmap can't be slice assigned
126        try:
127            m[:] = 'b'*mapsize
128        except TypeError:
129            pass
130        else:
131            self.fail("Able to write to readonly memory map")
132
133        # Ensuring that readonly mmap can't be item assigned
134        try:
135            m[0] = 'b'
136        except TypeError:
137            pass
138        else:
139            self.fail("Able to write to readonly memory map")
140
141        # Ensuring that readonly mmap can't be write() to
142        try:
143            m.seek(0,0)
144            m.write('abc')
145        except TypeError:
146            pass
147        else:
148            self.fail("Able to write to readonly memory map")
149
150        # Ensuring that readonly mmap can't be write_byte() to
151        try:
152            m.seek(0,0)
153            m.write_byte('d')
154        except TypeError:
155            pass
156        else:
157            self.fail("Able to write to readonly memory map")
158
159        # Ensuring that readonly mmap can't be resized
160        try:
161            m.resize(2*mapsize)
162        except SystemError:   # resize is not universally supported
163            pass
164        except TypeError:
165            pass
166        else:
167            self.fail("Able to resize readonly memory map")
168        f.close()
169        del m, f
170        self.assertEqual(open(TESTFN, "rb").read(), 'a'*mapsize,
171               "Readonly memory map data file was modified")
172
173        # Opening mmap with size too big
174        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
180            # 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.
183            if sys.platform.startswith('win'):
184                self.fail("Opening mmap with size+1 should work on Windows.")
185        else:
186            # we expect a ValueError on Unix, but not on Windows
187            if not sys.platform.startswith('win'):
188                self.fail("Opening mmap with size+1 should raise ValueError.")
189            m.close()
190        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()
196
197        # Opening mmap with access=ACCESS_WRITE
198        f = open(TESTFN, "r+b")
199        m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_WRITE)
200        # Modifying write-through memory map
201        m[:] = 'c'*mapsize
202        self.assertEqual(m[:], 'c'*mapsize,
203               "Write-through memory map memory not updated properly.")
204        m.flush()
205        m.close()
206        f.close()
207        f = open(TESTFN, 'rb')
208        stuff = f.read()
209        f.close()
210        self.assertEqual(stuff, 'c'*mapsize,
211               "Write-through memory map data file not updated properly.")
212
213        # Opening mmap with access=ACCESS_COPY
214        f = open(TESTFN, "r+b")
215        m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_COPY)
216        # Modifying copy-on-write memory map
217        m[:] = 'd'*mapsize
218        self.assertEqual(m[:], 'd' * mapsize,
219               "Copy-on-write memory map data not written correctly.")
220        m.flush()
221        self.assertEqual(open(TESTFN, "rb").read(), 'c'*mapsize,
222               "Copy-on-write test data file should not be modified.")
223        # Ensuring copy-on-write maps cannot be resized
224        self.assertRaises(TypeError, m.resize, 2*mapsize)
225        f.close()
226        del m, f
227
228        # 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()
232
233        if os.name == "posix":
234            # Try incompatible flags, prot and access parameters.
235            f = open(TESTFN, "r+b")
236            self.assertRaises(ValueError, mmap.mmap, f.fileno(), mapsize,
237                              flags=mmap.MAP_PRIVATE,
238                              prot=mmap.PROT_READ, access=mmap.ACCESS_WRITE)
239            f.close()
240
241    def test_bad_file_desc(self):
242        # Try opening a bad file descriptor...
243        self.assertRaises(mmap.error, mmap.mmap, -2, 4096)
244
245    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+')
249
250        data = 'aabaac\x00deef\x00\x00aa\x00'
251        n = len(data)
252        f.write(data)
253        f.flush()
254        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]
260                self.assertEqual(m.find(slice), data.find(slice))
261                self.assertEqual(m.find(slice + 'x'), -1)
262        m.close()
263
264    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
300    def test_double_close(self):
301        # make sure a double close doesn't crash on Solaris (Bug# 665913)
302        f = open(TESTFN, 'w+')
303
304        f.write(2**16 * 'a') # Arbitrary character
305        f.close()
306
307        f = open(TESTFN)
308        mf = mmap.mmap(f.fileno(), 2**16, access=mmap.ACCESS_READ)
309        mf.close()
310        mf.close()
311        f.close()
312
313    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+")
317
318            f.write(2**16 * 'm') # Arbitrary character
319            f.close()
320
321            f = open(TESTFN, "rb+")
322            mf = mmap.mmap(f.fileno(), 0)
323            self.assertEqual(len(mf), 2**16, "Map size should equal file size.")
324            self.assertEqual(mf.read(2**16), 2**16 * "m")
325            mf.close()
326            f.close()
327
328    def test_move(self):
329        # make move works everywhere (64-bit format problem earlier)
330        f = open(TESTFN, 'w+')
331
332        f.write("ABCDEabcde") # Arbitrary character
333        f.flush()
334
335        mf = mmap.mmap(f.fileno(), 10)
336        mf.move(5, 0, 5)
337        self.assertEqual(mf[:], "ABCDEABCDE", "Map move should have duplicated front 5")
338        mf.close()
339        f.close()
340
341        # 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
353        # 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)
368        m.close()
369
370    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")
375
376        for x in xrange(PAGESIZE):
377            m[x] = ch = chr(x & 255)
378            self.assertEqual(m[x], ch)
379
380    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
414    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()
449
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
470            m.close()
471
472        finally:
473            f.close()
474            try:
475                os.unlink(TESTFN)
476            except OSError:
477                pass
478
479    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
485    def test_prot_readonly(self):
486        if not hasattr(mmap, 'PROT_READ'):
487            return
488        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")
493        f.close()
494
495    def test_error(self):
496        self.assert_(issubclass(mmap.error, EnvironmentError))
497        self.assert_("mmap.error" in str(mmap.error))
498
499    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
531    if os.name == 'nt':
532        def test_tagname(self):
533            data1 = "0123456789"
534            data2 = "abcdefghij"
535            assert len(data1) == len(data2)
536
537            # 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)
544            m2.close()
545            m1.close()
546
547            # 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)
554            m2.close()
555            m1.close()
556
557        def test_crasher_on_windows(self):
558            # 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
564            m.close()
565
566            # Should not crash (Issue 5385)
567            open(TESTFN, "wb").write("x"*10)
568            f = open(TESTFN, "r+b")
569            m = mmap.mmap(f.fileno(), 0)
570            f.close()
571            try:
572                m.resize(0) # will raise WindowsError
573            except:
574                pass
575            try:
576                m[:]
577            except:
578                pass
579            m.close()
580
581
582def test_main():
583    run_unittest(MmapTests)
584
585if __name__ == '__main__':
586    test_main()