From 8cfd050f615a4bad699ec55d4cb19b1f1dc4137e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= <mgorny@gentoo.org>
Date: Sat, 9 Nov 2024 16:41:40 +0100
Subject: [PATCH] Override rich.console pipe handler for rich 13.8.0+

Explicitly override `rich.console.Console.on_broken_pipe()` to reraise
the original exception, to bring the behavior of rich 13.8.0+ in line
with older versions.  The new versions instead close output fds and exit
with error instead, which prevents pip's pipe handler from firing.
This is the minimal change needed to make pip's test suite pass after
upgrading vendored rich.

Bug #13006
Bug #13072
---
 src/pip/_internal/utils/logging.py | 9 ++++++++-
 2 files changed, 8 insertions(+), 1 deletion(-)
 create mode 100644 news/13072.trivial.rst

diff --git a/src/pip/_internal/utils/logging.py b/src/pip/_internal/utils/logging.py
index 41f6eb51a..be17add5d 100644
--- a/src/pip/_internal/utils/logging.py
+++ b/src/pip/_internal/utils/logging.py
@@ -137,12 +137,19 @@ class IndentedRenderable:
             yield Segment("\n")
 
 
+class PipConsole(Console):
+    def on_broken_pipe(self) -> None:
+        # Reraise the original exception, rich 13.8.0+ exits by default
+        # instead, preventing our handler from firing.
+        raise BrokenPipeError() from None
+
+
 class RichPipStreamHandler(RichHandler):
     KEYWORDS: ClassVar[Optional[List[str]]] = []
 
     def __init__(self, stream: Optional[TextIO], no_color: bool) -> None:
         super().__init__(
-            console=Console(file=stream, no_color=no_color, soft_wrap=True),
+            console=PipConsole(file=stream, no_color=no_color, soft_wrap=True),
             show_time=False,
             show_level=False,
             show_path=False,
-- 
2.47.0

