Skip to content

Commit 87502dd

Browse files
authored
bpo-40286: Use random.randbytes() in tests (GH-19575)
1 parent 223221b commit 87502dd

File tree

5 files changed

+9
-28
lines changed

5 files changed

+9
-28
lines changed

Lib/test/test_bz2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ def testEOFError(self):
710710
def testDecompress4G(self, size):
711711
# "Test BZ2Decompressor.decompress() with >4GiB input"
712712
blocksize = 10 * 1024 * 1024
713-
block = random.getrandbits(blocksize * 8).to_bytes(blocksize, 'little')
713+
block = random.randbytes(blocksize)
714714
try:
715715
data = block * (size // blocksize + 1)
716716
compressed = bz2.compress(data)

Lib/test/test_lzma.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ def test_compressor_bigmem(self, size):
350350
def test_decompressor_bigmem(self, size):
351351
lzd = LZMADecompressor()
352352
blocksize = 10 * 1024 * 1024
353-
block = random.getrandbits(blocksize * 8).to_bytes(blocksize, "little")
353+
block = random.randbytes(blocksize)
354354
try:
355355
input = block * (size // blocksize + 1)
356356
cdata = lzma.compress(input)

Lib/test/test_tarfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ def test_null_tarfile(self):
386386
def test_ignore_zeros(self):
387387
# Test TarFile's ignore_zeros option.
388388
# generate 512 pseudorandom bytes
389-
data = Random(0).getrandbits(512*8).to_bytes(512, 'big')
389+
data = Random(0).randbytes(512)
390390
for char in (b'\0', b'a'):
391391
# Test if EOFHeaderError ('\0') and InvalidHeaderError ('a')
392392
# are ignored correctly.

Lib/test/test_zipfile.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
from tempfile import TemporaryFile
19-
from random import randint, random, getrandbits
19+
from random import randint, random, randbytes
2020

2121
from test.support import script_helper
2222
from test.support import (TESTFN, findfile, unlink, rmtree, temp_dir, temp_cwd,
@@ -33,9 +33,6 @@
3333
('ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'),
3434
('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')]
3535

36-
def getrandbytes(size):
37-
return getrandbits(8 * size).to_bytes(size, 'little')
38-
3936
def get_files(test):
4037
yield TESTFN2
4138
with TemporaryFile() as f:
@@ -324,7 +321,7 @@ def test_read_return_size(self):
324321
# than requested.
325322
for test_size in (1, 4095, 4096, 4097, 16384):
326323
file_size = test_size + 1
327-
junk = getrandbytes(file_size)
324+
junk = randbytes(file_size)
328325
with zipfile.ZipFile(io.BytesIO(), "w", self.compression) as zipf:
329326
zipf.writestr('foo', junk)
330327
with zipf.open('foo', 'r') as fp:
@@ -2423,8 +2420,8 @@ def test_open_write(self):
24232420
class TestsWithMultipleOpens(unittest.TestCase):
24242421
@classmethod
24252422
def setUpClass(cls):
2426-
cls.data1 = b'111' + getrandbytes(10000)
2427-
cls.data2 = b'222' + getrandbytes(10000)
2423+
cls.data1 = b'111' + randbytes(10000)
2424+
cls.data2 = b'222' + randbytes(10000)
24282425

24292426
def make_test_archive(self, f):
24302427
# Create the ZIP archive

Lib/test/test_zlib.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ def check_big_compress_buffer(self, size, compress_func):
134134
# Generate 10 MiB worth of random, and expand it by repeating it.
135135
# The assumption is that zlib's memory is not big enough to exploit
136136
# such spread out redundancy.
137-
data = b''.join([random.getrandbits(8 * _1M).to_bytes(_1M, 'little')
138-
for i in range(10)])
137+
data = random.randbytes(_1M * 10)
139138
data = data * (size // len(data) + 1)
140139
try:
141140
compress_func(data)
@@ -488,7 +487,7 @@ def test_odd_flush(self):
488487
# others might simply have a single RNG
489488
gen = random
490489
gen.seed(1)
491-
data = genblock(1, 17 * 1024, generator=gen)
490+
data = gen.randbytes(17 * 1024)
492491

493492
# compress, sync-flush, and decompress
494493
first = co.compress(data)
@@ -825,20 +824,6 @@ def test_wbits(self):
825824
self.assertEqual(dco.decompress(gzip), HAMLET_SCENE)
826825

827826

828-
def genblock(seed, length, step=1024, generator=random):
829-
"""length-byte stream of random data from a seed (in step-byte blocks)."""
830-
if seed is not None:
831-
generator.seed(seed)
832-
randint = generator.randint
833-
if length < step or step < 2:
834-
step = length
835-
blocks = bytes()
836-
for i in range(0, length, step):
837-
blocks += bytes(randint(0, 255) for x in range(step))
838-
return blocks
839-
840-
841-
842827
def choose_lines(source, number, seed=None, generator=random):
843828
"""Return a list of number lines randomly chosen from the source"""
844829
if seed is not None:
@@ -847,7 +832,6 @@ def choose_lines(source, number, seed=None, generator=random):
847832
return [generator.choice(sources) for n in range(number)]
848833

849834

850-
851835
HAMLET_SCENE = b"""
852836
LAERTES
853837

0 commit comments

Comments
 (0)