ยปCore Development>Code coverage>Lib/test/test_mmap.py

Python code coverage for Lib/test/test_mmap.py

#countcontent
1n/afrom test.support import (TESTFN, run_unittest, import_module, unlink,
2n/a requires, _2G, _4G, gc_collect, cpython_only)
3n/aimport unittest
4n/aimport os
5n/aimport re
6n/aimport itertools
7n/aimport socket
8n/aimport sys
9n/aimport weakref
10n/a
11n/a# Skip test if we can't import mmap.
12n/ammap = import_module('mmap')
13n/a
14n/aPAGESIZE = mmap.PAGESIZE
15n/a
16n/aclass MmapTests(unittest.TestCase):
17n/a
18n/a def setUp(self):
19n/a if os.path.exists(TESTFN):
20n/a os.unlink(TESTFN)
21n/a
22n/a def tearDown(self):
23n/a try:
24n/a os.unlink(TESTFN)
25n/a except OSError:
26n/a pass
27n/a
28n/a def test_basic(self):
29n/a # Test mmap module on Unix systems and Windows
30n/a
31n/a # Create a file to be mmap'ed.
32n/a f = open(TESTFN, 'bw+')
33n/a try:
34n/a # Write 2 pages worth of data to the file
35n/a f.write(b'\0'* PAGESIZE)
36n/a f.write(b'foo')
37n/a f.write(b'\0'* (PAGESIZE-3) )
38n/a f.flush()
39n/a m = mmap.mmap(f.fileno(), 2 * PAGESIZE)
40n/a finally:
41n/a f.close()
42n/a
43n/a # Simple sanity checks
44n/a
45n/a tp = str(type(m)) # SF bug 128713: segfaulted on Linux
46n/a self.assertEqual(m.find(b'foo'), PAGESIZE)
47n/a
48n/a self.assertEqual(len(m), 2*PAGESIZE)
49n/a
50n/a self.assertEqual(m[0], 0)
51n/a self.assertEqual(m[0:3], b'\0\0\0')
52n/a
53n/a # Shouldn't crash on boundary (Issue #5292)
54n/a self.assertRaises(IndexError, m.__getitem__, len(m))
55n/a self.assertRaises(IndexError, m.__setitem__, len(m), b'\0')
56n/a
57n/a # Modify the file's content
58n/a m[0] = b'3'[0]
59n/a m[PAGESIZE +3: PAGESIZE +3+3] = b'bar'
60n/a
61n/a # Check that the modification worked
62n/a self.assertEqual(m[0], b'3'[0])
63n/a self.assertEqual(m[0:3], b'3\0\0')
64n/a self.assertEqual(m[PAGESIZE-1 : PAGESIZE + 7], b'\0foobar\0')
65n/a
66n/a m.flush()
67n/a
68n/a # Test doing a regular expression match in an mmap'ed file
69n/a match = re.search(b'[A-Za-z]+', m)
70n/a if match is None:
71n/a self.fail('regex match on mmap failed!')
72n/a else:
73n/a start, end = match.span(0)
74n/a length = end - start
75n/a
76n/a self.assertEqual(start, PAGESIZE)
77n/a self.assertEqual(end, PAGESIZE + 6)
78n/a
79n/a # test seeking around (try to overflow the seek implementation)
80n/a m.seek(0,0)
81n/a self.assertEqual(m.tell(), 0)
82n/a m.seek(42,1)
83n/a self.assertEqual(m.tell(), 42)
84n/a m.seek(0,2)
85n/a self.assertEqual(m.tell(), len(m))
86n/a
87n/a # Try to seek to negative position...
88n/a self.assertRaises(ValueError, m.seek, -1)
89n/a
90n/a # Try to seek beyond end of mmap...
91n/a self.assertRaises(ValueError, m.seek, 1, 2)
92n/a
93n/a # Try to seek to negative position...
94n/a self.assertRaises(ValueError, m.seek, -len(m)-1, 2)
95n/a
96n/a # Try resizing map
97n/a try:
98n/a m.resize(512)
99n/a except SystemError:
100n/a # resize() not supported
101n/a # No messages are printed, since the output of this test suite
102n/a # would then be different across platforms.
103n/a pass
104n/a else:
105n/a # resize() is supported
106n/a self.assertEqual(len(m), 512)
107n/a # Check that we can no longer seek beyond the new size.
108n/a self.assertRaises(ValueError, m.seek, 513, 0)
109n/a
110n/a # Check that the underlying file is truncated too
111n/a # (bug #728515)
112n/a f = open(TESTFN, 'rb')
113n/a try:
114n/a f.seek(0, 2)
115n/a self.assertEqual(f.tell(), 512)
116n/a finally:
117n/a f.close()
118n/a self.assertEqual(m.size(), 512)
119n/a
120n/a m.close()
121n/a
122n/a def test_access_parameter(self):
123n/a # Test for "access" keyword parameter
124n/a mapsize = 10
125n/a with open(TESTFN, "wb") as fp:
126n/a fp.write(b"a"*mapsize)
127n/a with open(TESTFN, "rb") as f:
128n/a m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_READ)
129n/a self.assertEqual(m[:], b'a'*mapsize, "Readonly memory map data incorrect.")
130n/a
131n/a # Ensuring that readonly mmap can't be slice assigned
132n/a try:
133n/a m[:] = b'b'*mapsize
134n/a except TypeError:
135n/a pass
136n/a else:
137n/a self.fail("Able to write to readonly memory map")
138n/a
139n/a # Ensuring that readonly mmap can't be item assigned
140n/a try:
141n/a m[0] = b'b'
142n/a except TypeError:
143n/a pass
144n/a else:
145n/a self.fail("Able to write to readonly memory map")
146n/a
147n/a # Ensuring that readonly mmap can't be write() to
148n/a try:
149n/a m.seek(0,0)
150n/a m.write(b'abc')
151n/a except TypeError:
152n/a pass
153n/a else:
154n/a self.fail("Able to write to readonly memory map")
155n/a
156n/a # Ensuring that readonly mmap can't be write_byte() to
157n/a try:
158n/a m.seek(0,0)
159n/a m.write_byte(b'd')
160n/a except TypeError:
161n/a pass
162n/a else:
163n/a self.fail("Able to write to readonly memory map")
164n/a
165n/a # Ensuring that readonly mmap can't be resized
166n/a try:
167n/a m.resize(2*mapsize)
168n/a except SystemError: # resize is not universally supported
169n/a pass
170n/a except TypeError:
171n/a pass
172n/a else:
173n/a self.fail("Able to resize readonly memory map")
174n/a with open(TESTFN, "rb") as fp:
175n/a self.assertEqual(fp.read(), b'a'*mapsize,
176n/a "Readonly memory map data file was modified")
177n/a
178n/a # Opening mmap with size too big
179n/a with open(TESTFN, "r+b") as f:
180n/a try:
181n/a m = mmap.mmap(f.fileno(), mapsize+1)
182n/a except ValueError:
183n/a # we do not expect a ValueError on Windows
184n/a # CAUTION: This also changes the size of the file on disk, and
185n/a # later tests assume that the length hasn't changed. We need to
186n/a # repair that.
187n/a if sys.platform.startswith('win'):
188n/a self.fail("Opening mmap with size+1 should work on Windows.")
189n/a else:
190n/a # we expect a ValueError on Unix, but not on Windows
191n/a if not sys.platform.startswith('win'):
192n/a self.fail("Opening mmap with size+1 should raise ValueError.")
193n/a m.close()
194n/a if sys.platform.startswith('win'):
195n/a # Repair damage from the resizing test.
196n/a with open(TESTFN, 'r+b') as f:
197n/a f.truncate(mapsize)
198n/a
199n/a # Opening mmap with access=ACCESS_WRITE
200n/a with open(TESTFN, "r+b") as f:
201n/a m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_WRITE)
202n/a # Modifying write-through memory map
203n/a m[:] = b'c'*mapsize
204n/a self.assertEqual(m[:], b'c'*mapsize,
205n/a "Write-through memory map memory not updated properly.")
206n/a m.flush()
207n/a m.close()
208n/a with open(TESTFN, 'rb') as f:
209n/a stuff = f.read()
210n/a self.assertEqual(stuff, b'c'*mapsize,
211n/a "Write-through memory map data file not updated properly.")
212n/a
213n/a # Opening mmap with access=ACCESS_COPY
214n/a with open(TESTFN, "r+b") as f:
215n/a m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_COPY)
216n/a # Modifying copy-on-write memory map
217n/a m[:] = b'd'*mapsize
218n/a self.assertEqual(m[:], b'd' * mapsize,
219n/a "Copy-on-write memory map data not written correctly.")
220n/a m.flush()
221n/a with open(TESTFN, "rb") as fp:
222n/a self.assertEqual(fp.read(), b'c'*mapsize,
223n/a "Copy-on-write test data file should not be modified.")
224n/a # Ensuring copy-on-write maps cannot be resized
225n/a self.assertRaises(TypeError, m.resize, 2*mapsize)
226n/a m.close()
227n/a
228n/a # Ensuring invalid access parameter raises exception
229n/a with open(TESTFN, "r+b") as f:
230n/a self.assertRaises(ValueError, mmap.mmap, f.fileno(), mapsize, access=4)
231n/a
232n/a if os.name == "posix":
233n/a # Try incompatible flags, prot and access parameters.
234n/a with open(TESTFN, "r+b") as f:
235n/a self.assertRaises(ValueError, mmap.mmap, f.fileno(), mapsize,
236n/a flags=mmap.MAP_PRIVATE,
237n/a prot=mmap.PROT_READ, access=mmap.ACCESS_WRITE)
238n/a
239n/a # Try writing with PROT_EXEC and without PROT_WRITE
240n/a prot = mmap.PROT_READ | getattr(mmap, 'PROT_EXEC', 0)
241n/a with open(TESTFN, "r+b") as f:
242n/a m = mmap.mmap(f.fileno(), mapsize, prot=prot)
243n/a self.assertRaises(TypeError, m.write, b"abcdef")
244n/a self.assertRaises(TypeError, m.write_byte, 0)
245n/a m.close()
246n/a
247n/a def test_bad_file_desc(self):
248n/a # Try opening a bad file descriptor...
249n/a self.assertRaises(OSError, mmap.mmap, -2, 4096)
250n/a
251n/a def test_tougher_find(self):
252n/a # Do a tougher .find() test. SF bug 515943 pointed out that, in 2.2,
253n/a # searching for data with embedded \0 bytes didn't work.
254n/a with open(TESTFN, 'wb+') as f:
255n/a
256n/a data = b'aabaac\x00deef\x00\x00aa\x00'
257n/a n = len(data)
258n/a f.write(data)
259n/a f.flush()
260n/a m = mmap.mmap(f.fileno(), n)
261n/a
262n/a for start in range(n+1):
263n/a for finish in range(start, n+1):
264n/a slice = data[start : finish]
265n/a self.assertEqual(m.find(slice), data.find(slice))
266n/a self.assertEqual(m.find(slice + b'x'), -1)
267n/a m.close()
268n/a
269n/a def test_find_end(self):
270n/a # test the new 'end' parameter works as expected
271n/a f = open(TESTFN, 'wb+')
272n/a data = b'one two ones'
273n/a n = len(data)
274n/a f.write(data)
275n/a f.flush()
276n/a m = mmap.mmap(f.fileno(), n)
277n/a f.close()
278n/a
279n/a self.assertEqual(m.find(b'one'), 0)
280n/a self.assertEqual(m.find(b'ones'), 8)
281n/a self.assertEqual(m.find(b'one', 0, -1), 0)
282n/a self.assertEqual(m.find(b'one', 1), 8)
283n/a self.assertEqual(m.find(b'one', 1, -1), 8)
284n/a self.assertEqual(m.find(b'one', 1, -2), -1)
285n/a self.assertEqual(m.find(bytearray(b'one')), 0)
286n/a
287n/a
288n/a def test_rfind(self):
289n/a # test the new 'end' parameter works as expected
290n/a f = open(TESTFN, 'wb+')
291n/a data = b'one two ones'
292n/a n = len(data)
293n/a f.write(data)
294n/a f.flush()
295n/a m = mmap.mmap(f.fileno(), n)
296n/a f.close()
297n/a
298n/a self.assertEqual(m.rfind(b'one'), 8)
299n/a self.assertEqual(m.rfind(b'one '), 0)
300n/a self.assertEqual(m.rfind(b'one', 0, -1), 8)
301n/a self.assertEqual(m.rfind(b'one', 0, -2), 0)
302n/a self.assertEqual(m.rfind(b'one', 1, -1), 8)
303n/a self.assertEqual(m.rfind(b'one', 1, -2), -1)
304n/a self.assertEqual(m.rfind(bytearray(b'one')), 8)
305n/a
306n/a
307n/a def test_double_close(self):
308n/a # make sure a double close doesn't crash on Solaris (Bug# 665913)
309n/a f = open(TESTFN, 'wb+')
310n/a
311n/a f.write(2**16 * b'a') # Arbitrary character
312n/a f.close()
313n/a
314n/a f = open(TESTFN, 'rb')
315n/a mf = mmap.mmap(f.fileno(), 2**16, access=mmap.ACCESS_READ)
316n/a mf.close()
317n/a mf.close()
318n/a f.close()
319n/a
320n/a @unittest.skipUnless(hasattr(os, "stat"), "needs os.stat()")
321n/a def test_entire_file(self):
322n/a # test mapping of entire file by passing 0 for map length
323n/a f = open(TESTFN, "wb+")
324n/a
325n/a f.write(2**16 * b'm') # Arbitrary character
326n/a f.close()
327n/a
328n/a f = open(TESTFN, "rb+")
329n/a mf = mmap.mmap(f.fileno(), 0)
330n/a self.assertEqual(len(mf), 2**16, "Map size should equal file size.")
331n/a self.assertEqual(mf.read(2**16), 2**16 * b"m")
332n/a mf.close()
333n/a f.close()
334n/a
335n/a @unittest.skipUnless(hasattr(os, "stat"), "needs os.stat()")
336n/a def test_length_0_offset(self):
337n/a # Issue #10916: test mapping of remainder of file by passing 0 for
338n/a # map length with an offset doesn't cause a segfault.
339n/a # NOTE: allocation granularity is currently 65536 under Win64,
340n/a # and therefore the minimum offset alignment.
341n/a with open(TESTFN, "wb") as f:
342n/a f.write((65536 * 2) * b'm') # Arbitrary character
343n/a
344n/a with open(TESTFN, "rb") as f:
345n/a with mmap.mmap(f.fileno(), 0, offset=65536, access=mmap.ACCESS_READ) as mf:
346n/a self.assertRaises(IndexError, mf.__getitem__, 80000)
347n/a
348n/a @unittest.skipUnless(hasattr(os, "stat"), "needs os.stat()")
349n/a def test_length_0_large_offset(self):
350n/a # Issue #10959: test mapping of a file by passing 0 for
351n/a # map length with a large offset doesn't cause a segfault.
352n/a with open(TESTFN, "wb") as f:
353n/a f.write(115699 * b'm') # Arbitrary character
354n/a
355n/a with open(TESTFN, "w+b") as f:
356n/a self.assertRaises(ValueError, mmap.mmap, f.fileno(), 0,
357n/a offset=2147418112)
358n/a
359n/a def test_move(self):
360n/a # make move works everywhere (64-bit format problem earlier)
361n/a f = open(TESTFN, 'wb+')
362n/a
363n/a f.write(b"ABCDEabcde") # Arbitrary character
364n/a f.flush()
365n/a
366n/a mf = mmap.mmap(f.fileno(), 10)
367n/a mf.move(5, 0, 5)
368n/a self.assertEqual(mf[:], b"ABCDEABCDE", "Map move should have duplicated front 5")
369n/a mf.close()
370n/a f.close()
371n/a
372n/a # more excessive test
373n/a data = b"0123456789"
374n/a for dest in range(len(data)):
375n/a for src in range(len(data)):
376n/a for count in range(len(data) - max(dest, src)):
377n/a expected = data[:dest] + data[src:src+count] + data[dest+count:]
378n/a m = mmap.mmap(-1, len(data))
379n/a m[:] = data
380n/a m.move(dest, src, count)
381n/a self.assertEqual(m[:], expected)
382n/a m.close()
383n/a
384n/a # segfault test (Issue 5387)
385n/a m = mmap.mmap(-1, 100)
386n/a offsets = [-100, -1, 0, 1, 100]
387n/a for source, dest, size in itertools.product(offsets, offsets, offsets):
388n/a try:
389n/a m.move(source, dest, size)
390n/a except ValueError:
391n/a pass
392n/a
393n/a offsets = [(-1, -1, -1), (-1, -1, 0), (-1, 0, -1), (0, -1, -1),
394n/a (-1, 0, 0), (0, -1, 0), (0, 0, -1)]
395n/a for source, dest, size in offsets:
396n/a self.assertRaises(ValueError, m.move, source, dest, size)
397n/a
398n/a m.close()
399n/a
400n/a m = mmap.mmap(-1, 1) # single byte
401n/a self.assertRaises(ValueError, m.move, 0, 0, 2)
402n/a self.assertRaises(ValueError, m.move, 1, 0, 1)
403n/a self.assertRaises(ValueError, m.move, 0, 1, 1)
404n/a m.move(0, 0, 1)
405n/a m.move(0, 0, 0)
406n/a
407n/a
408n/a def test_anonymous(self):
409n/a # anonymous mmap.mmap(-1, PAGE)
410n/a m = mmap.mmap(-1, PAGESIZE)
411n/a for x in range(PAGESIZE):
412n/a self.assertEqual(m[x], 0,
413n/a "anonymously mmap'ed contents should be zero")
414n/a
415n/a for x in range(PAGESIZE):
416n/a b = x & 0xff
417n/a m[x] = b
418n/a self.assertEqual(m[x], b)
419n/a
420n/a def test_read_all(self):
421n/a m = mmap.mmap(-1, 16)
422n/a self.addCleanup(m.close)
423n/a
424n/a # With no parameters, or None or a negative argument, reads all
425n/a m.write(bytes(range(16)))
426n/a m.seek(0)
427n/a self.assertEqual(m.read(), bytes(range(16)))
428n/a m.seek(8)
429n/a self.assertEqual(m.read(), bytes(range(8, 16)))
430n/a m.seek(16)
431n/a self.assertEqual(m.read(), b'')
432n/a m.seek(3)
433n/a self.assertEqual(m.read(None), bytes(range(3, 16)))
434n/a m.seek(4)
435n/a self.assertEqual(m.read(-1), bytes(range(4, 16)))
436n/a m.seek(5)
437n/a self.assertEqual(m.read(-2), bytes(range(5, 16)))
438n/a m.seek(9)
439n/a self.assertEqual(m.read(-42), bytes(range(9, 16)))
440n/a
441n/a def test_read_invalid_arg(self):
442n/a m = mmap.mmap(-1, 16)
443n/a self.addCleanup(m.close)
444n/a
445n/a self.assertRaises(TypeError, m.read, 'foo')
446n/a self.assertRaises(TypeError, m.read, 5.5)
447n/a self.assertRaises(TypeError, m.read, [1, 2, 3])
448n/a
449n/a def test_extended_getslice(self):
450n/a # Test extended slicing by comparing with list slicing.
451n/a s = bytes(reversed(range(256)))
452n/a m = mmap.mmap(-1, len(s))
453n/a m[:] = s
454n/a self.assertEqual(m[:], s)
455n/a indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
456n/a for start in indices:
457n/a for stop in indices:
458n/a # Skip step 0 (invalid)
459n/a for step in indices[1:]:
460n/a self.assertEqual(m[start:stop:step],
461n/a s[start:stop:step])
462n/a
463n/a def test_extended_set_del_slice(self):
464n/a # Test extended slicing by comparing with list slicing.
465n/a s = bytes(reversed(range(256)))
466n/a m = mmap.mmap(-1, len(s))
467n/a indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
468n/a for start in indices:
469n/a for stop in indices:
470n/a # Skip invalid step 0
471n/a for step in indices[1:]:
472n/a m[:] = s
473n/a self.assertEqual(m[:], s)
474n/a L = list(s)
475n/a # Make sure we have a slice of exactly the right length,
476n/a # but with different data.
477n/a data = L[start:stop:step]
478n/a data = bytes(reversed(data))
479n/a L[start:stop:step] = data
480n/a m[start:stop:step] = data
481n/a self.assertEqual(m[:], bytes(L))
482n/a
483n/a def make_mmap_file (self, f, halfsize):
484n/a # Write 2 pages worth of data to the file
485n/a f.write (b'\0' * halfsize)
486n/a f.write (b'foo')
487n/a f.write (b'\0' * (halfsize - 3))
488n/a f.flush ()
489n/a return mmap.mmap (f.fileno(), 0)
490n/a
491n/a def test_empty_file (self):
492n/a f = open (TESTFN, 'w+b')
493n/a f.close()
494n/a with open(TESTFN, "rb") as f :
495n/a self.assertRaisesRegex(ValueError,
496n/a "cannot mmap an empty file",
497n/a mmap.mmap, f.fileno(), 0,
498n/a access=mmap.ACCESS_READ)
499n/a
500n/a def test_offset (self):
501n/a f = open (TESTFN, 'w+b')
502n/a
503n/a try: # unlink TESTFN no matter what
504n/a halfsize = mmap.ALLOCATIONGRANULARITY
505n/a m = self.make_mmap_file (f, halfsize)
506n/a m.close ()
507n/a f.close ()
508n/a
509n/a mapsize = halfsize * 2
510n/a # Try invalid offset
511n/a f = open(TESTFN, "r+b")
512n/a for offset in [-2, -1, None]:
513n/a try:
514n/a m = mmap.mmap(f.fileno(), mapsize, offset=offset)
515n/a self.assertEqual(0, 1)
516n/a except (ValueError, TypeError, OverflowError):
517n/a pass
518n/a else:
519n/a self.assertEqual(0, 0)
520n/a f.close()
521n/a
522n/a # Try valid offset, hopefully 8192 works on all OSes
523n/a f = open(TESTFN, "r+b")
524n/a m = mmap.mmap(f.fileno(), mapsize - halfsize, offset=halfsize)
525n/a self.assertEqual(m[0:3], b'foo')
526n/a f.close()
527n/a
528n/a # Try resizing map
529n/a try:
530n/a m.resize(512)
531n/a except SystemError:
532n/a pass
533n/a else:
534n/a # resize() is supported
535n/a self.assertEqual(len(m), 512)
536n/a # Check that we can no longer seek beyond the new size.
537n/a self.assertRaises(ValueError, m.seek, 513, 0)
538n/a # Check that the content is not changed
539n/a self.assertEqual(m[0:3], b'foo')
540n/a
541n/a # Check that the underlying file is truncated too
542n/a f = open(TESTFN, 'rb')
543n/a f.seek(0, 2)
544n/a self.assertEqual(f.tell(), halfsize + 512)
545n/a f.close()
546n/a self.assertEqual(m.size(), halfsize + 512)
547n/a
548n/a m.close()
549n/a
550n/a finally:
551n/a f.close()
552n/a try:
553n/a os.unlink(TESTFN)
554n/a except OSError:
555n/a pass
556n/a
557n/a def test_subclass(self):
558n/a class anon_mmap(mmap.mmap):
559n/a def __new__(klass, *args, **kwargs):
560n/a return mmap.mmap.__new__(klass, -1, *args, **kwargs)
561n/a anon_mmap(PAGESIZE)
562n/a
563n/a @unittest.skipUnless(hasattr(mmap, 'PROT_READ'), "needs mmap.PROT_READ")
564n/a def test_prot_readonly(self):
565n/a mapsize = 10
566n/a with open(TESTFN, "wb") as fp:
567n/a fp.write(b"a"*mapsize)
568n/a f = open(TESTFN, "rb")
569n/a m = mmap.mmap(f.fileno(), mapsize, prot=mmap.PROT_READ)
570n/a self.assertRaises(TypeError, m.write, "foo")
571n/a f.close()
572n/a
573n/a def test_error(self):
574n/a self.assertIs(mmap.error, OSError)
575n/a
576n/a def test_io_methods(self):
577n/a data = b"0123456789"
578n/a with open(TESTFN, "wb") as fp:
579n/a fp.write(b"x"*len(data))
580n/a f = open(TESTFN, "r+b")
581n/a m = mmap.mmap(f.fileno(), len(data))
582n/a f.close()
583n/a # Test write_byte()
584n/a for i in range(len(data)):
585n/a self.assertEqual(m.tell(), i)
586n/a m.write_byte(data[i])
587n/a self.assertEqual(m.tell(), i+1)
588n/a self.assertRaises(ValueError, m.write_byte, b"x"[0])
589n/a self.assertEqual(m[:], data)
590n/a # Test read_byte()
591n/a m.seek(0)
592n/a for i in range(len(data)):
593n/a self.assertEqual(m.tell(), i)
594n/a self.assertEqual(m.read_byte(), data[i])
595n/a self.assertEqual(m.tell(), i+1)
596n/a self.assertRaises(ValueError, m.read_byte)
597n/a # Test read()
598n/a m.seek(3)
599n/a self.assertEqual(m.read(3), b"345")
600n/a self.assertEqual(m.tell(), 6)
601n/a # Test write()
602n/a m.seek(3)
603n/a m.write(b"bar")
604n/a self.assertEqual(m.tell(), 6)
605n/a self.assertEqual(m[:], b"012bar6789")
606n/a m.write(bytearray(b"baz"))
607n/a self.assertEqual(m.tell(), 9)
608n/a self.assertEqual(m[:], b"012barbaz9")
609n/a self.assertRaises(ValueError, m.write, b"ba")
610n/a
611n/a def test_non_ascii_byte(self):
612n/a for b in (129, 200, 255): # > 128
613n/a m = mmap.mmap(-1, 1)
614n/a m.write_byte(b)
615n/a self.assertEqual(m[0], b)
616n/a m.seek(0)
617n/a self.assertEqual(m.read_byte(), b)
618n/a m.close()
619n/a
620n/a @unittest.skipUnless(os.name == 'nt', 'requires Windows')
621n/a def test_tagname(self):
622n/a data1 = b"0123456789"
623n/a data2 = b"abcdefghij"
624n/a assert len(data1) == len(data2)
625n/a
626n/a # Test same tag
627n/a m1 = mmap.mmap(-1, len(data1), tagname="foo")
628n/a m1[:] = data1
629n/a m2 = mmap.mmap(-1, len(data2), tagname="foo")
630n/a m2[:] = data2
631n/a self.assertEqual(m1[:], data2)
632n/a self.assertEqual(m2[:], data2)
633n/a m2.close()
634n/a m1.close()
635n/a
636n/a # Test different tag
637n/a m1 = mmap.mmap(-1, len(data1), tagname="foo")
638n/a m1[:] = data1
639n/a m2 = mmap.mmap(-1, len(data2), tagname="boo")
640n/a m2[:] = data2
641n/a self.assertEqual(m1[:], data1)
642n/a self.assertEqual(m2[:], data2)
643n/a m2.close()
644n/a m1.close()
645n/a
646n/a @cpython_only
647n/a @unittest.skipUnless(os.name == 'nt', 'requires Windows')
648n/a def test_sizeof(self):
649n/a m1 = mmap.mmap(-1, 100)
650n/a tagname = "foo"
651n/a m2 = mmap.mmap(-1, 100, tagname=tagname)
652n/a self.assertEqual(sys.getsizeof(m2),
653n/a sys.getsizeof(m1) + len(tagname) + 1)
654n/a
655n/a @unittest.skipUnless(os.name == 'nt', 'requires Windows')
656n/a def test_crasher_on_windows(self):
657n/a # Should not crash (Issue 1733986)
658n/a m = mmap.mmap(-1, 1000, tagname="foo")
659n/a try:
660n/a mmap.mmap(-1, 5000, tagname="foo")[:] # same tagname, but larger size
661n/a except:
662n/a pass
663n/a m.close()
664n/a
665n/a # Should not crash (Issue 5385)
666n/a with open(TESTFN, "wb") as fp:
667n/a fp.write(b"x"*10)
668n/a f = open(TESTFN, "r+b")
669n/a m = mmap.mmap(f.fileno(), 0)
670n/a f.close()
671n/a try:
672n/a m.resize(0) # will raise OSError
673n/a except:
674n/a pass
675n/a try:
676n/a m[:]
677n/a except:
678n/a pass
679n/a m.close()
680n/a
681n/a @unittest.skipUnless(os.name == 'nt', 'requires Windows')
682n/a def test_invalid_descriptor(self):
683n/a # socket file descriptors are valid, but out of range
684n/a # for _get_osfhandle, causing a crash when validating the
685n/a # parameters to _get_osfhandle.
686n/a s = socket.socket()
687n/a try:
688n/a with self.assertRaises(OSError):
689n/a m = mmap.mmap(s.fileno(), 10)
690n/a finally:
691n/a s.close()
692n/a
693n/a def test_context_manager(self):
694n/a with mmap.mmap(-1, 10) as m:
695n/a self.assertFalse(m.closed)
696n/a self.assertTrue(m.closed)
697n/a
698n/a def test_context_manager_exception(self):
699n/a # Test that the OSError gets passed through
700n/a with self.assertRaises(Exception) as exc:
701n/a with mmap.mmap(-1, 10) as m:
702n/a raise OSError
703n/a self.assertIsInstance(exc.exception, OSError,
704n/a "wrong exception raised in context manager")
705n/a self.assertTrue(m.closed, "context manager failed")
706n/a
707n/a def test_weakref(self):
708n/a # Check mmap objects are weakrefable
709n/a mm = mmap.mmap(-1, 16)
710n/a wr = weakref.ref(mm)
711n/a self.assertIs(wr(), mm)
712n/a del mm
713n/a gc_collect()
714n/a self.assertIs(wr(), None)
715n/a
716n/a def test_write_returning_the_number_of_bytes_written(self):
717n/a mm = mmap.mmap(-1, 16)
718n/a self.assertEqual(mm.write(b""), 0)
719n/a self.assertEqual(mm.write(b"x"), 1)
720n/a self.assertEqual(mm.write(b"yz"), 2)
721n/a self.assertEqual(mm.write(b"python"), 6)
722n/a
723n/a @unittest.skipIf(os.name == 'nt', 'cannot resize anonymous mmaps on Windows')
724n/a def test_resize_past_pos(self):
725n/a m = mmap.mmap(-1, 8192)
726n/a self.addCleanup(m.close)
727n/a m.read(5000)
728n/a try:
729n/a m.resize(4096)
730n/a except SystemError:
731n/a self.skipTest("resizing not supported")
732n/a self.assertEqual(m.read(14), b'')
733n/a self.assertRaises(ValueError, m.read_byte)
734n/a self.assertRaises(ValueError, m.write_byte, 42)
735n/a self.assertRaises(ValueError, m.write, b'abc')
736n/a
737n/a
738n/aclass LargeMmapTests(unittest.TestCase):
739n/a
740n/a def setUp(self):
741n/a unlink(TESTFN)
742n/a
743n/a def tearDown(self):
744n/a unlink(TESTFN)
745n/a
746n/a def _make_test_file(self, num_zeroes, tail):
747n/a if sys.platform[:3] == 'win' or sys.platform == 'darwin':
748n/a requires('largefile',
749n/a 'test requires %s bytes and a long time to run' % str(0x180000000))
750n/a f = open(TESTFN, 'w+b')
751n/a try:
752n/a f.seek(num_zeroes)
753n/a f.write(tail)
754n/a f.flush()
755n/a except (OSError, OverflowError, ValueError):
756n/a try:
757n/a f.close()
758n/a except (OSError, OverflowError):
759n/a pass
760n/a raise unittest.SkipTest("filesystem does not have largefile support")
761n/a return f
762n/a
763n/a def test_large_offset(self):
764n/a with self._make_test_file(0x14FFFFFFF, b" ") as f:
765n/a with mmap.mmap(f.fileno(), 0, offset=0x140000000, access=mmap.ACCESS_READ) as m:
766n/a self.assertEqual(m[0xFFFFFFF], 32)
767n/a
768n/a def test_large_filesize(self):
769n/a with self._make_test_file(0x17FFFFFFF, b" ") as f:
770n/a if sys.maxsize < 0x180000000:
771n/a # On 32 bit platforms the file is larger than sys.maxsize so
772n/a # mapping the whole file should fail -- Issue #16743
773n/a with self.assertRaises(OverflowError):
774n/a mmap.mmap(f.fileno(), 0x180000000, access=mmap.ACCESS_READ)
775n/a with self.assertRaises(ValueError):
776n/a mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
777n/a with mmap.mmap(f.fileno(), 0x10000, access=mmap.ACCESS_READ) as m:
778n/a self.assertEqual(m.size(), 0x180000000)
779n/a
780n/a # Issue 11277: mmap() with large (~4GB) sparse files crashes on OS X.
781n/a
782n/a def _test_around_boundary(self, boundary):
783n/a tail = b' DEARdear '
784n/a start = boundary - len(tail) // 2
785n/a end = start + len(tail)
786n/a with self._make_test_file(start, tail) as f:
787n/a with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as m:
788n/a self.assertEqual(m[start:end], tail)
789n/a
790n/a @unittest.skipUnless(sys.maxsize > _4G, "test cannot run on 32-bit systems")
791n/a def test_around_2GB(self):
792n/a self._test_around_boundary(_2G)
793n/a
794n/a @unittest.skipUnless(sys.maxsize > _4G, "test cannot run on 32-bit systems")
795n/a def test_around_4GB(self):
796n/a self._test_around_boundary(_4G)
797n/a
798n/a
799n/adef test_main():
800n/a run_unittest(MmapTests, LargeMmapTests)
801n/a
802n/aif __name__ == '__main__':
803n/a test_main()