Mercurial > cpython-lru_cache-weakref
annotate Lib/pty.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | e4752f316053 |
| children | 6ca1c0d3ef19 |
| rev | line source |
|---|---|
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
10828
diff
changeset
|
1 """Pseudo terminal utilities.""" |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 # Bugs: No signal handling. Doesn't set slave termios and window size. |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
4 # Only tested on Linux. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
5 # See: W. Richard Stevens. 1992. Advanced Programming in the |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
6 # UNIX Environment. Chapter 19. |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 # Author: Steen Lumholt -- with additions by Guido. |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 from select import select |
|
17755
47d18602d5ea
Update to reflect deprecation of the FCNTL module: The fcntl module does
Fred Drake <fdrake@acm.org>
parents:
16562
diff
changeset
|
10 import os |
|
17818
29d84f920d5b
A disgusting "fix" for the test___all__ failure under Windows.
Tim Peters <tim.peters@gmail.com>
parents:
17798
diff
changeset
|
11 |
|
29d84f920d5b
A disgusting "fix" for the test___all__ failure under Windows.
Tim Peters <tim.peters@gmail.com>
parents:
17798
diff
changeset
|
12 # Absurd: import termios and then delete it. This is to force an attempt |
|
29d84f920d5b
A disgusting "fix" for the test___all__ failure under Windows.
Tim Peters <tim.peters@gmail.com>
parents:
17798
diff
changeset
|
13 # to import pty to raise an ImportError on platforms that lack termios. |
|
29d84f920d5b
A disgusting "fix" for the test___all__ failure under Windows.
Tim Peters <tim.peters@gmail.com>
parents:
17798
diff
changeset
|
14 # Without this explicit import of termios here, some other module may |
|
29d84f920d5b
A disgusting "fix" for the test___all__ failure under Windows.
Tim Peters <tim.peters@gmail.com>
parents:
17798
diff
changeset
|
15 # import tty first, which in turn imports termios and dies with an |
|
29d84f920d5b
A disgusting "fix" for the test___all__ failure under Windows.
Tim Peters <tim.peters@gmail.com>
parents:
17798
diff
changeset
|
16 # ImportError then. But since tty *does* exist across platforms, that |
|
29d84f920d5b
A disgusting "fix" for the test___all__ failure under Windows.
Tim Peters <tim.peters@gmail.com>
parents:
17798
diff
changeset
|
17 # leaves a damaged module object for tty in sys.modules, and the import |
|
29d84f920d5b
A disgusting "fix" for the test___all__ failure under Windows.
Tim Peters <tim.peters@gmail.com>
parents:
17798
diff
changeset
|
18 # of tty here then appears to work despite that the tty imported is junk. |
|
17978
5e87a08abb05
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
17818
diff
changeset
|
19 import termios |
|
17818
29d84f920d5b
A disgusting "fix" for the test___all__ failure under Windows.
Tim Peters <tim.peters@gmail.com>
parents:
17798
diff
changeset
|
20 del termios |
|
29d84f920d5b
A disgusting "fix" for the test___all__ failure under Windows.
Tim Peters <tim.peters@gmail.com>
parents:
17798
diff
changeset
|
21 |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 import tty |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 |
|
16562
623456576d6a
__all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents:
15870
diff
changeset
|
24 __all__ = ["openpty","fork","spawn"] |
|
623456576d6a
__all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents:
15870
diff
changeset
|
25 |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 STDIN_FILENO = 0 |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 STDOUT_FILENO = 1 |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 STDERR_FILENO = 2 |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 CHILD = 0 |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 |
|
13058
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
32 def openpty(): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
33 """openpty() -> (master_fd, slave_fd) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
34 Open a pty master/slave pair, using os.openpty() if possible.""" |
|
13058
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
35 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
36 try: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
37 return os.openpty() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
38 except (AttributeError, OSError): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
39 pass |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
40 master_fd, slave_name = _open_terminal() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
41 slave_fd = slave_open(slave_name) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
42 return master_fd, slave_fd |
|
13058
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
43 |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 def master_open(): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
45 """master_open() -> (master_fd, slave_name) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
46 Open a pty master and return the fd, and the filename of the slave end. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
47 Deprecated, use openpty() instead.""" |
|
13058
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
48 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
49 try: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
50 master_fd, slave_fd = os.openpty() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
51 except (AttributeError, OSError): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
52 pass |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
53 else: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
54 slave_name = os.ttyname(slave_fd) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
55 os.close(slave_fd) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
56 return master_fd, slave_name |
|
13058
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
57 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
58 return _open_terminal() |
|
13058
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
59 |
|
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
60 def _open_terminal(): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
61 """Open pty master and return (master_fd, tty_name). |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
62 SGI and generic BSD version, for when openpty() fails.""" |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
63 try: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
64 import sgi |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
65 except ImportError: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
66 pass |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
67 else: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
68 try: |
|
17755
47d18602d5ea
Update to reflect deprecation of the FCNTL module: The fcntl module does
Fred Drake <fdrake@acm.org>
parents:
16562
diff
changeset
|
69 tty_name, master_fd = sgi._getpty(os.O_RDWR, 0666, 0) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
70 except IOError, msg: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
71 raise os.error, msg |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
72 return master_fd, tty_name |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
73 for x in 'pqrstuvwxyzPQRST': |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
74 for y in '0123456789abcdef': |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
75 pty_name = '/dev/pty' + x + y |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
76 try: |
|
17755
47d18602d5ea
Update to reflect deprecation of the FCNTL module: The fcntl module does
Fred Drake <fdrake@acm.org>
parents:
16562
diff
changeset
|
77 fd = os.open(pty_name, os.O_RDWR) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
78 except os.error: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
79 continue |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
80 return (fd, '/dev/tty' + x + y) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
81 raise os.error, 'out of pty devices' |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 def slave_open(tty_name): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
84 """slave_open(tty_name) -> slave_fd |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
85 Open the pty slave and acquire the controlling terminal, returning |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
86 opened filedescriptor. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
87 Deprecated, use openpty() instead.""" |
|
13058
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
88 |
|
17755
47d18602d5ea
Update to reflect deprecation of the FCNTL module: The fcntl module does
Fred Drake <fdrake@acm.org>
parents:
16562
diff
changeset
|
89 return os.open(tty_name, os.O_RDWR) |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 def fork(): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
92 """fork() -> (pid, master_fd) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
93 Fork and make the child a session leader with a controlling terminal.""" |
|
13058
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
94 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
95 try: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
96 pid, fd = os.forkpty() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
97 except (AttributeError, OSError): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
98 pass |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
99 else: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
100 if pid == CHILD: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
101 try: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
102 os.setsid() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
103 except OSError: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
104 # os.forkpty() already set us session leader |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
105 pass |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
106 return pid, fd |
|
13058
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
107 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
108 master_fd, slave_fd = openpty() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
109 pid = os.fork() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
110 if pid == CHILD: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
111 # Establish a new session. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
112 os.setsid() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
113 os.close(master_fd) |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
115 # Slave becomes stdin/stdout/stderr of child. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
116 os.dup2(slave_fd, STDIN_FILENO) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
117 os.dup2(slave_fd, STDOUT_FILENO) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
118 os.dup2(slave_fd, STDERR_FILENO) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
119 if (slave_fd > STDERR_FILENO): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
120 os.close (slave_fd) |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
122 # Parent and child process. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
123 return pid, master_fd |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 |
|
13058
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
125 def _writen(fd, data): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
126 """Write all the data to a descriptor.""" |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
127 while data != '': |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
128 n = os.write(fd, data) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
129 data = data[n:] |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 |
|
13058
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
131 def _read(fd): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
132 """Default read function.""" |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
133 return os.read(fd, 1024) |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 |
|
13058
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
135 def _copy(master_fd, master_read=_read, stdin_read=_read): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
136 """Parent copy loop. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
137 Copies |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
138 pty master -> standard output (master_read) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
139 standard input -> pty master (stdin_read)""" |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
140 while 1: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
141 rfds, wfds, xfds = select( |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
142 [master_fd, STDIN_FILENO], [], []) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
143 if master_fd in rfds: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
144 data = master_read(master_fd) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
145 os.write(STDOUT_FILENO, data) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
146 if STDIN_FILENO in rfds: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
147 data = stdin_read(STDIN_FILENO) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
148 _writen(master_fd, data) |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 |
|
13058
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
150 def spawn(argv, master_read=_read, stdin_read=_read): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
151 """Create a spawned process.""" |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
152 if type(argv) == type(''): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
153 argv = (argv,) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
154 pid, master_fd = fork() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
155 if pid == CHILD: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
156 apply(os.execlp, (argv[0],) + argv) |
|
25485
e4752f316053
Backport loewis' 1.12:
Michael W. Hudson <mwh@python.net>
parents:
17978
diff
changeset
|
157 try: |
|
e4752f316053
Backport loewis' 1.12:
Michael W. Hudson <mwh@python.net>
parents:
17978
diff
changeset
|
158 mode = tty.tcgetattr(STDIN_FILENO) |
|
e4752f316053
Backport loewis' 1.12:
Michael W. Hudson <mwh@python.net>
parents:
17978
diff
changeset
|
159 tty.setraw(STDIN_FILENO) |
|
e4752f316053
Backport loewis' 1.12:
Michael W. Hudson <mwh@python.net>
parents:
17978
diff
changeset
|
160 restore = 1 |
|
e4752f316053
Backport loewis' 1.12:
Michael W. Hudson <mwh@python.net>
parents:
17978
diff
changeset
|
161 except tty.error: # This is the same as termios.error |
|
e4752f316053
Backport loewis' 1.12:
Michael W. Hudson <mwh@python.net>
parents:
17978
diff
changeset
|
162 restore = 0 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
163 try: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
164 _copy(master_fd, master_read, stdin_read) |
|
25485
e4752f316053
Backport loewis' 1.12:
Michael W. Hudson <mwh@python.net>
parents:
17978
diff
changeset
|
165 except (IOError, OSError): |
|
e4752f316053
Backport loewis' 1.12:
Michael W. Hudson <mwh@python.net>
parents:
17978
diff
changeset
|
166 if restore: |
|
e4752f316053
Backport loewis' 1.12:
Michael W. Hudson <mwh@python.net>
parents:
17978
diff
changeset
|
167 tty.tcsetattr(STDIN_FILENO, tty.TCSAFLUSH, mode) |
