Bug: piping output of commands results in `BrokenPipeError`
- [x] I have searched the issue tracker and believe that this is not a duplicate.
Make sure you run commands with -v flag before pasting the output.
Steps to reproduce
Piping the output of a command to something like grep causes a BrokenPipeError. The following command should be able to reproduce the issue (might need to run it a few times since it's somewhat random).
pdm venv create --name test-1
pdm venv create --name test-2
pdm venv create --name test-3
pdm venv list | grep -q "test"
The traceback:
Traceback (most recent call last):
File "/home/guacs/.local/bin/pdm", line 8, in <module>
sys.exit(main())
^^^^^^
File "/home/guacs/.local/share/pdm/venv/lib/python3.11/site-packages/pdm/core.py", line 290, in main
return Core().main(args or sys.argv[1:])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/guacs/.local/share/pdm/venv/lib/python3.11/site-packages/pdm/core.py", line 208, in main
raise cast(Exception, err).with_traceback(traceback) from None
File "/home/guacs/.local/share/pdm/venv/lib/python3.11/site-packages/pdm/core.py", line 203, in main
self.handle(project, options)
File "/home/guacs/.local/share/pdm/venv/lib/python3.11/site-packages/pdm/core.py", line 157, in handle
command.handle(project, options)
File "/home/guacs/.local/share/pdm/venv/lib/python3.11/site-packages/pdm/cli/commands/venv/list.py", line 23, in handle
project.core.ui.echo(f"{mark} [success]{ident}[/]: {venv.root}")
File "/home/guacs/.local/share/pdm/venv/lib/python3.11/site-packages/pdm/termui.py", line 187, in echo
console.print(message, **kwargs)
File "/home/guacs/.local/share/pdm/venv/lib/python3.11/site-packages/rich/console.py", line 1672, in print
with self:
File "/home/guacs/.local/share/pdm/venv/lib/python3.11/site-packages/rich/console.py", line 864, in __exit__
self._exit_buffer()
File "/home/guacs/.local/share/pdm/venv/lib/python3.11/site-packages/rich/console.py", line 822, in _exit_buffer
self._check_buffer()
File "/home/guacs/.local/share/pdm/venv/lib/python3.11/site-packages/rich/console.py", line 2064, in _check_buffer
self.file.flush()
BrokenPipeError: [Errno 32] Broken pipe
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
BrokenPipeError: [Errno 32] Broken pipe
Actual behavior
A BrokenPipeError is raised.
Expected behavior
No exceptions should be raised the piping should work without any issues.
Environment Information
# Paste the output of `pdm info && pdm info --env` below:
Virtualenv /home/guacs/.local/share/pdm/venvs/litestar-SQGtRF1u-test-python-3-8-pydantic-v1 is reused.
PDM version:
2.9.3
Python Interpreter:
~ (3.12)
Project Root:
~
Local Packages:
{
"implementation_name": "cpython",
"implementation_version": "3.12.0",
"os_name": "posix",
"platform_machine": "x86_64",
"platform_release": "5.15.90.1-microsoft-standard-WSL2",
"platform_system": "Linux",
"platform_version": "#1 SMP Fri Jan 27 02:56:13 UTC 2023",
"python_full_version": "3.12.0",
"platform_python_implementation": "CPython",
"python_version": "3.12",
"sys_platform": "linux"
}
EDIT: updated the reproduction steps
This behavior is documented in the official Python documentation here which also provides a workaround.
It seems not reproducible on my side. Can you apply the workaround you mentioned and check if that works?
I did some testing, and it seems the steps I had described to reproduce it wasn't correct. I'll update it in the main description itself. The issue is due to rich so I think it might not even be a pdm bug. For example, the following will consistently reproduce the issue:
# file: foo.py
from rich import Console
console = Console()
console.print("foo")
console.print("bar")
console.print("baz")
After this, running python foo.py | grep -q "foo" will consistently reproduce the issue. This was reported previously here.