diff options
author | Taine Zhao <twshere@outlook.com> | 2019-10-17 18:41:35 +0800 |
---|---|---|
committer | Xiang Zhang <angwerzx@126.com> | 2019-10-17 18:41:35 +0800 |
commit | d8ca2354ed30c12b9ce37c4535222b700a727b32 (patch) | |
tree | 19976661fd0a220047cd06bbed695e9452c73eea /Lib/test/test_mmap.py | |
parent | 9c11029bb41caab5576f354fbf808a5e91325bb0 (diff) | |
download | cpython-git-d8ca2354ed30c12b9ce37c4535222b700a727b32.tar.gz |
bpo-34953: Implement `mmap.mmap.__repr__` (GH-9891)
Diffstat (limited to 'Lib/test/test_mmap.py')
-rw-r--r-- | Lib/test/test_mmap.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index 88c501d81a..5400f25f50 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -740,6 +740,42 @@ class MmapTests(unittest.TestCase): # See bpo-34754 for details. self.assertRaises(OSError, mm.flush, 1, len(b'python')) + def test_repr(self): + open_mmap_repr_pat = re.compile( + r"<mmap.mmap closed=False, " + r"access=(?P<access>\S+), " + r"length=(?P<length>\d+), " + r"pos=(?P<pos>\d+), " + r"offset=(?P<offset>\d+)>") + closed_mmap_repr_pat = re.compile(r"<mmap.mmap closed=True>") + mapsizes = (50, 100, 1_000, 1_000_000, 10_000_000) + offsets = tuple((mapsize // 2 // mmap.ALLOCATIONGRANULARITY) + * mmap.ALLOCATIONGRANULARITY for mapsize in mapsizes) + for offset, mapsize in zip(offsets, mapsizes): + data = b'a' * mapsize + length = mapsize - offset + accesses = ('ACCESS_DEFAULT', 'ACCESS_READ', + 'ACCESS_COPY', 'ACCESS_WRITE') + positions = (0, length//10, length//5, length//4) + with open(TESTFN, "wb+") as fp: + fp.write(data) + fp.flush() + for access, pos in itertools.product(accesses, positions): + accint = getattr(mmap, access) + with mmap.mmap(fp.fileno(), + length, + access=accint, + offset=offset) as mm: + mm.seek(pos) + match = open_mmap_repr_pat.match(repr(mm)) + self.assertIsNotNone(match) + self.assertEqual(match.group('access'), access) + self.assertEqual(match.group('length'), str(length)) + self.assertEqual(match.group('pos'), str(pos)) + self.assertEqual(match.group('offset'), str(offset)) + match = closed_mmap_repr_pat.match(repr(mm)) + self.assertIsNotNone(match) + @unittest.skipUnless(hasattr(mmap.mmap, 'madvise'), 'needs madvise') def test_madvise(self): size = 2 * PAGESIZE |