Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-127747: Resolve BytesWarning in test.support.strace_helper #127849

Merged
merged 3 commits into from
Dec 14, 2024

Conversation

cmaloney
Copy link
Contributor

@cmaloney cmaloney commented Dec 12, 2024

Validation Steps:

  1. Update Lib/test/support/strace_helper.py to set _strace_binary to /usr/bin/false (or don't have strace installed)
  2. Run ./python -Werror -I -bb -m test.test_subprocess -vv POSIXProcessTestCase.test_vfork_used_when_expected

Description

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 when the python test is invoked with -b.

It is useful to distinguish between an OSError or bytes when debugging the test, so eliminate the BytesWarning and show both by changing to format with repr().

After fixing the implicing bytes-> str conversion, if /usr/bin/strace is uninstalled/doesn't exist, get a new error running the test:

  File "<source_dir>/cpython/Lib/test/support/strace_helper.py", line 183, in _can_strace
    assert res.events(), "Should have parsed multiple calls"
           ~~~~~~~~~~^^
AssertionError: Should have parsed multiple calls

Resolved by checking the exit code of the subprocess before asserting there should be events. Reason for asserting there are events, rather than returning False from _can_strace, is to try and ensure that if strace runs successfully and produces an output that can't be parsed that is a test error rather than silently disabling all strace tests (See: gh-121381 where a feature working was tested with strace but the strace tests stopped running, leading to this infrastructure in #123413).

skip news: This is a fix to an error message on a python test suite internal 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.
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.
Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Copy link
Contributor

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@hauntsaninja hauntsaninja merged commit c0264fc into python:main Dec 14, 2024
44 checks passed
@cmaloney cmaloney deleted the cmaloney/strace_warning branch December 16, 2024 02:56
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull request Jan 8, 2025
…ython#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip news tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants