annotate Lib/test/test_mmap.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents a3bbdd93b389
children 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
52456
f2c90fd05d66 Add import_function method to test.test_support, and modify a number of
R. David Murray <rdmurray@bitdance.com>
parents: 52030
diff changeset
1 from test.test_support import TESTFN, run_unittest, import_module
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
2 import unittest
52577
3aea75a34337 Issue #5387: Fixed mmap.move crash by integer overflow. (take2)
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52510
diff changeset
3 import os, re, itertools
12121
0727ff39e64f Added simple test case for mmap on Unix; someone needs to write a
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
4
52456
f2c90fd05d66 Add import_function method to test.test_support, and modify a number of
R. David Murray <rdmurray@bitdance.com>
parents: 52030
diff changeset
5 mmap = import_module('mmap')
f2c90fd05d66 Add import_function method to test.test_support, and modify a number of
R. David Murray <rdmurray@bitdance.com>
parents: 52030
diff changeset
6
12121
0727ff39e64f Added simple test case for mmap on Unix; someone needs to write a
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
7 PAGESIZE = mmap.PAGESIZE
0727ff39e64f Added simple test case for mmap on Unix; someone needs to write a
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
8
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
9 class MmapTests(unittest.TestCase):
12946
755c9ac9e6ae Additional tests for seek() method, written by Trent Mick
Andrew M. Kuchling <amk@amk.ca>
parents: 12242
diff changeset
10
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
11 def setUp(self):
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
12 if os.path.exists(TESTFN):
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
13 os.unlink(TESTFN)
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 14398
diff changeset
14
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
15 def tearDown(self):
17773
669604d9343f Change test_mmap.py to use test_support.TESTFN instead of hardcoded "foo",
Tim Peters <tim.peters@gmail.com>
parents: 16517
diff changeset
16 try:
17782
5387cb767059 unlink() would normally be found in the "os" module, so use it from there.
Fred Drake <fdrake@acm.org>
parents: 17773
diff changeset
17 os.unlink(TESTFN)
17773
669604d9343f Change test_mmap.py to use test_support.TESTFN instead of hardcoded "foo",
Tim Peters <tim.peters@gmail.com>
parents: 16517
diff changeset
18 except OSError:
669604d9343f Change test_mmap.py to use test_support.TESTFN instead of hardcoded "foo",
Tim Peters <tim.peters@gmail.com>
parents: 16517
diff changeset
19 pass
669604d9343f Change test_mmap.py to use test_support.TESTFN instead of hardcoded "foo",
Tim Peters <tim.peters@gmail.com>
parents: 16517
diff changeset
20
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
21 def test_basic(self):
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
22 # Test mmap module on Unix systems and Windows
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
23
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
24 # Create a file to be mmap'ed.
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
25 f = open(TESTFN, 'w+')
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
26 try:
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
27 # Write 2 pages worth of data to the file
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
28 f.write('\0'* PAGESIZE)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
29 f.write('foo')
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
30 f.write('\0'* (PAGESIZE-3) )
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
31 f.flush()
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
32 m = mmap.mmap(f.fileno(), 2 * PAGESIZE)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
33 f.close()
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
34
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
35 # Simple sanity checks
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
36
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
37 tp = str(type(m)) # SF bug 128713: segfaulted on Linux
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
38 self.assertEqual(m.find('foo'), PAGESIZE)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
39
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
40 self.assertEqual(len(m), 2*PAGESIZE)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
41
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
42 self.assertEqual(m[0], '\0')
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
43 self.assertEqual(m[0:3], '\0\0\0')
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
44
51688
0c6c0572c247 Issue #5292: Fixed mmap crash on its boundary access m[len(m)].
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 46236
diff changeset
45 # Shouldn't crash on boundary (Issue #5292)
0c6c0572c247 Issue #5292: Fixed mmap crash on its boundary access m[len(m)].
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 46236
diff changeset
46 self.assertRaises(IndexError, m.__getitem__, len(m))
0c6c0572c247 Issue #5292: Fixed mmap crash on its boundary access m[len(m)].
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 46236
diff changeset
47 self.assertRaises(IndexError, m.__setitem__, len(m), '\0')
0c6c0572c247 Issue #5292: Fixed mmap crash on its boundary access m[len(m)].
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 46236
diff changeset
48
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
49 # Modify the file's content
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
50 m[0] = '3'
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
51 m[PAGESIZE +3: PAGESIZE +3+3] = 'bar'
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
52
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
53 # Check that the modification worked
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
54 self.assertEqual(m[0], '3')
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
55 self.assertEqual(m[0:3], '3\0\0')
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
56 self.assertEqual(m[PAGESIZE-1 : PAGESIZE + 7], '\0foobar\0')
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
57
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
58 m.flush()
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
59
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
60 # Test doing a regular expression match in an mmap'ed file
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
61 match = re.search('[A-Za-z]+', m)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
62 if match is None:
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
63 self.fail('regex match on mmap failed!')
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
64 else:
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
65 start, end = match.span(0)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
66 length = end - start
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
67
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
68 self.assertEqual(start, PAGESIZE)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
69 self.assertEqual(end, PAGESIZE + 6)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
70
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
71 # test seeking around (try to overflow the seek implementation)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
72 m.seek(0,0)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
73 self.assertEqual(m.tell(), 0)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
74 m.seek(42,1)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
75 self.assertEqual(m.tell(), 42)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
76 m.seek(0,2)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
77 self.assertEqual(m.tell(), len(m))
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
78
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
79 # Try to seek to negative position...
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
80 self.assertRaises(ValueError, m.seek, -1)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
81
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
82 # Try to seek beyond end of mmap...
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
83 self.assertRaises(ValueError, m.seek, 1, 2)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
84
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
85 # Try to seek to negative position...
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
86 self.assertRaises(ValueError, m.seek, -len(m)-1, 2)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
87
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
88 # Try resizing map
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
89 try:
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
90 m.resize(512)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
91 except SystemError:
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
92 # resize() not supported
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
93 # No messages are printed, since the output of this test suite
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
94 # would then be different across platforms.
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
95 pass
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
96 else:
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
97 # resize() is supported
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
98 self.assertEqual(len(m), 512)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
99 # Check that we can no longer seek beyond the new size.
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
100 self.assertRaises(ValueError, m.seek, 513, 0)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
101
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
102 # Check that the underlying file is truncated too
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
103 # (bug #728515)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
104 f = open(TESTFN)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
105 f.seek(0, 2)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
106 self.assertEqual(f.tell(), 512)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
107 f.close()
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
108 self.assertEqual(m.size(), 512)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
109
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
110 m.close()
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
111
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
112 finally:
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
113 try:
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
114 f.close()
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
115 except OSError:
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
116 pass
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
117
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
118 def test_access_parameter(self):
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
119 # Test for "access" keyword parameter
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
120 mapsize = 10
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
121 open(TESTFN, "wb").write("a"*mapsize)
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
122 f = open(TESTFN, "rb")
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
123 m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_READ)
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
124 self.assertEqual(m[:], 'a'*mapsize, "Readonly memory map data incorrect.")
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
125
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
126 # Ensuring that readonly mmap can't be slice assigned
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
127 try:
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
128 m[:] = 'b'*mapsize
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
129 except TypeError:
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
130 pass
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
131 else:
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
132 self.fail("Able to write to readonly memory map")
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
133
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
134 # Ensuring that readonly mmap can't be item assigned
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
135 try:
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
136 m[0] = 'b'
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
137 except TypeError:
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
138 pass
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
139 else:
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
140 self.fail("Able to write to readonly memory map")
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
141
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
142 # Ensuring that readonly mmap can't be write() to
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
143 try:
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
144 m.seek(0,0)
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
145 m.write('abc')
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
146 except TypeError:
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
147 pass
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
148 else:
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
149 self.fail("Able to write to readonly memory map")
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
150
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
151 # Ensuring that readonly mmap can't be write_byte() to
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
152 try:
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
153 m.seek(0,0)
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
154 m.write_byte('d')
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
155 except TypeError:
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
156 pass
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
157 else:
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
158 self.fail("Able to write to readonly memory map")
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
159
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
160 # Ensuring that readonly mmap can't be resized
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
161 try:
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
162 m.resize(2*mapsize)
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
163 except SystemError: # resize is not universally supported
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
164 pass
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
165 except TypeError:
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
166 pass
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
167 else:
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
168 self.fail("Able to resize readonly memory map")
46236
17881f96ea01 Be sure to close the file. 2 places were deleting the file, so it was probably
Neal Norwitz <nnorwitz@gmail.com>
parents: 45380
diff changeset
169 f.close()
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
170 del m, f
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
171 self.assertEqual(open(TESTFN, "rb").read(), 'a'*mapsize,
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
172 "Readonly memory map data file was modified")
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
173
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
174 # Opening mmap with size too big
25130
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24364
diff changeset
175 import sys
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24364
diff changeset
176 f = open(TESTFN, "r+b")
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24364
diff changeset
177 try:
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24364
diff changeset
178 m = mmap.mmap(f.fileno(), mapsize+1)
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24364
diff changeset
179 except ValueError:
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24364
diff changeset
180 # we do not expect a ValueError on Windows
25192
5d207e57d040 A few days ago a test was added here to ensure that creating an mmap
Tim Peters <tim.peters@gmail.com>
parents: 25163
diff changeset
181 # CAUTION: This also changes the size of the file on disk, and
5d207e57d040 A few days ago a test was added here to ensure that creating an mmap
Tim Peters <tim.peters@gmail.com>
parents: 25163
diff changeset
182 # later tests assume that the length hasn't changed. We need to
5d207e57d040 A few days ago a test was added here to ensure that creating an mmap
Tim Peters <tim.peters@gmail.com>
parents: 25163
diff changeset
183 # repair that.
25130
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24364
diff changeset
184 if sys.platform.startswith('win'):
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
185 self.fail("Opening mmap with size+1 should work on Windows.")
25130
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24364
diff changeset
186 else:
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24364
diff changeset
187 # we expect a ValueError on Unix, but not on Windows
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24364
diff changeset
188 if not sys.platform.startswith('win'):
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
189 self.fail("Opening mmap with size+1 should raise ValueError.")
25203
abd625417a53 test_both(): I believe this was a typo: m is only defined if no
Barry Warsaw <barry@python.org>
parents: 25194
diff changeset
190 m.close()
25192
5d207e57d040 A few days ago a test was added here to ensure that creating an mmap
Tim Peters <tim.peters@gmail.com>
parents: 25163
diff changeset
191 f.close()
5d207e57d040 A few days ago a test was added here to ensure that creating an mmap
Tim Peters <tim.peters@gmail.com>
parents: 25163
diff changeset
192 if sys.platform.startswith('win'):
5d207e57d040 A few days ago a test was added here to ensure that creating an mmap
Tim Peters <tim.peters@gmail.com>
parents: 25163
diff changeset
193 # Repair damage from the resizing test.
5d207e57d040 A few days ago a test was added here to ensure that creating an mmap
Tim Peters <tim.peters@gmail.com>
parents: 25163
diff changeset
194 f = open(TESTFN, 'r+b')
5d207e57d040 A few days ago a test was added here to ensure that creating an mmap
Tim Peters <tim.peters@gmail.com>
parents: 25163
diff changeset
195 f.truncate(mapsize)
5d207e57d040 A few days ago a test was added here to ensure that creating an mmap
Tim Peters <tim.peters@gmail.com>
parents: 25163
diff changeset
196 f.close()
25130
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24364
diff changeset
197
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
198 # Opening mmap with access=ACCESS_WRITE
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
199 f = open(TESTFN, "r+b")
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
200 m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_WRITE)
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
201 # Modifying write-through memory map
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
202 m[:] = 'c'*mapsize
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
203 self.assertEqual(m[:], 'c'*mapsize,
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
204 "Write-through memory map memory not updated properly.")
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
205 m.flush()
25194
96dd9927e011 I left some debugging junk in here; removed it. Also replaced a few
Tim Peters <tim.peters@gmail.com>
parents: 25192
diff changeset
206 m.close()
96dd9927e011 I left some debugging junk in here; removed it. Also replaced a few
Tim Peters <tim.peters@gmail.com>
parents: 25192
diff changeset
207 f.close()
25192
5d207e57d040 A few days ago a test was added here to ensure that creating an mmap
Tim Peters <tim.peters@gmail.com>
parents: 25163
diff changeset
208 f = open(TESTFN, 'rb')
5d207e57d040 A few days ago a test was added here to ensure that creating an mmap
Tim Peters <tim.peters@gmail.com>
parents: 25163
diff changeset
209 stuff = f.read()
5d207e57d040 A few days ago a test was added here to ensure that creating an mmap
Tim Peters <tim.peters@gmail.com>
parents: 25163
diff changeset
210 f.close()
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
211 self.assertEqual(stuff, 'c'*mapsize,
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
212 "Write-through memory map data file not updated properly.")
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
213
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
214 # Opening mmap with access=ACCESS_COPY
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
215 f = open(TESTFN, "r+b")
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
216 m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_COPY)
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
217 # Modifying copy-on-write memory map
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
218 m[:] = 'd'*mapsize
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
219 self.assertEqual(m[:], 'd' * mapsize,
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
220 "Copy-on-write memory map data not written correctly.")
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
221 m.flush()
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
222 self.assertEqual(open(TESTFN, "rb").read(), 'c'*mapsize,
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
223 "Copy-on-write test data file should not be modified.")
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
224 # Ensuring copy-on-write maps cannot be resized
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
225 self.assertRaises(TypeError, m.resize, 2*mapsize)
46236
17881f96ea01 Be sure to close the file. 2 places were deleting the file, so it was probably
Neal Norwitz <nnorwitz@gmail.com>
parents: 45380
diff changeset
226 f.close()
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
227 del m, f
40284
ff678669aa6e Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 40259
diff changeset
228
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
229 # Ensuring invalid access parameter raises exception
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
230 f = open(TESTFN, "r+b")
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
231 self.assertRaises(ValueError, mmap.mmap, f.fileno(), mapsize, access=4)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
232 f.close()
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
233
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
234 if os.name == "posix":
20971
eba8c771db64 Removed print that executes only on Unix boxes; that made it impossible
Tim Peters <tim.peters@gmail.com>
parents: 20970
diff changeset
235 # Try incompatible flags, prot and access parameters.
eba8c771db64 Removed print that executes only on Unix boxes; that made it impossible
Tim Peters <tim.peters@gmail.com>
parents: 20970
diff changeset
236 f = open(TESTFN, "r+b")
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
237 self.assertRaises(ValueError, mmap.mmap, f.fileno(), mapsize,
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
238 flags=mmap.MAP_PRIVATE,
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
239 prot=mmap.PROT_READ, access=mmap.ACCESS_WRITE)
23128
5aaf3d7b8d8c SF bug 544733: Cygwin test_mmap fix for Python 2.2.1
Tim Peters <tim.peters@gmail.com>
parents: 22179
diff changeset
240 f.close()
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 17782
diff changeset
241
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
242 def test_bad_file_desc(self):
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
243 # Try opening a bad file descriptor...
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
244 self.assertRaises(mmap.error, mmap.mmap, -2, 4096)
36273
d0104364f6d1 Fix SF bug #1402308, segfault when using mmap(-1, ...)
Neal Norwitz <nnorwitz@gmail.com>
parents: 36073
diff changeset
245
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
246 def test_tougher_find(self):
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
247 # Do a tougher .find() test. SF bug 515943 pointed out that, in 2.2,
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
248 # searching for data with embedded \0 bytes didn't work.
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
249 f = open(TESTFN, 'w+')
22179
daf04db1a699 backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20971
diff changeset
250
daf04db1a699 backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20971
diff changeset
251 data = 'aabaac\x00deef\x00\x00aa\x00'
daf04db1a699 backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20971
diff changeset
252 n = len(data)
daf04db1a699 backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20971
diff changeset
253 f.write(data)
23128
5aaf3d7b8d8c SF bug 544733: Cygwin test_mmap fix for Python 2.2.1
Tim Peters <tim.peters@gmail.com>
parents: 22179
diff changeset
254 f.flush()
22179
daf04db1a699 backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20971
diff changeset
255 m = mmap.mmap(f.fileno(), n)
daf04db1a699 backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20971
diff changeset
256 f.close()
daf04db1a699 backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20971
diff changeset
257
daf04db1a699 backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20971
diff changeset
258 for start in range(n+1):
daf04db1a699 backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20971
diff changeset
259 for finish in range(start, n+1):
daf04db1a699 backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20971
diff changeset
260 slice = data[start : finish]
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
261 self.assertEqual(m.find(slice), data.find(slice))
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
262 self.assertEqual(m.find(slice + 'x'), -1)
27240
f59d80c7e1bb A new test here was failing on Windows, because the test before it never
Tim Peters <tim.peters@gmail.com>
parents: 27205
diff changeset
263 m.close()
22179
daf04db1a699 backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20971
diff changeset
264
44774
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
265 def test_find_end(self):
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
266 # test the new 'end' parameter works as expected
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
267 f = open(TESTFN, 'w+')
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
268 data = 'one two ones'
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
269 n = len(data)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
270 f.write(data)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
271 f.flush()
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
272 m = mmap.mmap(f.fileno(), n)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
273 f.close()
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
274
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
275 self.assertEqual(m.find('one'), 0)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
276 self.assertEqual(m.find('ones'), 8)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
277 self.assertEqual(m.find('one', 0, -1), 0)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
278 self.assertEqual(m.find('one', 1), 8)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
279 self.assertEqual(m.find('one', 1, -1), 8)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
280 self.assertEqual(m.find('one', 1, -2), -1)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
281
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
282
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
283 def test_rfind(self):
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
284 # test the new 'end' parameter works as expected
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
285 f = open(TESTFN, 'w+')
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
286 data = 'one two ones'
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
287 n = len(data)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
288 f.write(data)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
289 f.flush()
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
290 m = mmap.mmap(f.fileno(), n)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
291 f.close()
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
292
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
293 self.assertEqual(m.rfind('one'), 8)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
294 self.assertEqual(m.rfind('one '), 0)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
295 self.assertEqual(m.rfind('one', 0, -1), 8)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
296 self.assertEqual(m.rfind('one', 0, -2), 0)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
297 self.assertEqual(m.rfind('one', 1, -1), 8)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
298 self.assertEqual(m.rfind('one', 1, -2), -1)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
299
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 43640
diff changeset
300
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
301 def test_double_close(self):
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
302 # make sure a double close doesn't crash on Solaris (Bug# 665913)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
303 f = open(TESTFN, 'w+')
22179
daf04db1a699 backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20971
diff changeset
304
27240
f59d80c7e1bb A new test here was failing on Windows, because the test before it never
Tim Peters <tim.peters@gmail.com>
parents: 27205
diff changeset
305 f.write(2**16 * 'a') # Arbitrary character
27205
3c8d91f3f4c6 SF #665913, Fix mmap module core dump with unix
Neal Norwitz <nnorwitz@gmail.com>
parents: 25203
diff changeset
306 f.close()
3c8d91f3f4c6 SF #665913, Fix mmap module core dump with unix
Neal Norwitz <nnorwitz@gmail.com>
parents: 25203
diff changeset
307
3c8d91f3f4c6 SF #665913, Fix mmap module core dump with unix
Neal Norwitz <nnorwitz@gmail.com>
parents: 25203
diff changeset
308 f = open(TESTFN)
27240
f59d80c7e1bb A new test here was failing on Windows, because the test before it never
Tim Peters <tim.peters@gmail.com>
parents: 27205
diff changeset
309 mf = mmap.mmap(f.fileno(), 2**16, access=mmap.ACCESS_READ)
27205
3c8d91f3f4c6 SF #665913, Fix mmap module core dump with unix
Neal Norwitz <nnorwitz@gmail.com>
parents: 25203
diff changeset
310 mf.close()
3c8d91f3f4c6 SF #665913, Fix mmap module core dump with unix
Neal Norwitz <nnorwitz@gmail.com>
parents: 25203
diff changeset
311 mf.close()
3c8d91f3f4c6 SF #665913, Fix mmap module core dump with unix
Neal Norwitz <nnorwitz@gmail.com>
parents: 25203
diff changeset
312 f.close()
3c8d91f3f4c6 SF #665913, Fix mmap module core dump with unix
Neal Norwitz <nnorwitz@gmail.com>
parents: 25203
diff changeset
313
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
314 def test_entire_file(self):
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
315 # test mapping of entire file by passing 0 for map length
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
316 if hasattr(os, "stat"):
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
317 f = open(TESTFN, "w+")
34662
b40586129bb0 Patches #749830, #1144555: allow UNIX mmap size to default to current
Martin v. Löwis <martin@v.loewis.de>
parents: 27240
diff changeset
318
b40586129bb0 Patches #749830, #1144555: allow UNIX mmap size to default to current
Martin v. Löwis <martin@v.loewis.de>
parents: 27240
diff changeset
319 f.write(2**16 * 'm') # Arbitrary character
b40586129bb0 Patches #749830, #1144555: allow UNIX mmap size to default to current
Martin v. Löwis <martin@v.loewis.de>
parents: 27240
diff changeset
320 f.close()
34818
4eaab38566c9 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34662
diff changeset
321
34662
b40586129bb0 Patches #749830, #1144555: allow UNIX mmap size to default to current
Martin v. Löwis <martin@v.loewis.de>
parents: 27240
diff changeset
322 f = open(TESTFN, "rb+")
34818
4eaab38566c9 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34662
diff changeset
323 mf = mmap.mmap(f.fileno(), 0)
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
324 self.assertEqual(len(mf), 2**16, "Map size should equal file size.")
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
325 self.assertEqual(mf.read(2**16), 2**16 * "m")
34662
b40586129bb0 Patches #749830, #1144555: allow UNIX mmap size to default to current
Martin v. Löwis <martin@v.loewis.de>
parents: 27240
diff changeset
326 mf.close()
b40586129bb0 Patches #749830, #1144555: allow UNIX mmap size to default to current
Martin v. Löwis <martin@v.loewis.de>
parents: 27240
diff changeset
327 f.close()
34818
4eaab38566c9 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34662
diff changeset
328
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
329 def test_move(self):
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
330 # make move works everywhere (64-bit format problem earlier)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
331 f = open(TESTFN, 'w+')
34818
4eaab38566c9 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34662
diff changeset
332
36073
15b58e45f1a1 SF Patch #1365916, mmap fails on AMD64
Neal Norwitz <nnorwitz@gmail.com>
parents: 35465
diff changeset
333 f.write("ABCDEabcde") # Arbitrary character
15b58e45f1a1 SF Patch #1365916, mmap fails on AMD64
Neal Norwitz <nnorwitz@gmail.com>
parents: 35465
diff changeset
334 f.flush()
15b58e45f1a1 SF Patch #1365916, mmap fails on AMD64
Neal Norwitz <nnorwitz@gmail.com>
parents: 35465
diff changeset
335
15b58e45f1a1 SF Patch #1365916, mmap fails on AMD64
Neal Norwitz <nnorwitz@gmail.com>
parents: 35465
diff changeset
336 mf = mmap.mmap(f.fileno(), 10)
15b58e45f1a1 SF Patch #1365916, mmap fails on AMD64
Neal Norwitz <nnorwitz@gmail.com>
parents: 35465
diff changeset
337 mf.move(5, 0, 5)
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
338 self.assertEqual(mf[:], "ABCDEABCDE", "Map move should have duplicated front 5")
36073
15b58e45f1a1 SF Patch #1365916, mmap fails on AMD64
Neal Norwitz <nnorwitz@gmail.com>
parents: 35465
diff changeset
339 mf.close()
15b58e45f1a1 SF Patch #1365916, mmap fails on AMD64
Neal Norwitz <nnorwitz@gmail.com>
parents: 35465
diff changeset
340 f.close()
15b58e45f1a1 SF Patch #1365916, mmap fails on AMD64
Neal Norwitz <nnorwitz@gmail.com>
parents: 35465
diff changeset
341
52510
a91cf0defbd6 Issue #5387: Fixed mmap.move crash by integer overflow.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52456
diff changeset
342 # more excessive test
a91cf0defbd6 Issue #5387: Fixed mmap.move crash by integer overflow.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52456
diff changeset
343 data = "0123456789"
a91cf0defbd6 Issue #5387: Fixed mmap.move crash by integer overflow.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52456
diff changeset
344 for dest in range(len(data)):
a91cf0defbd6 Issue #5387: Fixed mmap.move crash by integer overflow.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52456
diff changeset
345 for src in range(len(data)):
a91cf0defbd6 Issue #5387: Fixed mmap.move crash by integer overflow.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52456
diff changeset
346 for count in range(len(data) - max(dest, src)):
a91cf0defbd6 Issue #5387: Fixed mmap.move crash by integer overflow.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52456
diff changeset
347 expected = data[:dest] + data[src:src+count] + data[dest+count:]
a91cf0defbd6 Issue #5387: Fixed mmap.move crash by integer overflow.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52456
diff changeset
348 m = mmap.mmap(-1, len(data))
a91cf0defbd6 Issue #5387: Fixed mmap.move crash by integer overflow.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52456
diff changeset
349 m[:] = data
a91cf0defbd6 Issue #5387: Fixed mmap.move crash by integer overflow.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52456
diff changeset
350 m.move(dest, src, count)
a91cf0defbd6 Issue #5387: Fixed mmap.move crash by integer overflow.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52456
diff changeset
351 self.assertEqual(m[:], expected)
a91cf0defbd6 Issue #5387: Fixed mmap.move crash by integer overflow.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52456
diff changeset
352 m.close()
a91cf0defbd6 Issue #5387: Fixed mmap.move crash by integer overflow.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52456
diff changeset
353
52577
3aea75a34337 Issue #5387: Fixed mmap.move crash by integer overflow. (take2)
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52510
diff changeset
354 # segfault test (Issue 5387)
3aea75a34337 Issue #5387: Fixed mmap.move crash by integer overflow. (take2)
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52510
diff changeset
355 m = mmap.mmap(-1, 100)
3aea75a34337 Issue #5387: Fixed mmap.move crash by integer overflow. (take2)
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52510
diff changeset
356 offsets = [-100, -1, 0, 1, 100]
3aea75a34337 Issue #5387: Fixed mmap.move crash by integer overflow. (take2)
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52510
diff changeset
357 for source, dest, size in itertools.product(offsets, offsets, offsets):
3aea75a34337 Issue #5387: Fixed mmap.move crash by integer overflow. (take2)
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52510
diff changeset
358 try:
3aea75a34337 Issue #5387: Fixed mmap.move crash by integer overflow. (take2)
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52510
diff changeset
359 m.move(source, dest, size)
3aea75a34337 Issue #5387: Fixed mmap.move crash by integer overflow. (take2)
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52510
diff changeset
360 except ValueError:
3aea75a34337 Issue #5387: Fixed mmap.move crash by integer overflow. (take2)
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52510
diff changeset
361 pass
52670
a73373ccccc2 bounds check arguments to mmap.move(). All of them. Really.
Jack Diederich <jackdied@gmail.com>
parents: 52577
diff changeset
362
a73373ccccc2 bounds check arguments to mmap.move(). All of them. Really.
Jack Diederich <jackdied@gmail.com>
parents: 52577
diff changeset
363 offsets = [(-1, -1, -1), (-1, -1, 0), (-1, 0, -1), (0, -1, -1),
a73373ccccc2 bounds check arguments to mmap.move(). All of them. Really.
Jack Diederich <jackdied@gmail.com>
parents: 52577
diff changeset
364 (-1, 0, 0), (0, -1, 0), (0, 0, -1)]
a73373ccccc2 bounds check arguments to mmap.move(). All of them. Really.
Jack Diederich <jackdied@gmail.com>
parents: 52577
diff changeset
365 for source, dest, size in offsets:
a73373ccccc2 bounds check arguments to mmap.move(). All of them. Really.
Jack Diederich <jackdied@gmail.com>
parents: 52577
diff changeset
366 self.assertRaises(ValueError, m.move, source, dest, size)
a73373ccccc2 bounds check arguments to mmap.move(). All of them. Really.
Jack Diederich <jackdied@gmail.com>
parents: 52577
diff changeset
367
52510
a91cf0defbd6 Issue #5387: Fixed mmap.move crash by integer overflow.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52456
diff changeset
368 m.close()
a91cf0defbd6 Issue #5387: Fixed mmap.move crash by integer overflow.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52456
diff changeset
369
52670
a73373ccccc2 bounds check arguments to mmap.move(). All of them. Really.
Jack Diederich <jackdied@gmail.com>
parents: 52577
diff changeset
370 m = mmap.mmap(-1, 1) # single byte
a73373ccccc2 bounds check arguments to mmap.move(). All of them. Really.
Jack Diederich <jackdied@gmail.com>
parents: 52577
diff changeset
371 self.assertRaises(ValueError, m.move, 0, 0, 2)
a73373ccccc2 bounds check arguments to mmap.move(). All of them. Really.
Jack Diederich <jackdied@gmail.com>
parents: 52577
diff changeset
372 self.assertRaises(ValueError, m.move, 1, 0, 1)
a73373ccccc2 bounds check arguments to mmap.move(). All of them. Really.
Jack Diederich <jackdied@gmail.com>
parents: 52577
diff changeset
373 self.assertRaises(ValueError, m.move, 0, 1, 1)
a73373ccccc2 bounds check arguments to mmap.move(). All of them. Really.
Jack Diederich <jackdied@gmail.com>
parents: 52577
diff changeset
374 m.move(0, 0, 1)
a73373ccccc2 bounds check arguments to mmap.move(). All of them. Really.
Jack Diederich <jackdied@gmail.com>
parents: 52577
diff changeset
375 m.move(0, 0, 0)
a73373ccccc2 bounds check arguments to mmap.move(). All of them. Really.
Jack Diederich <jackdied@gmail.com>
parents: 52577
diff changeset
376
a73373ccccc2 bounds check arguments to mmap.move(). All of them. Really.
Jack Diederich <jackdied@gmail.com>
parents: 52577
diff changeset
377
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
378 def test_anonymous(self):
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
379 # anonymous mmap.mmap(-1, PAGE)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
380 m = mmap.mmap(-1, PAGESIZE)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
381 for x in xrange(PAGESIZE):
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
382 self.assertEqual(m[x], '\0', "anonymously mmap'ed contents should be zero")
36073
15b58e45f1a1 SF Patch #1365916, mmap fails on AMD64
Neal Norwitz <nnorwitz@gmail.com>
parents: 35465
diff changeset
383
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
384 for x in xrange(PAGESIZE):
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
385 m[x] = ch = chr(x & 255)
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
386 self.assertEqual(m[x], ch)
36446
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36273
diff changeset
387
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
388 def test_extended_getslice(self):
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
389 # Test extended slicing by comparing with list slicing.
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
390 s = "".join(chr(c) for c in reversed(range(256)))
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
391 m = mmap.mmap(-1, len(s))
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
392 m[:] = s
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
393 self.assertEqual(m[:], s)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
394 indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
395 for start in indices:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
396 for stop in indices:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
397 # Skip step 0 (invalid)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
398 for step in indices[1:]:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
399 self.assertEqual(m[start:stop:step],
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
400 s[start:stop:step])
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
401
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
402 def test_extended_set_del_slice(self):
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
403 # Test extended slicing by comparing with list slicing.
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
404 s = "".join(chr(c) for c in reversed(range(256)))
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
405 m = mmap.mmap(-1, len(s))
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
406 indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
407 for start in indices:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
408 for stop in indices:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
409 # Skip invalid step 0
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
410 for step in indices[1:]:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
411 m[:] = s
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
412 self.assertEqual(m[:], s)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
413 L = list(s)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
414 # Make sure we have a slice of exactly the right length,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
415 # but with different data.
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
416 data = L[start:stop:step]
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
417 data = "".join(reversed(data))
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
418 L[start:stop:step] = data
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
419 m[start:stop:step] = data
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
420 self.assertEquals(m[:], "".join(L))
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 40284
diff changeset
421
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
422 def make_mmap_file (self, f, halfsize):
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
423 # Write 2 pages worth of data to the file
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
424 f.write ('\0' * halfsize)
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
425 f.write ('foo')
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
426 f.write ('\0' * (halfsize - 3))
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
427 f.flush ()
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
428 return mmap.mmap (f.fileno(), 0)
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
429
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
430 def test_offset (self):
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
431 f = open (TESTFN, 'w+b')
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
432
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
433 try: # unlink TESTFN no matter what
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
434 halfsize = mmap.ALLOCATIONGRANULARITY
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
435 m = self.make_mmap_file (f, halfsize)
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
436 m.close ()
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
437 f.close ()
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
438
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
439 mapsize = halfsize * 2
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
440 # Try invalid offset
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
441 f = open(TESTFN, "r+b")
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
442 for offset in [-2, -1, None]:
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
443 try:
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
444 m = mmap.mmap(f.fileno(), mapsize, offset=offset)
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
445 self.assertEqual(0, 1)
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
446 except (ValueError, TypeError, OverflowError):
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
447 pass
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
448 else:
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
449 self.assertEqual(0, 0)
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
450 f.close()
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
451
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
452 # Try valid offset, hopefully 8192 works on all OSes
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
453 f = open(TESTFN, "r+b")
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
454 m = mmap.mmap(f.fileno(), mapsize - halfsize, offset=halfsize)
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
455 self.assertEqual(m[0:3], 'foo')
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
456 f.close()
51692
9ad662a43eb3 Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51688
diff changeset
457
9ad662a43eb3 Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51688
diff changeset
458 # Try resizing map
9ad662a43eb3 Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51688
diff changeset
459 try:
9ad662a43eb3 Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51688
diff changeset
460 m.resize(512)
9ad662a43eb3 Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51688
diff changeset
461 except SystemError:
9ad662a43eb3 Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51688
diff changeset
462 pass
9ad662a43eb3 Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51688
diff changeset
463 else:
9ad662a43eb3 Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51688
diff changeset
464 # resize() is supported
9ad662a43eb3 Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51688
diff changeset
465 self.assertEqual(len(m), 512)
9ad662a43eb3 Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51688
diff changeset
466 # Check that we can no longer seek beyond the new size.
9ad662a43eb3 Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51688
diff changeset
467 self.assertRaises(ValueError, m.seek, 513, 0)
9ad662a43eb3 Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51688
diff changeset
468 # Check that the content is not changed
9ad662a43eb3 Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51688
diff changeset
469 self.assertEqual(m[0:3], 'foo')
9ad662a43eb3 Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51688
diff changeset
470
9ad662a43eb3 Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51688
diff changeset
471 # Check that the underlying file is truncated too
9ad662a43eb3 Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51688
diff changeset
472 f = open(TESTFN)
9ad662a43eb3 Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51688
diff changeset
473 f.seek(0, 2)
9ad662a43eb3 Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51688
diff changeset
474 self.assertEqual(f.tell(), halfsize + 512)
9ad662a43eb3 Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51688
diff changeset
475 f.close()
9ad662a43eb3 Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51688
diff changeset
476 self.assertEqual(m.size(), halfsize + 512)
9ad662a43eb3 Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51688
diff changeset
477
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
478 m.close()
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
479
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
480 finally:
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
481 f.close()
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
482 try:
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
483 os.unlink(TESTFN)
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
484 except OSError:
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
485 pass
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
486
44877
bb7ee3d89c2f Fix for #1087741 patch.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
487 def test_subclass(self):
bb7ee3d89c2f Fix for #1087741 patch.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
488 class anon_mmap(mmap.mmap):
bb7ee3d89c2f Fix for #1087741 patch.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
489 def __new__(klass, *args, **kwargs):
bb7ee3d89c2f Fix for #1087741 patch.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
490 return mmap.mmap.__new__(klass, -1, *args, **kwargs)
bb7ee3d89c2f Fix for #1087741 patch.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
491 anon_mmap(PAGESIZE)
bb7ee3d89c2f Fix for #1087741 patch.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
492
45358
a97689b3e742 Bug #2111: mmap segfaults when trying to write a block opened with PROT_READ
Christian Heimes <christian@cheimes.de>
parents: 44877
diff changeset
493 def test_prot_readonly(self):
45369
62df1e339832 mmap.PROT_READ does not exists on win32;
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45358
diff changeset
494 if not hasattr(mmap, 'PROT_READ'):
62df1e339832 mmap.PROT_READ does not exists on win32;
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45358
diff changeset
495 return
45358
a97689b3e742 Bug #2111: mmap segfaults when trying to write a block opened with PROT_READ
Christian Heimes <christian@cheimes.de>
parents: 44877
diff changeset
496 mapsize = 10
a97689b3e742 Bug #2111: mmap segfaults when trying to write a block opened with PROT_READ
Christian Heimes <christian@cheimes.de>
parents: 44877
diff changeset
497 open(TESTFN, "wb").write("a"*mapsize)
a97689b3e742 Bug #2111: mmap segfaults when trying to write a block opened with PROT_READ
Christian Heimes <christian@cheimes.de>
parents: 44877
diff changeset
498 f = open(TESTFN, "rb")
a97689b3e742 Bug #2111: mmap segfaults when trying to write a block opened with PROT_READ
Christian Heimes <christian@cheimes.de>
parents: 44877
diff changeset
499 m = mmap.mmap(f.fileno(), mapsize, prot=mmap.PROT_READ)
a97689b3e742 Bug #2111: mmap segfaults when trying to write a block opened with PROT_READ
Christian Heimes <christian@cheimes.de>
parents: 44877
diff changeset
500 self.assertRaises(TypeError, m.write, "foo")
46236
17881f96ea01 Be sure to close the file. 2 places were deleting the file, so it was probably
Neal Norwitz <nnorwitz@gmail.com>
parents: 45380
diff changeset
501 f.close()
44877
bb7ee3d89c2f Fix for #1087741 patch.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
502
45380
cc0232de066c Issue 2112. mmap does not raises EnvironmentError no more, but
Facundo Batista <facundobatista@gmail.com>
parents: 45369
diff changeset
503 def test_error(self):
cc0232de066c Issue 2112. mmap does not raises EnvironmentError no more, but
Facundo Batista <facundobatista@gmail.com>
parents: 45369
diff changeset
504 self.assert_(issubclass(mmap.error, EnvironmentError))
cc0232de066c Issue 2112. mmap does not raises EnvironmentError no more, but
Facundo Batista <facundobatista@gmail.com>
parents: 45369
diff changeset
505 self.assert_("mmap.error" in str(mmap.error))
cc0232de066c Issue 2112. mmap does not raises EnvironmentError no more, but
Facundo Batista <facundobatista@gmail.com>
parents: 45369
diff changeset
506
51907
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
507 def test_io_methods(self):
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
508 data = "0123456789"
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
509 open(TESTFN, "wb").write("x"*len(data))
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
510 f = open(TESTFN, "r+b")
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
511 m = mmap.mmap(f.fileno(), len(data))
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
512 f.close()
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
513 # Test write_byte()
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
514 for i in xrange(len(data)):
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
515 self.assertEquals(m.tell(), i)
52833
a3bbdd93b389 No behavior change.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52670
diff changeset
516 m.write_byte(data[i])
51907
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
517 self.assertEquals(m.tell(), i+1)
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
518 self.assertRaises(ValueError, m.write_byte, "x")
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
519 self.assertEquals(m[:], data)
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
520 # Test read_byte()
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
521 m.seek(0)
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
522 for i in xrange(len(data)):
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
523 self.assertEquals(m.tell(), i)
52833
a3bbdd93b389 No behavior change.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52670
diff changeset
524 self.assertEquals(m.read_byte(), data[i])
51907
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
525 self.assertEquals(m.tell(), i+1)
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
526 self.assertRaises(ValueError, m.read_byte)
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
527 # Test read()
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
528 m.seek(3)
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
529 self.assertEquals(m.read(3), "345")
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
530 self.assertEquals(m.tell(), 6)
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
531 # Test write()
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
532 m.seek(3)
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
533 m.write("bar")
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
534 self.assertEquals(m.tell(), 6)
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
535 self.assertEquals(m[:], "012bar6789")
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
536 m.seek(8)
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
537 self.assertRaises(ValueError, m.write, "bar")
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
538
51911
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
539 if os.name == 'nt':
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
540 def test_tagname(self):
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
541 data1 = "0123456789"
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
542 data2 = "abcdefghij"
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
543 assert len(data1) == len(data2)
52026
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
544
51911
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
545 # Test same tag
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
546 m1 = mmap.mmap(-1, len(data1), tagname="foo")
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
547 m1[:] = data1
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
548 m2 = mmap.mmap(-1, len(data2), tagname="foo")
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
549 m2[:] = data2
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
550 self.assertEquals(m1[:], data2)
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
551 self.assertEquals(m2[:], data2)
52026
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
552 m2.close()
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
553 m1.close()
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
554
51911
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
555 # Test differnt tag
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
556 m1 = mmap.mmap(-1, len(data1), tagname="foo")
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
557 m1[:] = data1
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
558 m2 = mmap.mmap(-1, len(data2), tagname="boo")
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
559 m2[:] = data2
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
560 self.assertEquals(m1[:], data1)
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
561 self.assertEquals(m2[:], data2)
52026
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
562 m2.close()
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
563 m1.close()
51911
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
564
52026
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
565 def test_crasher_on_windows(self):
51911
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
566 # Should not crash (Issue 1733986)
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
567 m = mmap.mmap(-1, 1000, tagname="foo")
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
568 try:
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
569 mmap.mmap(-1, 5000, tagname="foo")[:] # same tagname, but larger size
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
570 except:
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
571 pass
52026
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
572 m.close()
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
573
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
574 # Should not crash (Issue 5385)
52030
9cd355a79845 mmap.resize for anonymous map is not working yet, so changed to real file mapping...
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52026
diff changeset
575 open(TESTFN, "wb").write("x"*10)
9cd355a79845 mmap.resize for anonymous map is not working yet, so changed to real file mapping...
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52026
diff changeset
576 f = open(TESTFN, "r+b")
9cd355a79845 mmap.resize for anonymous map is not working yet, so changed to real file mapping...
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52026
diff changeset
577 m = mmap.mmap(f.fileno(), 0)
9cd355a79845 mmap.resize for anonymous map is not working yet, so changed to real file mapping...
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52026
diff changeset
578 f.close()
52026
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
579 try:
52030
9cd355a79845 mmap.resize for anonymous map is not working yet, so changed to real file mapping...
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52026
diff changeset
580 m.resize(0) # will raise WindowsError
52026
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
581 except:
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
582 pass
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
583 try:
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
584 m[:]
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
585 except:
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
586 pass
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
587 m.close()
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
588
45380
cc0232de066c Issue 2112. mmap does not raises EnvironmentError no more, but
Facundo Batista <facundobatista@gmail.com>
parents: 45369
diff changeset
589
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
590 def test_main():
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
591 run_unittest(MmapTests)
12121
0727ff39e64f Added simple test case for mmap on Unix; someone needs to write a
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
592
40259
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
593 if __name__ == '__main__':
d9a8d4f1d651 Convert test_mmap to unittest.
Georg Brandl <georg@python.org>
parents: 36446
diff changeset
594 test_main()