Skip to content

Commit a68f0a5

Browse files
authored
Support debug prints in mypy daemon (#13769)
Capture stdout in daemon and display it in the client. This makes it possible to use debug prints in the daemon when doing end-to-end testing. They previously only worked in daemon test cases.
1 parent 00ca6bf commit a68f0a5

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

mypy/dmypy/client.py

+4
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,10 @@ def request(
665665
return {"error": str(err)}
666666
# TODO: Other errors, e.g. ValueError, UnicodeError
667667
else:
668+
# Display debugging output written to stdout in the server process for convenience.
669+
stdout = response.get("stdout")
670+
if stdout:
671+
sys.stdout.write(stdout)
668672
return response
669673

670674

mypy/dmypy_server.py

+4
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ def serve(self) -> None:
214214
while True:
215215
with server:
216216
data = receive(server)
217+
debug_stdout = io.StringIO()
218+
sys.stdout = debug_stdout
217219
resp: dict[str, Any] = {}
218220
if "command" not in data:
219221
resp = {"error": "No command found in request"}
@@ -230,8 +232,10 @@ def serve(self) -> None:
230232
tb = traceback.format_exception(*sys.exc_info())
231233
resp = {"error": "Daemon crashed!\n" + "".join(tb)}
232234
resp.update(self._response_metadata())
235+
resp["stdout"] = debug_stdout.getvalue()
233236
server.write(json.dumps(resp).encode("utf8"))
234237
raise
238+
resp["stdout"] = debug_stdout.getvalue()
235239
try:
236240
resp.update(self._response_metadata())
237241
server.write(json.dumps(resp).encode("utf8"))

0 commit comments

Comments
 (0)