diff options
author | Tim Golden <mail@timgolden.me.uk> | 2021-10-29 08:20:21 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-29 08:20:21 (GMT) |
commit | 7bddd96982072d04bd6314da1ee7f40b7f875f00 (patch) | |
tree | 25946dd67ce9d7170cdeb681784a87b9e6a4f0e6 /Lib/test/test_mmap.py | |
parent | 074fa5750640a067d9894c69378a00ceecc3b948 (diff) | |
download | cpython-7bddd96982072d04bd6314da1ee7f40b7f875f00.zip cpython-7bddd96982072d04bd6314da1ee7f40b7f875f00.tar.gz cpython-7bddd96982072d04bd6314da1ee7f40b7f875f00.tar.bz2 |
bpo-45621: Small changes to mmap (GH-29247)
* Small tidy-ups / comments
* Use randomized names when testing tagged mmaps to avoid any risk of parallel tests treading on each others' toes
Diffstat (limited to 'Lib/test/test_mmap.py')
-rw-r--r-- | Lib/test/test_mmap.py | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index 82e2d2a..014171c 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -7,6 +7,7 @@ import re import itertools import random import socket +import string import sys import weakref @@ -15,6 +16,10 @@ mmap = import_module('mmap') PAGESIZE = mmap.PAGESIZE +tagname_prefix = f'python_{os.getpid()}_test_mmap' +def random_tagname(length=10): + suffix = ''.join(random.choices(string.ascii_uppercase, k=length)) + return f'{tagname_prefix}_{suffix}' class MmapTests(unittest.TestCase): @@ -610,11 +615,13 @@ class MmapTests(unittest.TestCase): data1 = b"0123456789" data2 = b"abcdefghij" assert len(data1) == len(data2) + tagname1 = random_tagname() + tagname2 = random_tagname() # Test same tag - m1 = mmap.mmap(-1, len(data1), tagname="foo") + m1 = mmap.mmap(-1, len(data1), tagname=tagname1) m1[:] = data1 - m2 = mmap.mmap(-1, len(data2), tagname="foo") + m2 = mmap.mmap(-1, len(data2), tagname=tagname1) m2[:] = data2 self.assertEqual(m1[:], data2) self.assertEqual(m2[:], data2) @@ -622,9 +629,9 @@ class MmapTests(unittest.TestCase): m1.close() # Test different tag - m1 = mmap.mmap(-1, len(data1), tagname="foo") + m1 = mmap.mmap(-1, len(data1), tagname=tagname1) m1[:] = data1 - m2 = mmap.mmap(-1, len(data2), tagname="boo") + m2 = mmap.mmap(-1, len(data2), tagname=tagname2) m2[:] = data2 self.assertEqual(m1[:], data1) self.assertEqual(m2[:], data2) @@ -635,7 +642,7 @@ class MmapTests(unittest.TestCase): @unittest.skipUnless(os.name == 'nt', 'requires Windows') def test_sizeof(self): m1 = mmap.mmap(-1, 100) - tagname = "foo" + tagname = random_tagname() m2 = mmap.mmap(-1, 100, tagname=tagname) self.assertEqual(sys.getsizeof(m2), sys.getsizeof(m1) + len(tagname) + 1) @@ -643,9 +650,10 @@ class MmapTests(unittest.TestCase): @unittest.skipUnless(os.name == 'nt', 'requires Windows') def test_crasher_on_windows(self): # Should not crash (Issue 1733986) - m = mmap.mmap(-1, 1000, tagname="foo") + tagname = random_tagname() + m = mmap.mmap(-1, 1000, tagname=tagname) try: - mmap.mmap(-1, 5000, tagname="foo")[:] # same tagname, but larger size + mmap.mmap(-1, 5000, tagname=tagname)[:] # same tagname, but larger size except: pass m.close() @@ -857,7 +865,7 @@ class MmapTests(unittest.TestCase): """ start_size = 2 * PAGESIZE reduced_size = PAGESIZE - tagname = "TEST" + tagname = random_tagname() data_length = 8 data = bytes(random.getrandbits(8) for _ in range(data_length)) |