Skip to content

Commit e964556

Browse files
committed
fix: replace TextIOWrapper with StringIO in CaptureStream to avoid uninitialized object error
CaptureStream inherited from TextIOWrapper without calling super().__init__(), leaving the internal C struct (self->ok) at 0. When mypy (or other tools) interacted with the stream, Python's C-level CHECK_INITIALIZED raised 'I/O operation on uninitialized object'. Changed to inherit from StringIO which is a pure Python stream that doesn't require C-level initialization, and updated get_hidden_stdout/get_hidden_stderr to use getvalue() instead of the custom .contents attribute.
1 parent 0a9d363 commit e964556

1 file changed

Lines changed: 7 additions & 14 deletions

File tree

prospector/tools/utils.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
import sys
2-
from io import TextIOWrapper
2+
from io import StringIO
33
from typing import Optional
44

55

6-
class CaptureStream(TextIOWrapper):
6+
class CaptureStream(StringIO):
77
def __init__(self, tty: bool) -> None:
8-
self.contents = ""
8+
super().__init__()
99
self._tty = tty
1010

11-
def write(self, text: str, /) -> int:
12-
self.contents += text
13-
return len(text)
11+
def isatty(self) -> bool:
12+
return self._tty
1413

1514
def close(self) -> None:
1615
pass
1716

18-
def flush(self) -> None:
19-
pass
20-
21-
def isatty(self) -> bool:
22-
return self._tty
23-
2417

2518
class CaptureOutput:
2619
_prev_streams = None
@@ -47,10 +40,10 @@ def __enter__(self) -> "CaptureOutput":
4740
return self
4841

4942
def get_hidden_stdout(self) -> str:
50-
return "" if self.stdout is None else self.stdout.contents
43+
return "" if self.stdout is None else self.stdout.getvalue()
5144

5245
def get_hidden_stderr(self) -> str:
53-
return "" if self.stderr is None else self.stderr.contents
46+
return "" if self.stderr is None else self.stderr.getvalue()
5447

5548
def __exit__(self, exc_type: type, exc_val: Exception, exc_tb: type) -> None:
5649
if self.hide:

0 commit comments

Comments
 (0)