@@ -36,9 +36,9 @@ def _wrap_stdin() -> tuple[anyio.AsyncFile[str], bool]:
3636 stdin_fd = os .dup (sys .stdin .fileno ())
3737 except (AttributeError , OSError , UnsupportedOperation ):
3838 # Some tests and embedders replace sys.stdin with fileno-less in-memory
39- # streams. Keep supporting that shape by falling back to the existing
40- # buffer-wrapping behavior .
41- return anyio .wrap_file (TextIOWrapper ( sys .stdin . buffer , encoding = "utf-8" , errors = "replace" ) ), False
39+ # streams. Reusing the caller-provided wrapper avoids closing it when the
40+ # transport exits .
41+ return anyio .wrap_file (sys .stdin ), False
4242
4343 stdin_buffer = os .fdopen (stdin_fd , "rb" , closefd = True )
4444 return anyio .wrap_file (TextIOWrapper (stdin_buffer , encoding = "utf-8" , errors = "replace" )), True
@@ -51,7 +51,7 @@ def _wrap_stdout() -> tuple[anyio.AsyncFile[str], bool]:
5151 except (AttributeError , OSError , UnsupportedOperation ):
5252 # Match the fileno-less stdin fallback for in-memory test streams and
5353 # embedders that provide file-like stdout objects.
54- return anyio .wrap_file (TextIOWrapper ( sys .stdout . buffer , encoding = "utf-8" ) ), False
54+ return anyio .wrap_file (sys .stdout ), False
5555
5656 stdout_buffer = os .fdopen (stdout_fd , "wb" , closefd = True )
5757 return anyio .wrap_file (TextIOWrapper (stdout_buffer , encoding = "utf-8" )), True
0 commit comments