| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  |  | :mod:`pty` --- Pseudo-terminal utilities
 | 
					
						
							|  |  |  |  | ========================================
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | .. module:: pty
 | 
					
						
							| 
									
										
										
										
											2021-08-13 12:57:07 +02:00
										 |  |  |  |    :platform: Unix
 | 
					
						
							|  |  |  |  |    :synopsis: Pseudo-Terminal Handling for Unix.
 | 
					
						
							| 
									
										
										
										
											2016-06-11 15:02:54 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  |  | .. moduleauthor:: Steen Lumholt
 | 
					
						
							|  |  |  |  | .. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-11 15:02:54 -04:00
										 |  |  |  | **Source code:** :source:`Lib/pty.py`
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | --------------
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | The :mod:`pty` module defines operations for handling the pseudo-terminal
 | 
					
						
							|  |  |  |  | concept: starting another process and being able to write to and read from its
 | 
					
						
							|  |  |  |  | controlling terminal programmatically.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-13 12:57:07 +02:00
										 |  |  |  | Pseudo-terminal handling is highly platform dependent. This code is mainly
 | 
					
						
							|  |  |  |  | tested on Linux, FreeBSD, and macOS (it is supposed to work on other POSIX
 | 
					
						
							|  |  |  |  | platforms but it's not been thoroughly tested).
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | The :mod:`pty` module defines the following functions:
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | .. function:: fork()
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |    Fork. Connect the child's controlling terminal to a pseudo-terminal. Return
 | 
					
						
							|  |  |  |  |    value is ``(pid, fd)``. Note that the child  gets *pid* 0, and the *fd* is
 | 
					
						
							|  |  |  |  |    *invalid*. The parent's return value is the *pid* of the child, and *fd* is a
 | 
					
						
							|  |  |  |  |    file descriptor connected to the child's controlling terminal (and also to the
 | 
					
						
							|  |  |  |  |    child's standard input and output).
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | .. function:: openpty()
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |    Open a new pseudo-terminal pair, using :func:`os.openpty` if possible, or
 | 
					
						
							| 
									
										
											  
											
												Merged revisions 75149,75260-75263,75265-75267,75292,75300,75376,75405,75429-75433,75437,75445,75501,75551,75572,75589-75591,75657,75742,75868,75952-75957,76057,76105,76139,76143,76162,76223 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r75149 | gregory.p.smith | 2009-09-29 16:56:31 -0500 (Tue, 29 Sep 2009) | 3 lines
  Mention issue6972 in extractall docs about overwriting things outside of
  the supplied path.
........
  r75260 | andrew.kuchling | 2009-10-05 16:24:20 -0500 (Mon, 05 Oct 2009) | 1 line
  Wording fix
........
  r75261 | andrew.kuchling | 2009-10-05 16:24:35 -0500 (Mon, 05 Oct 2009) | 1 line
  Fix narkup
........
  r75262 | andrew.kuchling | 2009-10-05 16:25:03 -0500 (Mon, 05 Oct 2009) | 1 line
  Document 'skip' parameter to constructor
........
  r75263 | andrew.kuchling | 2009-10-05 16:25:35 -0500 (Mon, 05 Oct 2009) | 1 line
  Note side benefit of socket.create_connection()
........
  r75265 | andrew.kuchling | 2009-10-05 17:31:11 -0500 (Mon, 05 Oct 2009) | 1 line
  Reword sentence
........
  r75266 | andrew.kuchling | 2009-10-05 17:32:48 -0500 (Mon, 05 Oct 2009) | 1 line
  Use standard comma punctuation; reword some sentences in the docs
........
  r75267 | andrew.kuchling | 2009-10-05 17:42:56 -0500 (Mon, 05 Oct 2009) | 1 line
  Backport r73983: Document the thousands separator.
........
  r75292 | benjamin.peterson | 2009-10-08 22:11:36 -0500 (Thu, 08 Oct 2009) | 1 line
  death to old CVS keyword
........
  r75300 | benjamin.peterson | 2009-10-09 16:48:14 -0500 (Fri, 09 Oct 2009) | 1 line
  fix some coding style
........
  r75376 | benjamin.peterson | 2009-10-11 20:26:07 -0500 (Sun, 11 Oct 2009) | 1 line
  platform we don't care about
........
  r75405 | neil.schemenauer | 2009-10-14 12:17:14 -0500 (Wed, 14 Oct 2009) | 4 lines
  Issue #1754094: Improve the stack depth calculation in the compiler.
  There should be no other effect than a small decrease in memory use.
  Patch by Christopher Tur Lesniewski-Laas.
........
  r75429 | benjamin.peterson | 2009-10-14 20:47:28 -0500 (Wed, 14 Oct 2009) | 1 line
  pep8ify if blocks
........
  r75430 | benjamin.peterson | 2009-10-14 20:49:37 -0500 (Wed, 14 Oct 2009) | 1 line
  use floor division and add a test that exercises the tabsize codepath
........
  r75431 | benjamin.peterson | 2009-10-14 20:56:25 -0500 (Wed, 14 Oct 2009) | 1 line
  change test to what I intended
........
  r75432 | benjamin.peterson | 2009-10-14 22:05:39 -0500 (Wed, 14 Oct 2009) | 1 line
  some cleanups
........
  r75433 | benjamin.peterson | 2009-10-14 22:06:55 -0500 (Wed, 14 Oct 2009) | 1 line
  make inspect.isabstract() always return a boolean; add a test for it, too #7069
........
  r75437 | benjamin.peterson | 2009-10-15 10:44:46 -0500 (Thu, 15 Oct 2009) | 1 line
  only clear a module's __dict__ if the module is the only one with a reference to it #7140
........
  r75445 | vinay.sajip | 2009-10-16 09:06:44 -0500 (Fri, 16 Oct 2009) | 1 line
  Issue #7120: logging: Removed import of multiprocessing which is causing crash in GAE.
........
  r75501 | antoine.pitrou | 2009-10-18 13:37:11 -0500 (Sun, 18 Oct 2009) | 3 lines
  Add a comment about unreachable code, and fix a typo
........
  r75551 | benjamin.peterson | 2009-10-19 22:14:10 -0500 (Mon, 19 Oct 2009) | 1 line
  use property api
........
  r75572 | benjamin.peterson | 2009-10-20 16:55:17 -0500 (Tue, 20 Oct 2009) | 1 line
  clarify buffer arg #7178
........
  r75589 | benjamin.peterson | 2009-10-21 21:26:47 -0500 (Wed, 21 Oct 2009) | 1 line
  whitespace
........
  r75590 | benjamin.peterson | 2009-10-21 21:36:47 -0500 (Wed, 21 Oct 2009) | 1 line
  rewrite to be nice to other implementations
........
  r75591 | benjamin.peterson | 2009-10-21 21:50:38 -0500 (Wed, 21 Oct 2009) | 4 lines
  rewrite for style, clarify, and comments
  Also, use the hasattr() like scheme of allowing BaseException exceptions through.
........
  r75657 | antoine.pitrou | 2009-10-24 07:41:27 -0500 (Sat, 24 Oct 2009) | 3 lines
  Fix compilation error in debug mode.
........
  r75742 | benjamin.peterson | 2009-10-26 17:51:16 -0500 (Mon, 26 Oct 2009) | 1 line
  use 'is' instead of id()
........
  r75868 | benjamin.peterson | 2009-10-27 15:59:18 -0500 (Tue, 27 Oct 2009) | 1 line
  test expect base classes
........
  r75952 | georg.brandl | 2009-10-29 15:38:32 -0500 (Thu, 29 Oct 2009) | 1 line
  Use the correct function name in docstring.
........
  r75953 | georg.brandl | 2009-10-29 15:39:50 -0500 (Thu, 29 Oct 2009) | 1 line
  Remove mention of the old -X command line switch.
........
  r75954 | georg.brandl | 2009-10-29 15:53:00 -0500 (Thu, 29 Oct 2009) | 1 line
  Use constants instead of magic integers for test result.  Do not re-run with --verbose3 for environment changing tests.
........
  r75955 | georg.brandl | 2009-10-29 15:54:03 -0500 (Thu, 29 Oct 2009) | 1 line
  Use a single style for all the docstrings in the math module.
........
  r75956 | georg.brandl | 2009-10-29 16:16:34 -0500 (Thu, 29 Oct 2009) | 1 line
  I do not think the "railroad" program mentioned is still available.
........
  r75957 | georg.brandl | 2009-10-29 16:44:56 -0500 (Thu, 29 Oct 2009) | 1 line
  Fix constant name.
........
  r76057 | benjamin.peterson | 2009-11-02 09:06:45 -0600 (Mon, 02 Nov 2009) | 1 line
  prevent a rather unlikely segfault
........
  r76105 | georg.brandl | 2009-11-04 01:38:12 -0600 (Wed, 04 Nov 2009) | 1 line
  #7259: show correct equivalent for operator.i* operations in docstring; fix minor issues in operator docs.
........
  r76139 | benjamin.peterson | 2009-11-06 19:04:38 -0600 (Fri, 06 Nov 2009) | 1 line
  spelling
........
  r76143 | georg.brandl | 2009-11-07 02:26:07 -0600 (Sat, 07 Nov 2009) | 1 line
  #7271: fix typo.
........
  r76162 | benjamin.peterson | 2009-11-08 22:10:53 -0600 (Sun, 08 Nov 2009) | 1 line
  discuss how to use -p
........
  r76223 | georg.brandl | 2009-11-12 02:29:46 -0600 (Thu, 12 Nov 2009) | 1 line
  Give the profile module a module directive.
........
											
										 
											2009-11-13 02:25:08 +00:00
										 |  |  |  |    emulation code for generic Unix systems. Return a pair of file descriptors
 | 
					
						
							|  |  |  |  |    ``(master, slave)``, for the master and the slave end, respectively.
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | .. function:: spawn(argv[, master_read[, stdin_read]])
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |    Spawn a process, and connect its controlling terminal with the current
 | 
					
						
							|  |  |  |  |    process's standard io. This is often used to baffle programs which insist on
 | 
					
						
							| 
									
										
										
										
											2019-05-20 08:06:16 -07:00
										 |  |  |  |    reading from the controlling terminal. It is expected that the process
 | 
					
						
							|  |  |  |  |    spawned behind the pty will eventually terminate, and when it does *spawn*
 | 
					
						
							|  |  |  |  |    will return.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-13 12:57:07 +02:00
										 |  |  |  |    A loop copies STDIN of the current process to the child and data received
 | 
					
						
							|  |  |  |  |    from the child to STDOUT of the current process. It is not signaled to the
 | 
					
						
							|  |  |  |  |    child if STDIN of the current process closes down.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-20 08:06:16 -07:00
										 |  |  |  |    The functions *master_read* and *stdin_read* are passed a file descriptor
 | 
					
						
							|  |  |  |  |    which they should read from, and they should always return a byte string. In
 | 
					
						
							|  |  |  |  |    order to force spawn to return before the child process exits an
 | 
					
						
							| 
									
										
										
										
											2021-08-11 15:21:46 -07:00
										 |  |  |  |    empty byte array should be returned to signal end of file.
 | 
					
						
							| 
									
										
										
										
											2019-05-20 08:06:16 -07:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |    The default implementation for both functions will read and return up to 1024
 | 
					
						
							|  |  |  |  |    bytes each time the function is called. The *master_read* callback is passed
 | 
					
						
							|  |  |  |  |    the pseudoterminal’s master file descriptor to read output from the child
 | 
					
						
							|  |  |  |  |    process, and *stdin_read* is passed file descriptor 0, to read from the
 | 
					
						
							|  |  |  |  |    parent process's standard input.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |    Returning an empty byte string from either callback is interpreted as an
 | 
					
						
							|  |  |  |  |    end-of-file (EOF) condition, and that callback will not be called after
 | 
					
						
							|  |  |  |  |    that. If *stdin_read* signals EOF the controlling terminal can no longer
 | 
					
						
							|  |  |  |  |    communicate with the parent process OR the child process. Unless the child
 | 
					
						
							|  |  |  |  |    process will quit without any input, *spawn* will then loop forever. If
 | 
					
						
							|  |  |  |  |    *master_read* signals EOF the same behavior results (on linux at least).
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-01 18:49:29 +02:00
										 |  |  |  |    Return the exit status value from :func:`os.waitpid` on the child process.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |    :func:`waitstatus_to_exitcode` can be used to convert the exit status into
 | 
					
						
							|  |  |  |  |    an exit code.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-04 16:15:00 -08:00
										 |  |  |  |    .. audit-event:: pty.spawn argv pty.spawn
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-29 12:41:03 -07:00
										 |  |  |  |    .. versionchanged:: 3.4
 | 
					
						
							|  |  |  |  |       :func:`spawn` now returns the status value from :func:`os.waitpid`
 | 
					
						
							|  |  |  |  |       on the child process.
 | 
					
						
							| 
									
										
										
										
											2010-12-30 17:22:33 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | Example
 | 
					
						
							|  |  |  |  | -------
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | .. sectionauthor:: Steen Lumholt
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | The following program acts like the Unix command :manpage:`script(1)`, using a
 | 
					
						
							|  |  |  |  | pseudo-terminal to record all input and output of a terminal session in a
 | 
					
						
							|  |  |  |  | "typescript". ::
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-12 17:25:06 +03:00
										 |  |  |  |     import argparse
 | 
					
						
							|  |  |  |  |     import os
 | 
					
						
							|  |  |  |  |     import pty
 | 
					
						
							|  |  |  |  |     import sys
 | 
					
						
							|  |  |  |  |     import time
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     parser = argparse.ArgumentParser()
 | 
					
						
							|  |  |  |  |     parser.add_argument('-a', dest='append', action='store_true')
 | 
					
						
							|  |  |  |  |     parser.add_argument('-p', dest='use_python', action='store_true')
 | 
					
						
							|  |  |  |  |     parser.add_argument('filename', nargs='?', default='typescript')
 | 
					
						
							|  |  |  |  |     options = parser.parse_args()
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     shell = sys.executable if options.use_python else os.environ.get('SHELL', 'sh')
 | 
					
						
							|  |  |  |  |     filename = options.filename
 | 
					
						
							|  |  |  |  |     mode = 'ab' if options.append else 'wb'
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     with open(filename, mode) as script:
 | 
					
						
							|  |  |  |  |         def read(fd):
 | 
					
						
							|  |  |  |  |             data = os.read(fd, 1024)
 | 
					
						
							|  |  |  |  |             script.write(data)
 | 
					
						
							|  |  |  |  |             return data
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         print('Script started, file is', filename)
 | 
					
						
							|  |  |  |  |         script.write(('Script started on %s\n' % time.asctime()).encode())
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         pty.spawn(shell, read)
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         script.write(('Script done on %s\n' % time.asctime()).encode())
 | 
					
						
							|  |  |  |  |         print('Script done, file is', filename)
 |