|
1 | | -from test_support import verify |
| 1 | +from test_support import verify, TESTFN, unlink |
2 | 2 | import mmap |
3 | 3 | import os, re, sys |
4 | 4 |
|
|
7 | 7 | def test_both(): |
8 | 8 | "Test mmap module on Unix systems and Windows" |
9 | 9 |
|
10 | | - # Create an mmap'ed file |
11 | | - f = open('foo', 'w+') |
12 | | - |
13 | | - # Write 2 pages worth of data to the file |
14 | | - f.write('\0'* PAGESIZE) |
15 | | - f.write('foo') |
16 | | - f.write('\0'* (PAGESIZE-3) ) |
17 | | - |
18 | | - m = mmap.mmap(f.fileno(), 2 * PAGESIZE) |
19 | | - f.close() |
20 | | - |
21 | | - # Simple sanity checks |
22 | | - |
23 | | - print type(m) # SF bug 128713: segfaulted on Linux |
24 | | - print ' Position of foo:', m.find('foo') / float(PAGESIZE), 'pages' |
25 | | - verify(m.find('foo') == PAGESIZE) |
26 | | - |
27 | | - print ' Length of file:', len(m) / float(PAGESIZE), 'pages' |
28 | | - verify(len(m) == 2*PAGESIZE) |
29 | | - |
30 | | - print ' Contents of byte 0:', repr(m[0]) |
31 | | - verify(m[0] == '\0') |
32 | | - print ' Contents of first 3 bytes:', repr(m[0:3]) |
33 | | - verify(m[0:3] == '\0\0\0') |
34 | | - |
35 | | - # Modify the file's content |
36 | | - print "\n Modifying file's content..." |
37 | | - m[0] = '3' |
38 | | - m[PAGESIZE +3: PAGESIZE +3+3]='bar' |
39 | | - |
40 | | - # Check that the modification worked |
41 | | - print ' Contents of byte 0:', repr(m[0]) |
42 | | - verify(m[0] == '3') |
43 | | - print ' Contents of first 3 bytes:', repr(m[0:3]) |
44 | | - verify(m[0:3] == '3\0\0') |
45 | | - print ' Contents of second page:', repr(m[PAGESIZE-1 : PAGESIZE + 7]) |
46 | | - verify(m[PAGESIZE-1 : PAGESIZE + 7] == '\0foobar\0') |
47 | | - |
48 | | - m.flush() |
49 | | - |
50 | | - # Test doing a regular expression match in an mmap'ed file |
51 | | - match=re.search('[A-Za-z]+', m) |
52 | | - if match is None: |
53 | | - print ' ERROR: regex match on mmap failed!' |
54 | | - else: |
55 | | - start, end = match.span(0) |
56 | | - length = end - start |
57 | | - |
58 | | - print ' Regex match on mmap (page start, length of match):', |
59 | | - print start / float(PAGESIZE), length |
60 | | - |
61 | | - verify(start == PAGESIZE) |
62 | | - verify(end == PAGESIZE + 6) |
63 | | - |
64 | | - # test seeking around (try to overflow the seek implementation) |
65 | | - m.seek(0,0) |
66 | | - print ' Seek to zeroth byte' |
67 | | - verify(m.tell() == 0) |
68 | | - m.seek(42,1) |
69 | | - print ' Seek to 42nd byte' |
70 | | - verify(m.tell() == 42) |
71 | | - m.seek(0,2) |
72 | | - print ' Seek to last byte' |
73 | | - verify(m.tell() == len(m)) |
74 | | - |
75 | | - print ' Try to seek to negative position...' |
76 | | - try: |
77 | | - m.seek(-1) |
78 | | - except ValueError: |
79 | | - pass |
80 | | - else: |
81 | | - verify(0, 'expected a ValueError but did not get it') |
82 | | - |
83 | | - print ' Try to seek beyond end of mmap...' |
84 | | - try: |
85 | | - m.seek(1,2) |
86 | | - except ValueError: |
87 | | - pass |
88 | | - else: |
89 | | - verify(0, 'expected a ValueError but did not get it') |
90 | | - |
91 | | - print ' Try to seek to negative position...' |
92 | | - try: |
93 | | - m.seek(-len(m)-1,2) |
94 | | - except ValueError: |
95 | | - pass |
96 | | - else: |
97 | | - verify(0, 'expected a ValueError but did not get it') |
98 | | - |
99 | | - # Try resizing map |
100 | | - print ' Attempting resize()' |
101 | | - try: |
102 | | - m.resize( 512 ) |
103 | | - except SystemError: |
104 | | - # resize() not supported |
105 | | - # No messages are printed, since the output of this test suite |
106 | | - # would then be different across platforms. |
107 | | - pass |
108 | | - else: |
109 | | - # resize() is supported |
110 | | - verify(len(m) == 512, |
111 | | - "len(m) is %d, but expecting 512" % (len(m),) ) |
112 | | - # Check that we can no longer seek beyond the new size. |
| 10 | + # Create a file to be mmap'ed. |
| 11 | + f = open(TESTFN, 'w+') |
| 12 | + |
| 13 | + try: # unlink TESTFN no matter what |
| 14 | + # Write 2 pages worth of data to the file |
| 15 | + f.write('\0'* PAGESIZE) |
| 16 | + f.write('foo') |
| 17 | + f.write('\0'* (PAGESIZE-3) ) |
| 18 | + |
| 19 | + m = mmap.mmap(f.fileno(), 2 * PAGESIZE) |
| 20 | + f.close() |
| 21 | + |
| 22 | + # Simple sanity checks |
| 23 | + |
| 24 | + print type(m) # SF bug 128713: segfaulted on Linux |
| 25 | + print ' Position of foo:', m.find('foo') / float(PAGESIZE), 'pages' |
| 26 | + verify(m.find('foo') == PAGESIZE) |
| 27 | + |
| 28 | + print ' Length of file:', len(m) / float(PAGESIZE), 'pages' |
| 29 | + verify(len(m) == 2*PAGESIZE) |
| 30 | + |
| 31 | + print ' Contents of byte 0:', repr(m[0]) |
| 32 | + verify(m[0] == '\0') |
| 33 | + print ' Contents of first 3 bytes:', repr(m[0:3]) |
| 34 | + verify(m[0:3] == '\0\0\0') |
| 35 | + |
| 36 | + # Modify the file's content |
| 37 | + print "\n Modifying file's content..." |
| 38 | + m[0] = '3' |
| 39 | + m[PAGESIZE +3: PAGESIZE +3+3]='bar' |
| 40 | + |
| 41 | + # Check that the modification worked |
| 42 | + print ' Contents of byte 0:', repr(m[0]) |
| 43 | + verify(m[0] == '3') |
| 44 | + print ' Contents of first 3 bytes:', repr(m[0:3]) |
| 45 | + verify(m[0:3] == '3\0\0') |
| 46 | + print ' Contents of second page:', repr(m[PAGESIZE-1 : PAGESIZE + 7]) |
| 47 | + verify(m[PAGESIZE-1 : PAGESIZE + 7] == '\0foobar\0') |
| 48 | + |
| 49 | + m.flush() |
| 50 | + |
| 51 | + # Test doing a regular expression match in an mmap'ed file |
| 52 | + match=re.search('[A-Za-z]+', m) |
| 53 | + if match is None: |
| 54 | + print ' ERROR: regex match on mmap failed!' |
| 55 | + else: |
| 56 | + start, end = match.span(0) |
| 57 | + length = end - start |
| 58 | + |
| 59 | + print ' Regex match on mmap (page start, length of match):', |
| 60 | + print start / float(PAGESIZE), length |
| 61 | + |
| 62 | + verify(start == PAGESIZE) |
| 63 | + verify(end == PAGESIZE + 6) |
| 64 | + |
| 65 | + # test seeking around (try to overflow the seek implementation) |
| 66 | + m.seek(0,0) |
| 67 | + print ' Seek to zeroth byte' |
| 68 | + verify(m.tell() == 0) |
| 69 | + m.seek(42,1) |
| 70 | + print ' Seek to 42nd byte' |
| 71 | + verify(m.tell() == 42) |
| 72 | + m.seek(0,2) |
| 73 | + print ' Seek to last byte' |
| 74 | + verify(m.tell() == len(m)) |
| 75 | + |
| 76 | + print ' Try to seek to negative position...' |
113 | 77 | try: |
114 | | - m.seek(513,0) |
| 78 | + m.seek(-1) |
115 | 79 | except ValueError: |
116 | 80 | pass |
117 | 81 | else: |
118 | | - verify(0, 'Could seek beyond the new size') |
| 82 | + verify(0, 'expected a ValueError but did not get it') |
| 83 | + |
| 84 | + print ' Try to seek beyond end of mmap...' |
| 85 | + try: |
| 86 | + m.seek(1,2) |
| 87 | + except ValueError: |
| 88 | + pass |
| 89 | + else: |
| 90 | + verify(0, 'expected a ValueError but did not get it') |
| 91 | + |
| 92 | + print ' Try to seek to negative position...' |
| 93 | + try: |
| 94 | + m.seek(-len(m)-1,2) |
| 95 | + except ValueError: |
| 96 | + pass |
| 97 | + else: |
| 98 | + verify(0, 'expected a ValueError but did not get it') |
| 99 | + |
| 100 | + # Try resizing map |
| 101 | + print ' Attempting resize()' |
| 102 | + try: |
| 103 | + m.resize( 512 ) |
| 104 | + except SystemError: |
| 105 | + # resize() not supported |
| 106 | + # No messages are printed, since the output of this test suite |
| 107 | + # would then be different across platforms. |
| 108 | + pass |
| 109 | + else: |
| 110 | + # resize() is supported |
| 111 | + verify(len(m) == 512, |
| 112 | + "len(m) is %d, but expecting 512" % (len(m),) ) |
| 113 | + # Check that we can no longer seek beyond the new size. |
| 114 | + try: |
| 115 | + m.seek(513,0) |
| 116 | + except ValueError: |
| 117 | + pass |
| 118 | + else: |
| 119 | + verify(0, 'Could seek beyond the new size') |
| 120 | + |
| 121 | + m.close() |
| 122 | + |
| 123 | + finally: |
| 124 | + try: |
| 125 | + f.close() |
| 126 | + except OSError: |
| 127 | + pass |
| 128 | + try: |
| 129 | + unlink(TESTFN) |
| 130 | + except OSError: |
| 131 | + pass |
119 | 132 |
|
120 | | - m.close() |
121 | | - os.unlink("foo") |
122 | 133 | print ' Test passed' |
123 | 134 |
|
124 | 135 | test_both() |
0 commit comments