blob: 7f4da41e93802dd99ddf8eb2dfaa2dcf3622c1d7 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`pty` --- Pseudo-terminal utilities
2========================================
3
4.. module:: pty
Miss Islington (bot)d4128482021-08-13 04:21:06 -07005 :platform: Unix
6 :synopsis: Pseudo-Terminal Handling for Unix.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007
Georg Brandl116aa622007-08-15 14:28:22 +00008.. moduleauthor:: Steen Lumholt
9.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
10
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040011**Source code:** :source:`Lib/pty.py`
12
13--------------
Georg Brandl116aa622007-08-15 14:28:22 +000014
15The :mod:`pty` module defines operations for handling the pseudo-terminal
16concept: starting another process and being able to write to and read from its
17controlling terminal programmatically.
18
Miss Islington (bot)d4128482021-08-13 04:21:06 -070019Pseudo-terminal handling is highly platform dependent. This code is mainly
20tested on Linux, FreeBSD, and macOS (it is supposed to work on other POSIX
21platforms but it's not been thoroughly tested).
Georg Brandl116aa622007-08-15 14:28:22 +000022
23The :mod:`pty` module defines the following functions:
24
25
26.. function:: fork()
27
28 Fork. Connect the child's controlling terminal to a pseudo-terminal. Return
29 value is ``(pid, fd)``. Note that the child gets *pid* 0, and the *fd* is
30 *invalid*. The parent's return value is the *pid* of the child, and *fd* is a
31 file descriptor connected to the child's controlling terminal (and also to the
32 child's standard input and output).
33
34
35.. function:: openpty()
36
37 Open a new pseudo-terminal pair, using :func:`os.openpty` if possible, or
Benjamin Petersona0dfa822009-11-13 02:25:08 +000038 emulation code for generic Unix systems. Return a pair of file descriptors
39 ``(master, slave)``, for the master and the slave end, respectively.
Georg Brandl116aa622007-08-15 14:28:22 +000040
41
42.. function:: spawn(argv[, master_read[, stdin_read]])
43
44 Spawn a process, and connect its controlling terminal with the current
45 process's standard io. This is often used to baffle programs which insist on
Geoff Shannon522ccef2019-05-20 08:06:16 -070046 reading from the controlling terminal. It is expected that the process
47 spawned behind the pty will eventually terminate, and when it does *spawn*
48 will return.
Georg Brandl116aa622007-08-15 14:28:22 +000049
Miss Islington (bot)d4128482021-08-13 04:21:06 -070050 A loop copies STDIN of the current process to the child and data received
51 from the child to STDOUT of the current process. It is not signaled to the
52 child if STDIN of the current process closes down.
53
Geoff Shannon522ccef2019-05-20 08:06:16 -070054 The functions *master_read* and *stdin_read* are passed a file descriptor
55 which they should read from, and they should always return a byte string. In
56 order to force spawn to return before the child process exits an
Miss Islington (bot)5d444432021-08-12 05:36:04 -070057 empty byte array should be returned to signal end of file.
Geoff Shannon522ccef2019-05-20 08:06:16 -070058
59 The default implementation for both functions will read and return up to 1024
60 bytes each time the function is called. The *master_read* callback is passed
61 the pseudoterminals master file descriptor to read output from the child
62 process, and *stdin_read* is passed file descriptor 0, to read from the
63 parent process's standard input.
64
65 Returning an empty byte string from either callback is interpreted as an
66 end-of-file (EOF) condition, and that callback will not be called after
67 that. If *stdin_read* signals EOF the controlling terminal can no longer
68 communicate with the parent process OR the child process. Unless the child
69 process will quit without any input, *spawn* will then loop forever. If
70 *master_read* signals EOF the same behavior results (on linux at least).
71
Victor Stinner65a796e2020-04-01 18:49:29 +020072 Return the exit status value from :func:`os.waitpid` on the child process.
73
74 :func:`waitstatus_to_exitcode` can be used to convert the exit status into
75 an exit code.
76
Saiyang Gou95f60012020-02-04 16:15:00 -080077 .. audit-event:: pty.spawn argv pty.spawn
Georg Brandl116aa622007-08-15 14:28:22 +000078
Gregory P. Smith0f21adf72012-09-29 12:41:03 -070079 .. versionchanged:: 3.4
80 :func:`spawn` now returns the status value from :func:`os.waitpid`
81 on the child process.
Georg Brandl4cf83f42010-12-30 17:22:33 +000082
83Example
84-------
85
86.. sectionauthor:: Steen Lumholt
87
88The following program acts like the Unix command :manpage:`script(1)`, using a
89pseudo-terminal to record all input and output of a terminal session in a
90"typescript". ::
91
Berker Peksagc2dd6802015-05-12 17:25:06 +030092 import argparse
93 import os
94 import pty
95 import sys
96 import time
Georg Brandl4cf83f42010-12-30 17:22:33 +000097
Berker Peksagc2dd6802015-05-12 17:25:06 +030098 parser = argparse.ArgumentParser()
99 parser.add_argument('-a', dest='append', action='store_true')
100 parser.add_argument('-p', dest='use_python', action='store_true')
101 parser.add_argument('filename', nargs='?', default='typescript')
102 options = parser.parse_args()
Georg Brandl4cf83f42010-12-30 17:22:33 +0000103
Berker Peksagc2dd6802015-05-12 17:25:06 +0300104 shell = sys.executable if options.use_python else os.environ.get('SHELL', 'sh')
105 filename = options.filename
106 mode = 'ab' if options.append else 'wb'
Georg Brandl4cf83f42010-12-30 17:22:33 +0000107
Berker Peksagc2dd6802015-05-12 17:25:06 +0300108 with open(filename, mode) as script:
109 def read(fd):
110 data = os.read(fd, 1024)
111 script.write(data)
112 return data
Georg Brandl4cf83f42010-12-30 17:22:33 +0000113
Berker Peksagc2dd6802015-05-12 17:25:06 +0300114 print('Script started, file is', filename)
115 script.write(('Script started on %s\n' % time.asctime()).encode())
Georg Brandl4cf83f42010-12-30 17:22:33 +0000116
Berker Peksagc2dd6802015-05-12 17:25:06 +0300117 pty.spawn(shell, read)
Georg Brandl4cf83f42010-12-30 17:22:33 +0000118
Berker Peksagc2dd6802015-05-12 17:25:06 +0300119 script.write(('Script done on %s\n' % time.asctime()).encode())
120 print('Script done, file is', filename)