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

Handle SIGPIPE/BrokenPipeError in pytest's CLI #7185

Merged
merged 1 commit into from
May 8, 2020

Conversation

bluetech
Copy link
Member

@bluetech bluetech commented May 7, 2020

Running pytest | head -1 and similar causes an annoying error to be
printed to stderr:

Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
BrokenPipeError: [Errno 32] Broken pipe

(or possibly even a propagating exception in older/other Python versions).

The standard UNIX behavior is to handle the SIGPIPE silently. To
recommended method to do this in Python is described here:
https://docs.python.org/3/library/signal.html#note-on-sigpipe

It is not appropriate to apply this recommendation to pytest.main(),
which is used programmatically for in-process runs. Hence, change
pytest's entrypoint to a new pytest.console_main() function, to be
used exclusively by pytest's CLI, and add the SIGPIPE code there.

Fixes #4375.

Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

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

Thanks @bluetech!

Besides my comments we should also add a CHANGELOG entry. Is this a bug fix or an improvement btw? 🤔

setup.cfg Show resolved Hide resolved
src/_pytest/config/__init__.py Show resolved Hide resolved
@RonnyPfannschmidt
Copy link
Member

It's a improvement

Running `pytest | head -1` and similar causes an annoying error to be
printed to stderr:

    Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
    BrokenPipeError: [Errno 32] Broken pipe

(or possibly even a propagating exception in older/other Python versions).

The standard UNIX behavior is to handle the EPIPE silently. To
recommended method to do this in Python is described here:
https://docs.python.org/3/library/signal.html#note-on-sigpipe

It is not appropriate to apply this recommendation to `pytest.main()`,
which is used programmatically for in-process runs. Hence, change
pytest's entrypoint to a new `pytest.console_main()` function, to be
used exclusively by pytest's CLI, and add the SIGPIPE code there.

Fixes pytest-dev#4375.
@bluetech
Copy link
Member Author

bluetech commented May 8, 2020

  • Added a test (it seems Windows behaves differently in a way that can't be handled really, so I skipped it there -- I don't have a Windows to test with unfortunately).
  • Added changelog.

Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

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

Thanks!

@bluetech
Copy link
Member Author

bluetech commented May 8, 2020

I'm not sure why the BrokenPipeError isn't covered in CI -- the tests passes, and it is covered when I run it locally, and the test fails without the changes. Maybe something different about the environment.

@bluetech bluetech merged commit 857b5c9 into pytest-dev:master May 8, 2020
@bluetech bluetech deleted the sigpipe branch June 17, 2020 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BrokenPipeError via terminalwriter
3 participants