Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c0264fc

Browse files
authored
gh-127747: Resolve BytesWarning in test.support.strace_helper (#127849)
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. 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.
1 parent 2de048c commit c0264fc

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

Lib/test/support/strace_helper.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def _make_error(reason, details):
104104
return StraceResult(
105105
strace_returncode=-1,
106106
python_returncode=-1,
107-
event_bytes=f"error({reason},details={details}) = -1".encode('utf-8'),
107+
event_bytes= f"error({reason},details={details!r}) = -1".encode('utf-8'),
108108
stdout=res.out if res else b"",
109109
stderr=res.err if res else b"")
110110

@@ -179,9 +179,10 @@ def get_syscalls(code, strace_flags, prelude="", cleanup="",
179179
@cache
180180
def _can_strace():
181181
res = strace_python("import sys; sys.exit(0)", [], check=False)
182-
assert res.events(), "Should have parsed multiple calls"
183-
184-
return res.strace_returncode == 0 and res.python_returncode == 0
182+
if res.strace_returncode == 0 and res.python_returncode == 0:
183+
assert res.events(), "Should have parsed multiple calls"
184+
return True
185+
return False
185186

186187

187188
def requires_strace():

0 commit comments

Comments
 (0)