← mined_oracle

rich_2055

failed WRONG_FIX UNSUBMITTED wrong_fix_unsubmitted(None) · None tool calls · 0 s · Textualize/rich

Task input

(not found in data/tasks.jsonl)

Tool calls (0)

#ToolArgumentsResult
No trace captured.

Patch

--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
 The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
+## [12.0.1] - 2022-03-14
+
+### Fixed
+
+- Fix capturing stdout on legacy Windows https://github.com/Textualize/rich/pull/2055
+
 ## [12.0.0] - 2022-03-10
 
 ### Added
--- a/rich/_win32_console.py
+++ b/rich/_win32_console.py
@@ -302,40 +302,6 @@ def SetConsoleTitle(title: str) -> bool:
     return bool(_SetConsoleTitle(title))
 
 
-_WriteConsole = windll.kernel32.WriteConsoleW
-_WriteConsole.argtypes = [
-    wintypes.HANDLE,
-    wintypes.LPWSTR,
-    wintypes.DWORD,
-    wintypes.LPDWORD,
-    wintypes.LPVOID,
-]
-_WriteConsole.restype = wintypes.BOOL
-
-
-def WriteConsole(std_handle: wintypes.HANDLE, text: str) -> bool:
-    """Write a string of text to the console, starting at the current cursor position
-
-    Args:
-        std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
-        text (str): The text to write.
-
-    Returns:
-        bool: True if the function succeeds, otherwise False.
-    """
-    buffer = wintypes.LPWSTR(text)
-    num_chars_written = wintypes.LPDWORD()
-    return bool(
-        _WriteConsole(
-            std_handle,
-            buffer,
-            wintypes.DWORD(len(text)),
-            num_chars_written,
-            wintypes.LPVOID(None),
-        )
-    )
-
-
 class LegacyWindowsTerm:
     """This class allows interaction with the legacy Windows Console API. It should only be used in the context
     of environments where virtual terminal processing is not available. However, if it is used in a Windows environment,
@@ -367,7 +333,7 @@ class LegacyWindowsTerm:
         15,  # bright white
     ]
 
-    def __init__(self) -> None:
+    def __init__(self, file: IO[str]) -> None:
         handle = GetStdHandle(STDOUT)
         self._handle = handle
         default_text = GetConsoleScreenBufferInfo(handle).wAttributes
@@ -377,6 +343,10 @@ class LegacyWindowsTerm:
         self._default_back = (default_text >> 4) & 7
         self._default_attrs = self._default_fore | (self._default_back << 4)
 
+        self._file = file
+        self.write = file.write
+        self.flush = file.flush
+
     @property
     def cursor_position(self) -> WindowsCoordinates:
         """Returns the current position of the cursor (0-based)
@@ -405,7 +375,8 @@ class LegacyWindowsTerm:
         Args:
             text (str): The text to write to the console
         """
-        WriteConsole(self._handle, text)
+        self.write(text)
+        self.flush()
 
     def write_styled(self, text: str, style: Style) -> None:
         """Write styled text to the terminal.
@@ -576,7 +547,7 @@ if __name__ == "__main__":
 
     console = Console()
 
-    term = LegacyWindowsTerm()
+    term = LegacyWindowsTerm(sys.stdout)
     term.set_title("Win32 Console Examples")
 
     style = Style(color="black", bgcolor="red")
--- a/rich/console.py
+++ b/rich/console.py
@@ -1932,7 +1932,10 @@ class Console:
                             from rich._win32_console import LegacyWindowsTerm
                             from rich._windows_renderer import legacy_windows_render
 
-                            legacy_windows_render(self._buffer[:], LegacyWindowsTerm())
+                            with open(file_no, "w") as output_file:
+                                legacy_windows_render(
+                                    self._buffer[:], LegacyWindowsTerm(output_file)
+                                )
 
                         output_capture_enabled = bool(self._buffer_index)
                         if not legacy_windows_stdout or output_capture_enabled:

Test output

show
no tests ran in 0.04s