1 from test_support
import verify
, TESTFN
5 PAGESIZE
= mmap
.PAGESIZE
8 "Test mmap module on Unix systems and Windows"
10 # Create a file to be mmap'ed.
11 if os
.path
.exists(TESTFN
):
13 f
= open(TESTFN
, 'w+')
15 try: # unlink TESTFN no matter what
16 # Write 2 pages worth of data to the file
17 f
.write('\0'* PAGESIZE
)
19 f
.write('\0'* (PAGESIZE
-3) )
21 m
= mmap
.mmap(f
.fileno(), 2 * PAGESIZE
)
24 # Simple sanity checks
26 print type(m
) # SF bug 128713: segfaulted on Linux
27 print ' Position of foo:', m
.find('foo') / float(PAGESIZE
), 'pages'
28 verify(m
.find('foo') == PAGESIZE
)
30 print ' Length of file:', len(m
) / float(PAGESIZE
), 'pages'
31 verify(len(m
) == 2*PAGESIZE
)
33 print ' Contents of byte 0:', repr(m
[0])
35 print ' Contents of first 3 bytes:', repr(m
[0:3])
36 verify(m
[0:3] == '\0\0\0')
38 # Modify the file's content
39 print "\n Modifying file's content..."
41 m
[PAGESIZE
+3: PAGESIZE
+3+3] = 'bar'
43 # Check that the modification worked
44 print ' Contents of byte 0:', repr(m
[0])
46 print ' Contents of first 3 bytes:', repr(m
[0:3])
47 verify(m
[0:3] == '3\0\0')
48 print ' Contents of second page:', repr(m
[PAGESIZE
-1 : PAGESIZE
+ 7])
49 verify(m
[PAGESIZE
-1 : PAGESIZE
+ 7] == '\0foobar\0')
53 # Test doing a regular expression match in an mmap'ed file
54 match
= re
.search('[A-Za-z]+', m
)
56 print ' ERROR: regex match on mmap failed!'
58 start
, end
= match
.span(0)
61 print ' Regex match on mmap (page start, length of match):',
62 print start
/ float(PAGESIZE
), length
64 verify(start
== PAGESIZE
)
65 verify(end
== PAGESIZE
+ 6)
67 # test seeking around (try to overflow the seek implementation)
69 print ' Seek to zeroth byte'
72 print ' Seek to 42nd byte'
73 verify(m
.tell() == 42)
75 print ' Seek to last byte'
76 verify(m
.tell() == len(m
))
78 print ' Try to seek to negative position...'
84 verify(0, 'expected a ValueError but did not get it')
86 print ' Try to seek beyond end of mmap...'
92 verify(0, 'expected a ValueError but did not get it')
94 print ' Try to seek to negative position...'
100 verify(0, 'expected a ValueError but did not get it')
103 print ' Attempting resize()'
107 # resize() not supported
108 # No messages are printed, since the output of this test suite
109 # would then be different across platforms.
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.
121 verify(0, 'Could seek beyond the new size')