Skip to content

Commit b22d021

Browse files
hugovkAlexWaygood
andauthored
gh-100176: Tools/iobench: Remove redundant compat code for Python <= 3.2 (#100197)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent 3310b94 commit b22d021

File tree

1 file changed

+54
-42
lines changed

1 file changed

+54
-42
lines changed

Tools/iobench/iobench.py

+54-42
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# -*- coding: utf-8 -*-
2-
# This file should be kept compatible with both Python 2.6 and Python >= 3.0.
3-
41
import itertools
52
import os
63
import platform
@@ -14,39 +11,37 @@
1411
TEXT_ENCODING = 'utf8'
1512
NEWLINES = 'lf'
1613

17-
# Compatibility
18-
try:
19-
xrange
20-
except NameError:
21-
xrange = range
2214

2315
def text_open(fn, mode, encoding=None):
2416
try:
2517
return open(fn, mode, encoding=encoding or TEXT_ENCODING)
2618
except TypeError:
27-
if 'r' in mode:
28-
mode += 'U' # 'U' mode is needed only in Python 2.x
2919
return open(fn, mode)
3020

21+
3122
def get_file_sizes():
3223
for s in ['20 KiB', '400 KiB', '10 MiB']:
3324
size, unit = s.split()
3425
size = int(size) * {'KiB': 1024, 'MiB': 1024 ** 2}[unit]
3526
yield s.replace(' ', ''), size
3627

28+
3729
def get_binary_files():
3830
return ((name + ".bin", size) for name, size in get_file_sizes())
3931

32+
4033
def get_text_files():
41-
return (("%s-%s-%s.txt" % (name, TEXT_ENCODING, NEWLINES), size)
34+
return ((f"{name}-{TEXT_ENCODING}-{NEWLINES}.txt", size)
4235
for name, size in get_file_sizes())
4336

37+
4438
def with_open_mode(mode):
4539
def decorate(f):
4640
f.file_open_mode = mode
4741
return f
4842
return decorate
4943

44+
5045
def with_sizes(*sizes):
5146
def decorate(f):
5247
f.file_sizes = sizes
@@ -64,6 +59,7 @@ def read_bytewise(f):
6459
while f.read(1):
6560
pass
6661

62+
6763
@with_open_mode("r")
6864
@with_sizes("medium")
6965
def read_small_chunks(f):
@@ -72,6 +68,7 @@ def read_small_chunks(f):
7268
while f.read(20):
7369
pass
7470

71+
7572
@with_open_mode("r")
7673
@with_sizes("medium")
7774
def read_big_chunks(f):
@@ -80,6 +77,7 @@ def read_big_chunks(f):
8077
while f.read(4096):
8178
pass
8279

80+
8381
@with_open_mode("r")
8482
@with_sizes("small", "medium", "large")
8583
def read_whole_file(f):
@@ -88,6 +86,7 @@ def read_whole_file(f):
8886
while f.read():
8987
pass
9088

89+
9190
@with_open_mode("rt")
9291
@with_sizes("medium")
9392
def read_lines(f):
@@ -96,26 +95,29 @@ def read_lines(f):
9695
for line in f:
9796
pass
9897

98+
9999
@with_open_mode("r")
100100
@with_sizes("medium")
101101
def seek_forward_bytewise(f):
102102
""" seek forward one unit at a time """
103103
f.seek(0, 2)
104104
size = f.tell()
105105
f.seek(0, 0)
106-
for i in xrange(0, size - 1):
106+
for i in range(0, size - 1):
107107
f.seek(i, 0)
108108

109+
109110
@with_open_mode("r")
110111
@with_sizes("medium")
111112
def seek_forward_blockwise(f):
112113
""" seek forward 1000 units at a time """
113114
f.seek(0, 2)
114115
size = f.tell()
115116
f.seek(0, 0)
116-
for i in xrange(0, size - 1, 1000):
117+
for i in range(0, size - 1, 1000):
117118
f.seek(i, 0)
118119

120+
119121
@with_open_mode("rb")
120122
@with_sizes("medium")
121123
def read_seek_bytewise(f):
@@ -124,6 +126,7 @@ def read_seek_bytewise(f):
124126
while f.read(1):
125127
f.seek(1, 1)
126128

129+
127130
@with_open_mode("rb")
128131
@with_sizes("medium")
129132
def read_seek_blockwise(f):
@@ -137,28 +140,31 @@ def read_seek_blockwise(f):
137140
@with_sizes("small")
138141
def write_bytewise(f, source):
139142
""" write one unit at a time """
140-
for i in xrange(0, len(source)):
143+
for i in range(0, len(source)):
141144
f.write(source[i:i+1])
142145

146+
143147
@with_open_mode("w")
144148
@with_sizes("medium")
145149
def write_small_chunks(f, source):
146150
""" write 20 units at a time """
147-
for i in xrange(0, len(source), 20):
151+
for i in range(0, len(source), 20):
148152
f.write(source[i:i+20])
149153

154+
150155
@with_open_mode("w")
151156
@with_sizes("medium")
152157
def write_medium_chunks(f, source):
153158
""" write 4096 units at a time """
154-
for i in xrange(0, len(source), 4096):
159+
for i in range(0, len(source), 4096):
155160
f.write(source[i:i+4096])
156161

162+
157163
@with_open_mode("w")
158164
@with_sizes("large")
159165
def write_large_chunks(f, source):
160166
""" write 1e6 units at a time """
161-
for i in xrange(0, len(source), 1000000):
167+
for i in range(0, len(source), 1000000):
162168
f.write(source[i:i+1000000])
163169

164170

@@ -167,59 +173,65 @@ def write_large_chunks(f, source):
167173
def modify_bytewise(f, source):
168174
""" modify one unit at a time """
169175
f.seek(0)
170-
for i in xrange(0, len(source)):
176+
for i in range(0, len(source)):
171177
f.write(source[i:i+1])
172178

179+
173180
@with_open_mode("w+")
174181
@with_sizes("medium")
175182
def modify_small_chunks(f, source):
176183
""" modify 20 units at a time """
177184
f.seek(0)
178-
for i in xrange(0, len(source), 20):
185+
for i in range(0, len(source), 20):
179186
f.write(source[i:i+20])
180187

188+
181189
@with_open_mode("w+")
182190
@with_sizes("medium")
183191
def modify_medium_chunks(f, source):
184192
""" modify 4096 units at a time """
185193
f.seek(0)
186-
for i in xrange(0, len(source), 4096):
194+
for i in range(0, len(source), 4096):
187195
f.write(source[i:i+4096])
188196

197+
189198
@with_open_mode("wb+")
190199
@with_sizes("medium")
191200
def modify_seek_forward_bytewise(f, source):
192201
""" alternate write & seek one unit """
193202
f.seek(0)
194-
for i in xrange(0, len(source), 2):
203+
for i in range(0, len(source), 2):
195204
f.write(source[i:i+1])
196205
f.seek(i+2)
197206

207+
198208
@with_open_mode("wb+")
199209
@with_sizes("medium")
200210
def modify_seek_forward_blockwise(f, source):
201211
""" alternate write & seek 1000 units """
202212
f.seek(0)
203-
for i in xrange(0, len(source), 2000):
213+
for i in range(0, len(source), 2000):
204214
f.write(source[i:i+1000])
205215
f.seek(i+2000)
206216

217+
207218
# XXX the 2 following tests don't work with py3k's text IO
208219
@with_open_mode("wb+")
209220
@with_sizes("medium")
210221
def read_modify_bytewise(f, source):
211222
""" alternate read & write one unit """
212223
f.seek(0)
213-
for i in xrange(0, len(source), 2):
224+
for i in range(0, len(source), 2):
214225
f.read(1)
215226
f.write(source[i+1:i+2])
216227

228+
217229
@with_open_mode("wb+")
218230
@with_sizes("medium")
219231
def read_modify_blockwise(f, source):
220232
""" alternate read & write 1000 units """
221233
f.seek(0)
222-
for i in xrange(0, len(source), 2000):
234+
for i in range(0, len(source), 2000):
223235
f.read(1000)
224236
f.write(source[i+1000:i+2000])
225237

@@ -242,6 +254,7 @@ def read_modify_blockwise(f, source):
242254
read_modify_bytewise, read_modify_blockwise,
243255
]
244256

257+
245258
def run_during(duration, func):
246259
_t = time.time
247260
n = 0
@@ -257,6 +270,7 @@ def run_during(duration, func):
257270
real = (end[4] if start[4] else time.time()) - real_start
258271
return n, real, sum(end[0:2]) - sum(start[0:2])
259272

273+
260274
def warm_cache(filename):
261275
with open(filename, "rb") as f:
262276
f.read()
@@ -266,18 +280,17 @@ def run_all_tests(options):
266280
def print_label(filename, func):
267281
name = re.split(r'[-.]', filename)[0]
268282
out.write(
269-
("[%s] %s... "
270-
% (name.center(7), func.__doc__.strip())
271-
).ljust(52))
283+
f"[{name.center(7)}] {func.__doc__.strip()}... ".ljust(52))
272284
out.flush()
273285

274286
def print_results(size, n, real, cpu):
275287
bw = n * float(size) / 1024 ** 2 / real
276288
bw = ("%4d MiB/s" if bw > 100 else "%.3g MiB/s") % bw
277289
out.write(bw.rjust(12) + "\n")
278290
if cpu < 0.90 * real:
279-
out.write(" warning: test above used only %d%% CPU, "
280-
"result may be flawed!\n" % (100.0 * cpu / real))
291+
out.write(" warning: test above used only "
292+
f"{cpu / real:%} CPU, "
293+
"result may be flawed!\n")
281294

282295
def run_one_test(name, size, open_func, test_func, *args):
283296
mode = test_func.file_open_mode
@@ -308,22 +321,15 @@ def run_test_family(tests, mode_filter, files, open_func, *make_args):
308321
"large": 2,
309322
}
310323

311-
print("Python %s" % sys.version)
312-
if sys.version_info < (3, 3):
313-
if sys.maxunicode > 0xffff:
314-
text = "UCS-4 (wide build)"
315-
else:
316-
text = "UTF-16 (narrow build)"
317-
else:
318-
text = "PEP 393"
319-
print("Unicode: %s" % text)
324+
print(f"Python {sys.version}")
325+
print("Unicode: PEP 393")
320326
print(platform.platform())
321327
binary_files = list(get_binary_files())
322328
text_files = list(get_text_files())
323329
if "b" in options:
324330
print("Binary unit = one byte")
325331
if "t" in options:
326-
print("Text unit = one character (%s-decoded)" % TEXT_ENCODING)
332+
print(f"Text unit = one character ({TEXT_ENCODING}-decoded)")
327333

328334
# Binary reads
329335
if "b" in options and "r" in options:
@@ -338,6 +344,7 @@ def run_test_family(tests, mode_filter, files, open_func, *make_args):
338344
# Binary writes
339345
if "b" in options and "w" in options:
340346
print("\n** Binary append **\n")
347+
341348
def make_test_source(name, size):
342349
with open(name, "rb") as f:
343350
return f.read()
@@ -347,6 +354,7 @@ def make_test_source(name, size):
347354
# Text writes
348355
if "t" in options and "w" in options:
349356
print("\n** Text append **\n")
357+
350358
def make_test_source(name, size):
351359
with text_open(name, "r") as f:
352360
return f.read()
@@ -356,6 +364,7 @@ def make_test_source(name, size):
356364
# Binary overwrites
357365
if "b" in options and "w" in options:
358366
print("\n** Binary overwrite **\n")
367+
359368
def make_test_source(name, size):
360369
with open(name, "rb") as f:
361370
return f.read()
@@ -365,6 +374,7 @@ def make_test_source(name, size):
365374
# Text overwrites
366375
if "t" in options and "w" in options:
367376
print("\n** Text overwrite **\n")
377+
368378
def make_test_source(name, size):
369379
with text_open(name, "r") as f:
370380
return f.read()
@@ -388,7 +398,7 @@ def prepare_files():
388398
break
389399
else:
390400
raise RuntimeError(
391-
"Couldn't find chunk marker in %s !" % __file__)
401+
f"Couldn't find chunk marker in {__file__} !")
392402
if NEWLINES == "all":
393403
it = itertools.cycle(["\n", "\r", "\r\n"])
394404
else:
@@ -414,6 +424,7 @@ def prepare_files():
414424
f.write(head)
415425
f.write(tail)
416426

427+
417428
def main():
418429
global TEXT_ENCODING, NEWLINES
419430

@@ -433,7 +444,7 @@ def main():
433444
help="run write & modify tests")
434445
parser.add_option("-E", "--encoding",
435446
action="store", dest="encoding", default=None,
436-
help="encoding for text tests (default: %s)" % TEXT_ENCODING)
447+
help=f"encoding for text tests (default: {TEXT_ENCODING})")
437448
parser.add_option("-N", "--newlines",
438449
action="store", dest="newlines", default='lf',
439450
help="line endings for text tests "
@@ -446,7 +457,7 @@ def main():
446457
parser.error("unexpected arguments")
447458
NEWLINES = options.newlines.lower()
448459
if NEWLINES not in ('lf', 'cr', 'crlf', 'all'):
449-
parser.error("invalid 'newlines' option: %r" % NEWLINES)
460+
parser.error(f"invalid 'newlines' option: {NEWLINES!r}")
450461

451462
test_options = ""
452463
if options.read:
@@ -471,6 +482,7 @@ def main():
471482
prepare_files()
472483
run_all_tests(test_options)
473484

485+
474486
if __name__ == "__main__":
475487
main()
476488

0 commit comments

Comments
 (0)