diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index 5c3c44249345f3..aec6bc45b6e8fe 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -1155,6 +1155,10 @@ def emit(self, record): self.flush() except RecursionError: # See issue 36272 raise + except BrokenPipeError: + devnull = os.open(os.devnull, os.O_WRONLY) + os.dup2(devnull, sys.stdout.fileno()) + sys.exit(1) except Exception: self.handleError(record) diff --git a/Misc/NEWS.d/next/Library/2025-05-23-09-09-54.gh-issue-134568.BevBCI.rst b/Misc/NEWS.d/next/Library/2025-05-23-09-09-54.gh-issue-134568.BevBCI.rst new file mode 100644 index 00000000000000..65d1f816758444 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-05-23-09-09-54.gh-issue-134568.BevBCI.rst @@ -0,0 +1 @@ +:class:`logging.StreamHandler` now handles :exc:`BrokenPipeError` when the output is cut short by piping to the ``head`` utility.