From 7d02313a43068432e4740939931fb65aaf3f2c50 Mon Sep 17 00:00:00 2001 From: Cody Maloney Date: Mon, 9 Dec 2024 16:54:20 -0800 Subject: [PATCH 1/3] gh-127747: Use repr to normalize in strace_helper The strace_helper code has a _make_error function to simplify making StraceResult objects in error cases. That takes a details parameter which is either a caught OSError or `bytes`. If it's bytes, _make_error would implicitly coerce that to a str inside of a f-string, resulting in a BytesWarning. It's useful to see if it's an OSError or bytes when debugging, resolve by changing to format with repr(). This is an error message on an internal helper. --- Lib/test/support/strace_helper.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/support/strace_helper.py b/Lib/test/support/strace_helper.py index eab16ea3e2889f..b31b49a654bdab 100644 --- a/Lib/test/support/strace_helper.py +++ b/Lib/test/support/strace_helper.py @@ -104,7 +104,8 @@ def _make_error(reason, details): return StraceResult( strace_returncode=-1, python_returncode=-1, - event_bytes=f"error({reason},details={details}) = -1".encode('utf-8'), + event_bytes= \ + f"error({reason},details={details!r}) = -1".encode('utf-8'), stdout=res.out if res else b"", stderr=res.err if res else b"") From 9bf31a628ce5c53f3957456aa6e753a681a85083 Mon Sep 17 00:00:00 2001 From: Cody Maloney Date: Wed, 11 Dec 2024 18:44:11 -0800 Subject: [PATCH 2/3] gh-127747: Fix w/o strace and with -Werror A non-zero exit code occurs if the strace binary isn't found, and no events will be parsed in that case (there is no output). Handle that case by checking exit code before checking for events. Still asserting around events rather than returning false, so that hopefully if there's some change to `strace` that breaks the parsing, will see that as a test failure rather than silently loosing strace tests because they are auto-disabled. --- Lib/test/support/strace_helper.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Lib/test/support/strace_helper.py b/Lib/test/support/strace_helper.py index b31b49a654bdab..dff7fc48cf0eb8 100644 --- a/Lib/test/support/strace_helper.py +++ b/Lib/test/support/strace_helper.py @@ -180,9 +180,10 @@ def get_syscalls(code, strace_flags, prelude="", cleanup="", @cache def _can_strace(): res = strace_python("import sys; sys.exit(0)", [], check=False) - assert res.events(), "Should have parsed multiple calls" - - return res.strace_returncode == 0 and res.python_returncode == 0 + if res.strace_returncode == 0 and res.python_returncode == 0: + assert res.events(), "Should have parsed multiple calls" + return True + return False def requires_strace(): From e83dbfbe5649e8642f86f7fc5616f739ae659c03 Mon Sep 17 00:00:00 2001 From: Cody Maloney Date: Thu, 12 Dec 2024 13:45:20 -0800 Subject: [PATCH 3/3] Review fixes --- Lib/test/support/strace_helper.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/test/support/strace_helper.py b/Lib/test/support/strace_helper.py index dff7fc48cf0eb8..798d6c6886962f 100644 --- a/Lib/test/support/strace_helper.py +++ b/Lib/test/support/strace_helper.py @@ -104,8 +104,7 @@ def _make_error(reason, details): return StraceResult( strace_returncode=-1, python_returncode=-1, - event_bytes= \ - f"error({reason},details={details!r}) = -1".encode('utf-8'), + event_bytes= f"error({reason},details={details!r}) = -1".encode('utf-8'), stdout=res.out if res else b"", stderr=res.err if res else b"")