[New-bugs-announce] [issue34243] pty.spawn: inconsistent interface

zerx report at bugs.python.org
Thu Jul 26 19:54:56 EDT 2018


New submission from zerx <olyx32 at gmail.com>:

# spawn(argv, master_read=_read, stdin_read=_read)

With stdin_read function it's possible to customize how one can read from stdin, but it makes little sense without stdout counterpart. For example, you cannot use a wrapped ssl socket to forward a terminal over secure connection, because os.write(STDOUT_FILENO, data) is not suitable for ssl sockets.

The proposal is to add stdout_write argument.

diff --git a/pty.py b/pty.py
index e841f12..ed4a12f 100644
--- a/pty.py
+++ b/pty.py
@@ -126,10 +126,10 @@ def _read(fd):
     """Default read function."""
     return os.read(fd, 1024)
 
-def _copy(master_fd, master_read=_read, stdin_read=_read):
+def _copy(master_fd, master_read=_read, stdin_read=_read, stdout_write=_writen):
     """Parent copy loop.
     Copies
-            pty master -> standard output   (master_read)
+            pty master -> standard output   (master_read, stdout_write)
             standard input -> pty master    (stdin_read)"""
     fds = [master_fd, STDIN_FILENO]
     while True:
@@ -139,7 +139,7 @@ def _copy(master_fd, master_read=_read, stdin_read=_read):
             if not data:  # Reached EOF.
                 fds.remove(master_fd)
             else:
-                os.write(STDOUT_FILENO, data)
+                stdout_write(STDOUT_FILENO, data)
         if STDIN_FILENO in rfds:
             data = stdin_read(STDIN_FILENO)
             if not data:
@@ -147,7 +147,7 @@ def _copy(master_fd, master_read=_read, stdin_read=_read):
             else:
                 _writen(master_fd, data)
 
-def spawn(argv, master_read=_read, stdin_read=_read):
+def spawn(argv, master_read=_read, stdin_read=_read, stdout_write=_writen):
     """Create a spawned process."""
     if type(argv) == type(''):
         argv = (argv,)
@@ -161,7 +161,7 @@ def spawn(argv, master_read=_read, stdin_read=_read):
     except tty.error:    # This is the same as termios.error
         restore = 0
     try:
-        _copy(master_fd, master_read, stdin_read)
+        _copy(master_fd, master_read, stdin_read, stdout_write)
     except OSError:
         if restore:
             tty.tcsetattr(STDIN_FILENO, tty.TCSAFLUSH, mode)

----------
components: Library (Lib)
messages: 322452
nosy: statix
priority: normal
severity: normal
status: open
title: pty.spawn: inconsistent interface
type: enhancement
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34243>
_______________________________________


More information about the New-bugs-announce mailing list