annotate Lib/popen2.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 810bd6773863
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10628
diff changeset
1 """Spawn a command with pipes to its stdin, stdout, and optionally stderr.
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10628
diff changeset
2
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10628
diff changeset
3 The normal os.popen(cmd, mode) call spawns a shell command and provides a
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10628
diff changeset
4 file interface to just the input or output of the process depending on
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10628
diff changeset
5 whether mode is 'r' or 'w'. This module provides the functions popen2(cmd)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10628
diff changeset
6 and popen3(cmd) which return two or three pipes to the spawned command.
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10628
diff changeset
7 """
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10628
diff changeset
8
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 import os
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 import sys
41690
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41216
diff changeset
11 import warnings
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41216
diff changeset
12 warnings.warn("The popen2 module is deprecated. Use the subprocess module.",
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41216
diff changeset
13 DeprecationWarning, stacklevel=2)
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
22246
27a0fb019599 back out spurious change from 1.22
Skip Montanaro <skip@pobox.com>
parents: 22245
diff changeset
15 __all__ = ["popen2", "popen3", "popen4"]
16474
4d406c26c16e a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15004
diff changeset
16
30656
8141eead1907 Patch #817329: Use SC_OPEN_MAX to determine MAXFD. Backported to 2.3.
Martin v. Löwis <martin@v.loewis.de>
parents: 29775
diff changeset
17 try:
8141eead1907 Patch #817329: Use SC_OPEN_MAX to determine MAXFD. Backported to 2.3.
Martin v. Löwis <martin@v.loewis.de>
parents: 29775
diff changeset
18 MAXFD = os.sysconf('SC_OPEN_MAX')
8141eead1907 Patch #817329: Use SC_OPEN_MAX to determine MAXFD. Backported to 2.3.
Martin v. Löwis <martin@v.loewis.de>
parents: 29775
diff changeset
19 except (AttributeError, ValueError):
8141eead1907 Patch #817329: Use SC_OPEN_MAX to determine MAXFD. Backported to 2.3.
Martin v. Löwis <martin@v.loewis.de>
parents: 29775
diff changeset
20 MAXFD = 256
5972
8abb4d8c3134 Rewrite using class, to make waiting for processes possible;
Guido van Rossum <guido@python.org>
parents: 5338
diff changeset
21
8abb4d8c3134 Rewrite using class, to make waiting for processes possible;
Guido van Rossum <guido@python.org>
parents: 5338
diff changeset
22 _active = []
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
5972
8abb4d8c3134 Rewrite using class, to make waiting for processes possible;
Guido van Rossum <guido@python.org>
parents: 5338
diff changeset
24 def _cleanup():
8abb4d8c3134 Rewrite using class, to make waiting for processes possible;
Guido van Rossum <guido@python.org>
parents: 5338
diff changeset
25 for inst in _active[:]:
37192
593e8fb9a3ca Bug #1183780: Add Popen objects to _active only in __del__.
Martin v. Löwis <martin@v.loewis.de>
parents: 37170
diff changeset
26 if inst.poll(_deadstate=sys.maxint) >= 0:
593e8fb9a3ca Bug #1183780: Add Popen objects to _active only in __del__.
Martin v. Löwis <martin@v.loewis.de>
parents: 37170
diff changeset
27 try:
593e8fb9a3ca Bug #1183780: Add Popen objects to _active only in __del__.
Martin v. Löwis <martin@v.loewis.de>
parents: 37170
diff changeset
28 _active.remove(inst)
593e8fb9a3ca Bug #1183780: Add Popen objects to _active only in __del__.
Martin v. Löwis <martin@v.loewis.de>
parents: 37170
diff changeset
29 except ValueError:
593e8fb9a3ca Bug #1183780: Add Popen objects to _active only in __del__.
Martin v. Löwis <martin@v.loewis.de>
parents: 37170
diff changeset
30 # This can happen if two threads create a new Popen instance.
593e8fb9a3ca Bug #1183780: Add Popen objects to _active only in __del__.
Martin v. Löwis <martin@v.loewis.de>
parents: 37170
diff changeset
31 # It's harmless that it was already removed, so ignore.
593e8fb9a3ca Bug #1183780: Add Popen objects to _active only in __del__.
Martin v. Löwis <martin@v.loewis.de>
parents: 37170
diff changeset
32 pass
5972
8abb4d8c3134 Rewrite using class, to make waiting for processes possible;
Guido van Rossum <guido@python.org>
parents: 5338
diff changeset
33
8abb4d8c3134 Rewrite using class, to make waiting for processes possible;
Guido van Rossum <guido@python.org>
parents: 5338
diff changeset
34 class Popen3:
44523
155f5c36ff92 #1591: Clarify docstring of Popen3.
Georg Brandl <georg@python.org>
parents: 41690
diff changeset
35 """Class representing a child process. Normally, instances are created
155f5c36ff92 #1591: Clarify docstring of Popen3.
Georg Brandl <georg@python.org>
parents: 41690
diff changeset
36 internally by the functions popen2() and popen3()."""
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10628
diff changeset
37
14912
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
38 sts = -1 # Child not completed yet
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
39
29775
e6f9f6b5d8d7 Use Boolean values for the capturestderr flag.
Fred Drake <fdrake@acm.org>
parents: 29358
diff changeset
40 def __init__(self, cmd, capturestderr=False, bufsize=-1):
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10628
diff changeset
41 """The parameter 'cmd' is the shell command to execute in a
33720
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
42 sub-process. On UNIX, 'cmd' may be a sequence, in which case arguments
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
43 will be passed directly to the program without shell intervention (as
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
44 with os.spawnv()). If 'cmd' is a string it will be passed to the shell
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
45 (as with os.system()). The 'capturestderr' flag, if true, specifies
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
46 that the object should capture standard error output of the child
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
47 process. The default is false. If the 'bufsize' parameter is
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
48 specified, it specifies the size of the I/O buffers to/from the child
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
49 process."""
14912
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
50 _cleanup()
37170
29df4fe128a0 Preserve command name, for later printing of active
Martin v. Löwis <martin@v.loewis.de>
parents: 34577
diff changeset
51 self.cmd = cmd
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
52 p2cread, p2cwrite = os.pipe()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
53 c2pread, c2pwrite = os.pipe()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
54 if capturestderr:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
55 errout, errin = os.pipe()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
56 self.pid = os.fork()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
57 if self.pid == 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
58 # Child
14912
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
59 os.dup2(p2cread, 0)
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
60 os.dup2(c2pwrite, 1)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
61 if capturestderr:
14912
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
62 os.dup2(errin, 2)
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
63 self._run_child(cmd)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
64 os.close(p2cread)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
65 self.tochild = os.fdopen(p2cwrite, 'w', bufsize)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
66 os.close(c2pwrite)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
67 self.fromchild = os.fdopen(c2pread, 'r', bufsize)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
68 if capturestderr:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
69 os.close(errin)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
70 self.childerr = os.fdopen(errout, 'r', bufsize)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
71 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
72 self.childerr = None
37192
593e8fb9a3ca Bug #1183780: Add Popen objects to _active only in __del__.
Martin v. Löwis <martin@v.loewis.de>
parents: 37170
diff changeset
73
593e8fb9a3ca Bug #1183780: Add Popen objects to _active only in __del__.
Martin v. Löwis <martin@v.loewis.de>
parents: 37170
diff changeset
74 def __del__(self):
593e8fb9a3ca Bug #1183780: Add Popen objects to _active only in __del__.
Martin v. Löwis <martin@v.loewis.de>
parents: 37170
diff changeset
75 # In case the child hasn't been waited on, check if it's done.
593e8fb9a3ca Bug #1183780: Add Popen objects to _active only in __del__.
Martin v. Löwis <martin@v.loewis.de>
parents: 37170
diff changeset
76 self.poll(_deadstate=sys.maxint)
593e8fb9a3ca Bug #1183780: Add Popen objects to _active only in __del__.
Martin v. Löwis <martin@v.loewis.de>
parents: 37170
diff changeset
77 if self.sts < 0:
39133
d0a95654f803 Fix check for empty list (vs. None).
Georg Brandl <georg@python.org>
parents: 38917
diff changeset
78 if _active is not None:
38308
74beb189fd7e Guard the _active.remove() call to avoid errors when there is no _active list.
Georg Brandl <georg@python.org>
parents: 37193
diff changeset
79 # Child is still running, keep us alive until we can wait on it.
74beb189fd7e Guard the _active.remove() call to avoid errors when there is no _active list.
Georg Brandl <georg@python.org>
parents: 37193
diff changeset
80 _active.append(self)
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10628
diff changeset
81
14912
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
82 def _run_child(self, cmd):
23645
e1e90b0f1f45 Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents: 22500
diff changeset
83 if isinstance(cmd, basestring):
14912
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
84 cmd = ['/bin/sh', '-c', cmd]
45482
810bd6773863 Use os.closerange() in popen2.
Georg Brandl <georg@python.org>
parents: 44523
diff changeset
85 os.closerange(3, MAXFD)
14912
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
86 try:
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
87 os.execvp(cmd[0], cmd)
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
88 finally:
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
89 os._exit(1)
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
90
37192
593e8fb9a3ca Bug #1183780: Add Popen objects to _active only in __del__.
Martin v. Löwis <martin@v.loewis.de>
parents: 37170
diff changeset
91 def poll(self, _deadstate=None):
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10628
diff changeset
92 """Return the exit status of the child process if it has finished,
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10628
diff changeset
93 or -1 if it hasn't finished yet."""
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
94 if self.sts < 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
95 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
96 pid, sts = os.waitpid(self.pid, os.WNOHANG)
37193
7f322ef68438 Clarify cases when waitpid might not return self.pid.
Martin v. Löwis <martin@v.loewis.de>
parents: 37192
diff changeset
97 # pid will be 0 if self.pid hasn't terminated
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
98 if pid == self.pid:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
99 self.sts = sts
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
100 except os.error:
37192
593e8fb9a3ca Bug #1183780: Add Popen objects to _active only in __del__.
Martin v. Löwis <martin@v.loewis.de>
parents: 37170
diff changeset
101 if _deadstate is not None:
593e8fb9a3ca Bug #1183780: Add Popen objects to _active only in __del__.
Martin v. Löwis <martin@v.loewis.de>
parents: 37170
diff changeset
102 self.sts = _deadstate
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
103 return self.sts
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10628
diff changeset
104
5972
8abb4d8c3134 Rewrite using class, to make waiting for processes possible;
Guido van Rossum <guido@python.org>
parents: 5338
diff changeset
105 def wait(self):
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10628
diff changeset
106 """Wait for and return the exit status of the child process."""
29358
b1eb2a4756bc When a previous call to poll() has already seen the process status,
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
107 if self.sts < 0:
b1eb2a4756bc When a previous call to poll() has already seen the process status,
Guido van Rossum <guido@python.org>
parents: 23645
diff changeset
108 pid, sts = os.waitpid(self.pid, 0)
37193
7f322ef68438 Clarify cases when waitpid might not return self.pid.
Martin v. Löwis <martin@v.loewis.de>
parents: 37192
diff changeset
109 # This used to be a test, but it is believed to be
7f322ef68438 Clarify cases when waitpid might not return self.pid.
Martin v. Löwis <martin@v.loewis.de>
parents: 37192
diff changeset
110 # always true, so I changed it to an assertion - mvl
7f322ef68438 Clarify cases when waitpid might not return self.pid.
Martin v. Löwis <martin@v.loewis.de>
parents: 37192
diff changeset
111 assert pid == self.pid
7f322ef68438 Clarify cases when waitpid might not return self.pid.
Martin v. Löwis <martin@v.loewis.de>
parents: 37192
diff changeset
112 self.sts = sts
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6344
diff changeset
113 return self.sts
5338
67995bc17f62 Change inspired by Tommy Burnette to add an interface to get stderr, too.
Guido van Rossum <guido@python.org>
parents: 2924
diff changeset
114
14236
58d7995ce795 Added os.popen2() and os.popen3() for non-Windows platforms.
Fred Drake <fdrake@acm.org>
parents: 14079
diff changeset
115
14912
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
116 class Popen4(Popen3):
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
117 childerr = None
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
118
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
119 def __init__(self, cmd, bufsize=-1):
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
120 _cleanup()
37192
593e8fb9a3ca Bug #1183780: Add Popen objects to _active only in __del__.
Martin v. Löwis <martin@v.loewis.de>
parents: 37170
diff changeset
121 self.cmd = cmd
14912
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
122 p2cread, p2cwrite = os.pipe()
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
123 c2pread, c2pwrite = os.pipe()
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
124 self.pid = os.fork()
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
125 if self.pid == 0:
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
126 # Child
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
127 os.dup2(p2cread, 0)
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
128 os.dup2(c2pwrite, 1)
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
129 os.dup2(c2pwrite, 2)
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
130 self._run_child(cmd)
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
131 os.close(p2cread)
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
132 self.tochild = os.fdopen(p2cwrite, 'w', bufsize)
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
133 os.close(c2pwrite)
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
134 self.fromchild = os.fdopen(c2pread, 'r', bufsize)
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
135
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
136
22013
9885da3feeec OS/2 EMX port Library and regression test changes:
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21135
diff changeset
137 if sys.platform[:3] == "win" or sys.platform == "os2emx":
14912
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
138 # Some things don't make sense on non-Unix platforms.
15004
ddb20190591d test_popen2 broke on Windows shortly after 2.0b2 was released. Fixed it.
Tim Peters <tim.peters@gmail.com>
parents: 14912
diff changeset
139 del Popen3, Popen4
14912
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
140
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
141 def popen2(cmd, bufsize=-1, mode='t'):
33720
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
142 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
143 be a sequence, in which case arguments will be passed directly to the
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
144 program without shell intervention (as with os.spawnv()). If 'cmd' is a
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
145 string it will be passed to the shell (as with os.system()). If
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
146 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
147 file objects (child_stdout, child_stdin) are returned."""
13486
32b686da3e51 - changed the nt.popen2 return values back to
Fredrik Lundh <fredrik@pythonware.com>
parents: 13479
diff changeset
148 w, r = os.popen2(cmd, mode, bufsize)
32b686da3e51 - changed the nt.popen2 return values back to
Fredrik Lundh <fredrik@pythonware.com>
parents: 13479
diff changeset
149 return r, w
5338
67995bc17f62 Change inspired by Tommy Burnette to add an interface to get stderr, too.
Guido van Rossum <guido@python.org>
parents: 2924
diff changeset
150
14912
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
151 def popen3(cmd, bufsize=-1, mode='t'):
33720
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
152 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
153 be a sequence, in which case arguments will be passed directly to the
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
154 program without shell intervention (as with os.spawnv()). If 'cmd' is a
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
155 string it will be passed to the shell (as with os.system()). If
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
156 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
157 file objects (child_stdout, child_stdin, child_stderr) are returned."""
13486
32b686da3e51 - changed the nt.popen2 return values back to
Fredrik Lundh <fredrik@pythonware.com>
parents: 13479
diff changeset
158 w, r, e = os.popen3(cmd, mode, bufsize)
32b686da3e51 - changed the nt.popen2 return values back to
Fredrik Lundh <fredrik@pythonware.com>
parents: 13479
diff changeset
159 return r, w, e
13479
93ae34aa20ab - added popen.popen2/popen3/popen4 support for
Fredrik Lundh <fredrik@pythonware.com>
parents: 11706
diff changeset
160
14912
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
161 def popen4(cmd, bufsize=-1, mode='t'):
33720
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
162 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
163 be a sequence, in which case arguments will be passed directly to the
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
164 program without shell intervention (as with os.spawnv()). If 'cmd' is a
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
165 string it will be passed to the shell (as with os.system()). If
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
166 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
167 file objects (child_stdout_stderr, child_stdin) are returned."""
13486
32b686da3e51 - changed the nt.popen2 return values back to
Fredrik Lundh <fredrik@pythonware.com>
parents: 13479
diff changeset
168 w, r = os.popen4(cmd, mode, bufsize)
32b686da3e51 - changed the nt.popen2 return values back to
Fredrik Lundh <fredrik@pythonware.com>
parents: 13479
diff changeset
169 return r, w
32b686da3e51 - changed the nt.popen2 return values back to
Fredrik Lundh <fredrik@pythonware.com>
parents: 13479
diff changeset
170 else:
14912
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
171 def popen2(cmd, bufsize=-1, mode='t'):
33720
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
172 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
173 be a sequence, in which case arguments will be passed directly to the
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
174 program without shell intervention (as with os.spawnv()). If 'cmd' is a
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
175 string it will be passed to the shell (as with os.system()). If
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
176 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
177 file objects (child_stdout, child_stdin) are returned."""
29775
e6f9f6b5d8d7 Use Boolean values for the capturestderr flag.
Fred Drake <fdrake@acm.org>
parents: 29358
diff changeset
178 inst = Popen3(cmd, False, bufsize)
14912
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
179 return inst.fromchild, inst.tochild
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
180
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
181 def popen3(cmd, bufsize=-1, mode='t'):
33720
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
182 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
183 be a sequence, in which case arguments will be passed directly to the
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
184 program without shell intervention (as with os.spawnv()). If 'cmd' is a
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
185 string it will be passed to the shell (as with os.system()). If
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
186 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
187 file objects (child_stdout, child_stdin, child_stderr) are returned."""
29775
e6f9f6b5d8d7 Use Boolean values for the capturestderr flag.
Fred Drake <fdrake@acm.org>
parents: 29358
diff changeset
188 inst = Popen3(cmd, True, bufsize)
14912
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
189 return inst.fromchild, inst.tochild, inst.childerr
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
190
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
191 def popen4(cmd, bufsize=-1, mode='t'):
33720
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
192 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
193 be a sequence, in which case arguments will be passed directly to the
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
194 program without shell intervention (as with os.spawnv()). If 'cmd' is a
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
195 string it will be passed to the shell (as with os.system()). If
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
196 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 31515
diff changeset
197 file objects (child_stdout_stderr, child_stdin) are returned."""
14912
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
198 inst = Popen4(cmd, bufsize)
2cdf7ac9162d popen4(), class Popen4: popen4() support for Unix.
Fred Drake <fdrake@acm.org>
parents: 14361
diff changeset
199 return inst.fromchild, inst.tochild
5972
8abb4d8c3134 Rewrite using class, to make waiting for processes possible;
Guido van Rossum <guido@python.org>
parents: 5338
diff changeset
200
16474
4d406c26c16e a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15004
diff changeset
201 __all__.extend(["Popen3", "Popen4"])