Mercurial > cpython-lru_cache-weakref
annotate Lib/pty.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | f889458c65cc |
| children |
| 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 |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 import tty |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 |
|
16562
623456576d6a
__all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents:
15870
diff
changeset
|
13 __all__ = ["openpty","fork","spawn"] |
|
623456576d6a
__all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents:
15870
diff
changeset
|
14 |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 STDIN_FILENO = 0 |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 STDOUT_FILENO = 1 |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 STDERR_FILENO = 2 |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 CHILD = 0 |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 |
|
13058
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
21 def openpty(): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
22 """openpty() -> (master_fd, slave_fd) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
23 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
|
24 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
25 try: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
26 return os.openpty() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
27 except (AttributeError, OSError): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
28 pass |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
29 master_fd, slave_name = _open_terminal() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
30 slave_fd = slave_open(slave_name) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
31 return master_fd, slave_fd |
|
13058
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
32 |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 def master_open(): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
34 """master_open() -> (master_fd, slave_name) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
35 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
|
36 Deprecated, use openpty() instead.""" |
|
13058
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
37 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
38 try: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
39 master_fd, slave_fd = os.openpty() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
40 except (AttributeError, OSError): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
41 pass |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
42 else: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
43 slave_name = os.ttyname(slave_fd) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
44 os.close(slave_fd) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
45 return master_fd, slave_name |
|
13058
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
46 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
47 return _open_terminal() |
|
13058
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
48 |
|
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
49 def _open_terminal(): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
50 """Open pty master and return (master_fd, tty_name). |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
51 SGI and generic BSD version, for when openpty() fails.""" |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
52 try: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
53 import sgi |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
54 except ImportError: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
55 pass |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
56 else: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
57 try: |
|
17755
47d18602d5ea
Update to reflect deprecation of the FCNTL module: The fcntl module does
Fred Drake <fdrake@acm.org>
parents:
16562
diff
changeset
|
58 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
|
59 except IOError, msg: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
60 raise os.error, msg |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
61 return master_fd, tty_name |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
62 for x in 'pqrstuvwxyzPQRST': |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
63 for y in '0123456789abcdef': |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
64 pty_name = '/dev/pty' + x + y |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
65 try: |
|
17755
47d18602d5ea
Update to reflect deprecation of the FCNTL module: The fcntl module does
Fred Drake <fdrake@acm.org>
parents:
16562
diff
changeset
|
66 fd = os.open(pty_name, os.O_RDWR) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
67 except os.error: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
68 continue |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
69 return (fd, '/dev/tty' + x + y) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
70 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
|
71 |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 def slave_open(tty_name): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
73 """slave_open(tty_name) -> slave_fd |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
74 Open the pty slave and acquire the controlling terminal, returning |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
75 opened filedescriptor. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
76 Deprecated, use openpty() instead.""" |
|
13058
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
77 |
|
26998
6ca1c0d3ef19
Expose I_ constants. Auto-detect stropts.h. Properly configure the slave terminal.
Martin v. Löwis <martin@v.loewis.de>
parents:
24397
diff
changeset
|
78 result = os.open(tty_name, os.O_RDWR) |
|
6ca1c0d3ef19
Expose I_ constants. Auto-detect stropts.h. Properly configure the slave terminal.
Martin v. Löwis <martin@v.loewis.de>
parents:
24397
diff
changeset
|
79 try: |
|
6ca1c0d3ef19
Expose I_ constants. Auto-detect stropts.h. Properly configure the slave terminal.
Martin v. Löwis <martin@v.loewis.de>
parents:
24397
diff
changeset
|
80 from fcntl import ioctl, I_PUSH |
|
6ca1c0d3ef19
Expose I_ constants. Auto-detect stropts.h. Properly configure the slave terminal.
Martin v. Löwis <martin@v.loewis.de>
parents:
24397
diff
changeset
|
81 except ImportError: |
|
6ca1c0d3ef19
Expose I_ constants. Auto-detect stropts.h. Properly configure the slave terminal.
Martin v. Löwis <martin@v.loewis.de>
parents:
24397
diff
changeset
|
82 return result |
|
6ca1c0d3ef19
Expose I_ constants. Auto-detect stropts.h. Properly configure the slave terminal.
Martin v. Löwis <martin@v.loewis.de>
parents:
24397
diff
changeset
|
83 try: |
|
27507
d9d57f5f98cd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
26998
diff
changeset
|
84 ioctl(result, I_PUSH, "ptem") |
|
d9d57f5f98cd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
26998
diff
changeset
|
85 ioctl(result, I_PUSH, "ldterm") |
|
26998
6ca1c0d3ef19
Expose I_ constants. Auto-detect stropts.h. Properly configure the slave terminal.
Martin v. Löwis <martin@v.loewis.de>
parents:
24397
diff
changeset
|
86 except IOError: |
|
6ca1c0d3ef19
Expose I_ constants. Auto-detect stropts.h. Properly configure the slave terminal.
Martin v. Löwis <martin@v.loewis.de>
parents:
24397
diff
changeset
|
87 pass |
|
6ca1c0d3ef19
Expose I_ constants. Auto-detect stropts.h. Properly configure the slave terminal.
Martin v. Löwis <martin@v.loewis.de>
parents:
24397
diff
changeset
|
88 return result |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 def fork(): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
91 """fork() -> (pid, master_fd) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
92 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
|
93 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
94 try: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
95 pid, fd = os.forkpty() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
96 except (AttributeError, OSError): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
97 pass |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
98 else: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
99 if pid == CHILD: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
100 try: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
101 os.setsid() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
102 except OSError: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
103 # os.forkpty() already set us session leader |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
104 pass |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
105 return pid, fd |
|
13058
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
106 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
107 master_fd, slave_fd = openpty() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
108 pid = os.fork() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
109 if pid == CHILD: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
110 # Establish a new session. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
111 os.setsid() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
112 os.close(master_fd) |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
114 # Slave becomes stdin/stdout/stderr of child. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
115 os.dup2(slave_fd, STDIN_FILENO) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
116 os.dup2(slave_fd, STDOUT_FILENO) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
117 os.dup2(slave_fd, STDERR_FILENO) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
118 if (slave_fd > STDERR_FILENO): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
119 os.close (slave_fd) |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 |
|
40313
526c9584f252
Patch #838546: Make terminal become controlling in pty.fork().
Martin v. Löwis <martin@v.loewis.de>
parents:
32883
diff
changeset
|
121 # Explicitly open the tty to make it become a controlling tty. |
|
526c9584f252
Patch #838546: Make terminal become controlling in pty.fork().
Martin v. Löwis <martin@v.loewis.de>
parents:
32883
diff
changeset
|
122 tmp_fd = os.open(os.ttyname(STDOUT_FILENO), os.O_RDWR) |
|
526c9584f252
Patch #838546: Make terminal become controlling in pty.fork().
Martin v. Löwis <martin@v.loewis.de>
parents:
32883
diff
changeset
|
123 os.close(tmp_fd) |
|
40512
92f3ef8fa4af
[Patch #783050 from Patrick Lynch] The emulation of forkpty() is incorrect;
Andrew M. Kuchling <amk@amk.ca>
parents:
40313
diff
changeset
|
124 else: |
|
92f3ef8fa4af
[Patch #783050 from Patrick Lynch] The emulation of forkpty() is incorrect;
Andrew M. Kuchling <amk@amk.ca>
parents:
40313
diff
changeset
|
125 os.close(slave_fd) |
|
40732
5dae0d7f683a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
40512
diff
changeset
|
126 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
127 # Parent and child process. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
128 return pid, master_fd |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 |
|
13058
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
130 def _writen(fd, data): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
131 """Write all the data to a descriptor.""" |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
132 while data != '': |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
133 n = os.write(fd, data) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
134 data = data[n:] |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 |
|
13058
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
136 def _read(fd): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
137 """Default read function.""" |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
138 return os.read(fd, 1024) |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 |
|
13058
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
140 def _copy(master_fd, master_read=_read, stdin_read=_read): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
141 """Parent copy loop. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
142 Copies |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
143 pty master -> standard output (master_read) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
144 standard input -> pty master (stdin_read)""" |
|
74979
f889458c65cc
Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents:
40732
diff
changeset
|
145 fds = [master_fd, STDIN_FILENO] |
|
f889458c65cc
Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents:
40732
diff
changeset
|
146 while True: |
|
f889458c65cc
Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents:
40732
diff
changeset
|
147 rfds, wfds, xfds = select(fds, [], []) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
148 if master_fd in rfds: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
149 data = master_read(master_fd) |
|
74979
f889458c65cc
Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents:
40732
diff
changeset
|
150 if not data: # Reached EOF. |
|
f889458c65cc
Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents:
40732
diff
changeset
|
151 fds.remove(master_fd) |
|
f889458c65cc
Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents:
40732
diff
changeset
|
152 else: |
|
f889458c65cc
Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents:
40732
diff
changeset
|
153 os.write(STDOUT_FILENO, data) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
154 if STDIN_FILENO in rfds: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
155 data = stdin_read(STDIN_FILENO) |
|
74979
f889458c65cc
Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents:
40732
diff
changeset
|
156 if not data: |
|
f889458c65cc
Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents:
40732
diff
changeset
|
157 fds.remove(STDIN_FILENO) |
|
f889458c65cc
Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents:
40732
diff
changeset
|
158 else: |
|
f889458c65cc
Issue #2489: Fix bug in _copy loop that could consume 100% cpu on EOF.
Gregory P. Smith <greg@krypto.org>
parents:
40732
diff
changeset
|
159 _writen(master_fd, data) |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 |
|
13058
53f4022c0cd5
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
161 def spawn(argv, master_read=_read, stdin_read=_read): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
162 """Create a spawned process.""" |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
163 if type(argv) == type(''): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
164 argv = (argv,) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
165 pid, master_fd = fork() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
166 if pid == CHILD: |
|
28089
14f65d661084
Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents:
27507
diff
changeset
|
167 os.execlp(argv[0], *argv) |
|
24397
f6052aeb134f
Patch #581705: Catch OSError, termios.error in spawn. 2.2 bugfix candidate.
Martin v. Löwis <martin@v.loewis.de>
parents:
17978
diff
changeset
|
168 try: |
|
f6052aeb134f
Patch #581705: Catch OSError, termios.error in spawn. 2.2 bugfix candidate.
Martin v. Löwis <martin@v.loewis.de>
parents:
17978
diff
changeset
|
169 mode = tty.tcgetattr(STDIN_FILENO) |
|
f6052aeb134f
Patch #581705: Catch OSError, termios.error in spawn. 2.2 bugfix candidate.
Martin v. Löwis <martin@v.loewis.de>
parents:
17978
diff
changeset
|
170 tty.setraw(STDIN_FILENO) |
|
f6052aeb134f
Patch #581705: Catch OSError, termios.error in spawn. 2.2 bugfix candidate.
Martin v. Löwis <martin@v.loewis.de>
parents:
17978
diff
changeset
|
171 restore = 1 |
|
f6052aeb134f
Patch #581705: Catch OSError, termios.error in spawn. 2.2 bugfix candidate.
Martin v. Löwis <martin@v.loewis.de>
parents:
17978
diff
changeset
|
172 except tty.error: # This is the same as termios.error |
|
f6052aeb134f
Patch #581705: Catch OSError, termios.error in spawn. 2.2 bugfix candidate.
Martin v. Löwis <martin@v.loewis.de>
parents:
17978
diff
changeset
|
173 restore = 0 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
174 try: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13322
diff
changeset
|
175 _copy(master_fd, master_read, stdin_read) |
|
24397
f6052aeb134f
Patch #581705: Catch OSError, termios.error in spawn. 2.2 bugfix candidate.
Martin v. Löwis <martin@v.loewis.de>
parents:
17978
diff
changeset
|
176 except (IOError, OSError): |
|
f6052aeb134f
Patch #581705: Catch OSError, termios.error in spawn. 2.2 bugfix candidate.
Martin v. Löwis <martin@v.loewis.de>
parents:
17978
diff
changeset
|
177 if restore: |
|
f6052aeb134f
Patch #581705: Catch OSError, termios.error in spawn. 2.2 bugfix candidate.
Martin v. Löwis <martin@v.loewis.de>
parents:
17978
diff
changeset
|
178 tty.tcsetattr(STDIN_FILENO, tty.TCSAFLUSH, mode) |
|
32257
8c0394aec6dc
[Bug #897935] Fix fd leak in pty.spawn(). Reported by James Henstridge; 2.3 bugfix candidate.
Andrew M. Kuchling <amk@amk.ca>
parents:
28089
diff
changeset
|
179 |
|
8c0394aec6dc
[Bug #897935] Fix fd leak in pty.spawn(). Reported by James Henstridge; 2.3 bugfix candidate.
Andrew M. Kuchling <amk@amk.ca>
parents:
28089
diff
changeset
|
180 os.close(master_fd) |
