3 import string
, os
, re
, sys
5 PAGESIZE
= mmap
.PAGESIZE
8 "Test mmap module on Unix systems and Windows"
10 # Create an mmap'ed file
13 # Write 2 pages worth of data to the file
14 f
.write('\0'* PAGESIZE
)
16 f
.write('\0'* (PAGESIZE
-3) )
18 m
= mmap
.mmap(f
.fileno(), 2 * PAGESIZE
)
21 # Simple sanity checks
22 print ' Position of foo:', string
.find(m
, 'foo') / float(PAGESIZE
), 'pages'
23 assert string
.find(m
, 'foo') == PAGESIZE
25 print ' Length of file:', len(m
) / float(PAGESIZE
), 'pages'
26 assert len(m
) == 2*PAGESIZE
28 print ' Contents of byte 0:', repr(m
[0])
30 print ' Contents of first 3 bytes:', repr(m
[0:3])
31 assert m
[0:3] == '\0\0\0'
33 # Modify the file's content
34 print "\n Modifying file's content..."
36 m
[PAGESIZE
+3: PAGESIZE
+3+3]='bar'
38 # Check that the modification worked
39 print ' Contents of byte 0:', repr(m
[0])
41 print ' Contents of first 3 bytes:', repr(m
[0:3])
42 assert m
[0:3] == '3\0\0'
43 print ' Contents of second page:', m
[PAGESIZE
-1 : PAGESIZE
+ 7]
44 assert m
[PAGESIZE
-1 : PAGESIZE
+ 7] == '\0foobar\0'
48 # Test doing a regular expression match in an mmap'ed file
49 match
=re
.search('[A-Za-z]+', m
)
51 print ' ERROR: regex match on mmap failed!'
53 start
, end
= match
.span(0)
56 print ' Regex match on mmap (page start, length of match):',
57 print start
/ float(PAGESIZE
), length
59 assert start
== PAGESIZE
60 assert end
== PAGESIZE
+ 6