From b30986e693c4d266b476a1f1a6eeb4aa8b3db738 Mon Sep 17 00:00:00 2001 From: Marcin Date: Mon, 27 Jan 2025 16:38:43 +0000 Subject: [PATCH 1/2] update to use f-strings --- Lib/test/libregrtest/cmdline.py | 6 +++--- Lib/test/libregrtest/logger.py | 4 ++-- Lib/test/libregrtest/main.py | 4 ++-- Lib/test/libregrtest/refleak.py | 3 +-- Lib/test/libregrtest/run_workers.py | 10 ++++------ Lib/test/libregrtest/utils.py | 19 +++++++++---------- 6 files changed, 21 insertions(+), 25 deletions(-) diff --git a/Lib/test/libregrtest/cmdline.py b/Lib/test/libregrtest/cmdline.py index bf9a71efbdbff9..5ef5af6fcb8c80 100644 --- a/Lib/test/libregrtest/cmdline.py +++ b/Lib/test/libregrtest/cmdline.py @@ -402,8 +402,8 @@ def _parse_args(args, **kwargs): ns = Namespace() for k, v in kwargs.items(): if not hasattr(ns, k): - raise TypeError('%r is an invalid keyword argument ' - 'for this function' % k) + raise TypeError(f'{k!r} is an invalid keyword argument ' + 'for this function') setattr(ns, k, v) parser = _create_parser() @@ -412,7 +412,7 @@ def _parse_args(args, **kwargs): ns.args = parser.parse_known_args(args=args, namespace=ns)[1] for arg in ns.args: if arg.startswith('-'): - parser.error("unrecognized arguments: %s" % arg) + parser.error(f"unrecognized arguments: {arg}") if ns.timeout is not None: # Support "--timeout=" (no value) so Makefile.pre.pre TESTTIMEOUT diff --git a/Lib/test/libregrtest/logger.py b/Lib/test/libregrtest/logger.py index fa1d4d575c8fd4..62c7c73a0fddf1 100644 --- a/Lib/test/libregrtest/logger.py +++ b/Lib/test/libregrtest/logger.py @@ -33,7 +33,7 @@ def log(self, line: str = '') -> None: mins, secs = divmod(int(log_time), 60) hours, mins = divmod(mins, 60) - formatted_log_time = "%d:%02d:%02d" % (hours, mins, secs) + formatted_log_time = f"{hours}:{mins:02d}:{secs:02d} " line = f"{formatted_log_time} {line}" if empty: @@ -68,7 +68,7 @@ def set_tests(self, runtests: RunTests) -> None: self.test_count_text = '' self.test_count_width = 3 else: - self.test_count_text = '/{}'.format(len(runtests.tests)) + self.test_count_text = f"/{len(runtests.tests)}" self.test_count_width = len(self.test_count_text) - 1 def start_load_tracker(self) -> None: diff --git a/Lib/test/libregrtest/main.py b/Lib/test/libregrtest/main.py index dcbcc6790c68d8..deacce6806b29b 100644 --- a/Lib/test/libregrtest/main.py +++ b/Lib/test/libregrtest/main.py @@ -457,7 +457,7 @@ def finalize_tests(self, coverage: trace.CoverageResults | None) -> None: ignore_missing_files=True) if self.want_run_leaks: - os.system("leaks %d" % os.getpid()) + os.system(f"leaks {os.getpid()}") if self.junit_filename: self.results.write_junit(self.junit_filename) @@ -473,7 +473,7 @@ def display_summary(self) -> None: # Total duration print() - print("Total duration: %s" % format_duration(duration)) + print(f"Total duration: {format_duration(duration)}") self.results.display_summary(self.first_runtests, filtered) diff --git a/Lib/test/libregrtest/refleak.py b/Lib/test/libregrtest/refleak.py index d0d1c8cdc9a11b..acfcf3dd6a4166 100644 --- a/Lib/test/libregrtest/refleak.py +++ b/Lib/test/libregrtest/refleak.py @@ -209,8 +209,7 @@ def check_fd_deltas(deltas): failing = checker(deltas) suspicious = any(deltas) if failing or suspicious: - msg = '%s leaked %s %s, sum=%s' % ( - test_name, deltas, item_name, sum(deltas)) + msg = f'{test_name} leaked {deltas} {item_name}, sum={sum(deltas)}' print(msg, end='', file=sys.stderr) if failing: print(file=sys.stderr, flush=True) diff --git a/Lib/test/libregrtest/run_workers.py b/Lib/test/libregrtest/run_workers.py index 424085a0050eb5..71b39fb40b64f6 100644 --- a/Lib/test/libregrtest/run_workers.py +++ b/Lib/test/libregrtest/run_workers.py @@ -134,7 +134,7 @@ def __repr__(self) -> str: dt = time.monotonic() - self.start_time info.extend((f'pid={popen.pid}', f'time={format_duration(dt)}')) - return '<%s>' % ' '.join(info) + return '<{}>'.format(' '.join(info)) def _kill(self) -> None: popen = self._popen @@ -510,9 +510,7 @@ def start_workers(self) -> None: msg = (f"Run {tests} in parallel using " f"{nworkers} worker {processes}") if self.timeout and self.worker_timeout is not None: - msg += (" (timeout: %s, worker timeout: %s)" - % (format_duration(self.timeout), - format_duration(self.worker_timeout))) + msg += (f" (timeout: {format_duration(self.timeout)}, worker timeout: {format_duration(self.worker_timeout)})") self.log(msg) for worker in self.workers: worker.start() @@ -560,9 +558,9 @@ def display_result(self, mp_result: MultiprocessResult) -> None: text = str(result) if mp_result.err_msg: # WORKER_BUG - text += ' (%s)' % mp_result.err_msg + text += f' ({mp_result.err_msg})' elif (result.duration and result.duration >= PROGRESS_MIN_TIME and not pgo): - text += ' (%s)' % format_duration(result.duration) + text += f' ({format_duration(result.duration)})' if not pgo: running = get_running(self.workers) if running: diff --git a/Lib/test/libregrtest/utils.py b/Lib/test/libregrtest/utils.py index 3eff9e753b6d84..9b61b3e2678538 100644 --- a/Lib/test/libregrtest/utils.py +++ b/Lib/test/libregrtest/utils.py @@ -66,18 +66,18 @@ def format_duration(seconds: float) -> str: parts = [] if hours: - parts.append('%s hour' % hours) + parts.append(f'{hours} hour') if minutes: - parts.append('%s min' % minutes) + parts.append(f'{minutes} min') if seconds: if parts: # 2 min 1 sec - parts.append('%s sec' % seconds) + parts.append(f'{seconds} sec') else: # 1.0 sec parts.append('%.1f sec' % (seconds + ms / 1000)) if not parts: - return '%s ms' % ms + return f'{ms} ms' parts = parts[:2] return ' '.join(parts) @@ -650,7 +650,7 @@ def display_header(use_resources: tuple[str, ...], # Print basic platform information print("==", platform.python_implementation(), *sys.version.split()) print("==", platform.platform(aliased=True), - "%s-endian" % sys.byteorder) + f"{sys.byteorder}-endian") print("== Python build:", ' '.join(get_build_info())) print("== cwd:", os.getcwd()) @@ -661,8 +661,7 @@ def display_header(use_resources: tuple[str, ...], if process_cpu_count and process_cpu_count != cpu_count: cpu_count = f"{process_cpu_count} (process) / {cpu_count} (system)" print("== CPU count:", cpu_count) - print("== encodings: locale=%s FS=%s" - % (locale.getencoding(), sys.getfilesystemencoding())) + print(f"== encodings: locale={locale.getencoding()} FS={sys.getfilesystemencoding()}") if use_resources: text = format_resources(use_resources) @@ -728,13 +727,13 @@ def cleanup_temp_dir(tmp_dir: StrPath) -> None: import glob path = os.path.join(glob.escape(tmp_dir), TMP_PREFIX + '*') - print("Cleanup %s directory" % tmp_dir) + print(f"Cleanup {tmp_dir} directory") for name in glob.glob(path): if os.path.isdir(name): - print("Remove directory: %s" % name) + print(f"Remove directory: {name}") os_helper.rmtree(name) else: - print("Remove file: %s" % name) + print(f"Remove file: {name}") os_helper.unlink(name) From 81242bd105d3c41c737bf5f0f665798e3da54382 Mon Sep 17 00:00:00 2001 From: Marcin Date: Mon, 27 Jan 2025 16:44:19 +0000 Subject: [PATCH 2/2] blurb --- .../next/Tests/2025-01-27-16-44-00.gh-issue-129370.wI-jb4.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Tests/2025-01-27-16-44-00.gh-issue-129370.wI-jb4.rst diff --git a/Misc/NEWS.d/next/Tests/2025-01-27-16-44-00.gh-issue-129370.wI-jb4.rst b/Misc/NEWS.d/next/Tests/2025-01-27-16-44-00.gh-issue-129370.wI-jb4.rst new file mode 100644 index 00000000000000..76bed686a63bb6 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2025-01-27-16-44-00.gh-issue-129370.wI-jb4.rst @@ -0,0 +1 @@ +Update libregrtest to use f-strings