summaryrefslogtreecommitdiff
path: root/devdocs/python~3.12/library%2Fpty.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/python~3.12/library%2Fpty.html')
-rw-r--r--devdocs/python~3.12/library%2Fpty.html44
1 files changed, 44 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Fpty.html b/devdocs/python~3.12/library%2Fpty.html
new file mode 100644
index 00000000..c964e97c
--- /dev/null
+++ b/devdocs/python~3.12/library%2Fpty.html
@@ -0,0 +1,44 @@
+ <span id="pty-pseudo-terminal-utilities"></span><h1>pty — Pseudo-terminal utilities</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/pty.py">Lib/pty.py</a></p> <p>The <a class="reference internal" href="#module-pty" title="pty: Pseudo-Terminal Handling for Unix. (Unix)"><code>pty</code></a> 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.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <p>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).</p> <p>The <a class="reference internal" href="#module-pty" title="pty: Pseudo-Terminal Handling for Unix. (Unix)"><code>pty</code></a> module defines the following functions:</p> <dl class="py function"> <dt class="sig sig-object py" id="pty.fork">
+<code>pty.fork()</code> </dt> <dd>
+<p>Fork. Connect the child’s controlling terminal to a pseudo-terminal. Return value is <code>(pid, fd)</code>. Note that the child gets <em>pid</em> 0, and the <em>fd</em> is <em>invalid</em>. The parent’s return value is the <em>pid</em> of the child, and <em>fd</em> is a file descriptor connected to the child’s controlling terminal (and also to the child’s standard input and output).</p> <div class="admonition warning"> <p class="admonition-title">Warning</p> <p>On macOS the use of this function is unsafe when mixed with using higher-level system APIs, and that includes using <a class="reference internal" href="urllib.request#module-urllib.request" title="urllib.request: Extensible library for opening URLs."><code>urllib.request</code></a>.</p> </div> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="pty.openpty">
+<code>pty.openpty()</code> </dt> <dd>
+<p>Open a new pseudo-terminal pair, using <a class="reference internal" href="os#os.openpty" title="os.openpty"><code>os.openpty()</code></a> if possible, or emulation code for generic Unix systems. Return a pair of file descriptors <code>(master, slave)</code>, for the master and the slave end, respectively.</p> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="pty.spawn">
+<code>pty.spawn(argv[, master_read[, stdin_read]])</code> </dt> <dd>
+<p>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 reading from the controlling terminal. It is expected that the process spawned behind the pty will eventually terminate, and when it does <em>spawn</em> will return.</p> <p>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.</p> <p>The functions <em>master_read</em> and <em>stdin_read</em> 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 empty byte array should be returned to signal end of file.</p> <p>The default implementation for both functions will read and return up to 1024 bytes each time the function is called. The <em>master_read</em> callback is passed the pseudoterminal’s master file descriptor to read output from the child process, and <em>stdin_read</em> is passed file descriptor 0, to read from the parent process’s standard input.</p> <p>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 <em>stdin_read</em> 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, <em>spawn</em> will then loop forever. If <em>master_read</em> signals EOF the same behavior results (on linux at least).</p> <p>Return the exit status value from <a class="reference internal" href="os#os.waitpid" title="os.waitpid"><code>os.waitpid()</code></a> on the child process.</p> <p><a class="reference internal" href="os#os.waitstatus_to_exitcode" title="os.waitstatus_to_exitcode"><code>os.waitstatus_to_exitcode()</code></a> can be used to convert the exit status into an exit code.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>pty.spawn</code> with argument <code>argv</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span><a class="reference internal" href="#pty.spawn" title="pty.spawn"><code>spawn()</code></a> now returns the status value from <a class="reference internal" href="os#os.waitpid" title="os.waitpid"><code>os.waitpid()</code></a> on the child process.</p> </div> </dd>
+</dl> <section id="example"> <h2>Example</h2> <p>The following program acts like the Unix command <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/script(1)">script(1)</a></em>, using a pseudo-terminal to record all input and output of a terminal session in a “typescript”.</p> <pre data-language="python">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)
+</pre> </section> <div class="_attribution">
+ <p class="_attribution-p">
+ &copy; 2001&ndash;2023 Python Software Foundation<br>Licensed under the PSF License.<br>
+ <a href="https://docs.python.org/3.12/library/pty.html" class="_attribution-link">https://docs.python.org/3.12/library/pty.html</a>
+ </p>
+</div>