ยปCore Development>Code coverage>Lib/popen2.py

Python code coverage for Lib/popen2.py

#countcontent
1n/a"""Spawn a command with pipes to its stdin, stdout, and optionally stderr.
2n/a
3n/aThe normal os.popen(cmd, mode) call spawns a shell command and provides a
4n/afile interface to just the input or output of the process depending on
5n/awhether mode is 'r' or 'w'. This module provides the functions popen2(cmd)
6n/aand popen3(cmd) which return two or three pipes to the spawned command.
71"""
8n/a
91import os
101import sys
111import warnings
121warnings.warn("The popen2 module is deprecated. Use the subprocess module.",
131 DeprecationWarning, stacklevel=2)
14n/a
151__all__ = ["popen2", "popen3", "popen4"]
16n/a
171try:
181 MAXFD = os.sysconf('SC_OPEN_MAX')
190except (AttributeError, ValueError):
200 MAXFD = 256
21n/a
221_active = []
23n/a
241def _cleanup():
2516 for inst in _active[:]:
263 if inst.poll(_deadstate=sys.maxint) >= 0:
273 try:
283 _active.remove(inst)
290 except ValueError:
30n/a # This can happen if two threads create a new Popen instance.
31n/a # It's harmless that it was already removed, so ignore.
320 pass
33n/a
342class Popen3:
35n/a """Class representing a child process. Normally, instances are created
361 internally by the functions popen2() and popen3()."""
37n/a
381 sts = -1 # Child not completed yet
39n/a
401 def __init__(self, cmd, capturestderr=False, bufsize=-1):
41n/a """The parameter 'cmd' is the shell command to execute in a
42n/a sub-process. On UNIX, 'cmd' may be a sequence, in which case arguments
43n/a will be passed directly to the program without shell intervention (as
44n/a with os.spawnv()). If 'cmd' is a string it will be passed to the shell
45n/a (as with os.system()). The 'capturestderr' flag, if true, specifies
46n/a that the object should capture standard error output of the child
47n/a process. The default is false. If the 'bufsize' parameter is
48n/a specified, it specifies the size of the I/O buffers to/from the child
49n/a process."""
503 _cleanup()
513 self.cmd = cmd
523 p2cread, p2cwrite = os.pipe()
533 c2pread, c2pwrite = os.pipe()
543 if capturestderr:
552 errout, errin = os.pipe()
563 self.pid = os.fork()
573 if self.pid == 0:
58n/a # Child
590 os.dup2(p2cread, 0)
600 os.dup2(c2pwrite, 1)
610 if capturestderr:
620 os.dup2(errin, 2)
630 self._run_child(cmd)
643 os.close(p2cread)
653 self.tochild = os.fdopen(p2cwrite, 'w', bufsize)
663 os.close(c2pwrite)
673 self.fromchild = os.fdopen(c2pread, 'r', bufsize)
683 if capturestderr:
692 os.close(errin)
702 self.childerr = os.fdopen(errout, 'r', bufsize)
71n/a else:
721 self.childerr = None
73n/a
741 def __del__(self):
75n/a # In case the child hasn't been waited on, check if it's done.
766 self.poll(_deadstate=sys.maxint)
776 if self.sts < 0:
783 if _active is not None:
79n/a # Child is still running, keep us alive until we can wait on it.
803 _active.append(self)
81n/a
821 def _run_child(self, cmd):
830 if isinstance(cmd, basestring):
840 cmd = ['/bin/sh', '-c', cmd]
850 os.closerange(3, MAXFD)
860 try:
870 os.execvp(cmd[0], cmd)
88n/a finally:
890 os._exit(1)
90n/a
911 def poll(self, _deadstate=None):
92n/a """Return the exit status of the child process if it has finished,
93n/a or -1 if it hasn't finished yet."""
949 if self.sts < 0:
954 try:
964 pid, sts = os.waitpid(self.pid, os.WNOHANG)
97n/a # pid will be 0 if self.pid hasn't terminated
984 if pid == self.pid:
991 self.sts = sts
1000 except os.error:
1010 if _deadstate is not None:
1020 self.sts = _deadstate
1039 return self.sts
104n/a
1051 def wait(self):
106n/a """Wait for and return the exit status of the child process."""
1072 if self.sts < 0:
1082 pid, sts = os.waitpid(self.pid, 0)
109n/a # This used to be a test, but it is believed to be
110n/a # always true, so I changed it to an assertion - mvl
1112 assert pid == self.pid
1122 self.sts = sts
1132 return self.sts
114n/a
115n/a
1162class Popen4(Popen3):
1171 childerr = None
118n/a
1191 def __init__(self, cmd, bufsize=-1):
1200 _cleanup()
1210 self.cmd = cmd
1220 p2cread, p2cwrite = os.pipe()
1230 c2pread, c2pwrite = os.pipe()
1240 self.pid = os.fork()
1250 if self.pid == 0:
126n/a # Child
1270 os.dup2(p2cread, 0)
1280 os.dup2(c2pwrite, 1)
1290 os.dup2(c2pwrite, 2)
1300 self._run_child(cmd)
1310 os.close(p2cread)
1320 self.tochild = os.fdopen(p2cwrite, 'w', bufsize)
1330 os.close(c2pwrite)
1340 self.fromchild = os.fdopen(c2pread, 'r', bufsize)
135n/a
136n/a
1371if sys.platform[:3] == "win" or sys.platform == "os2emx":
138n/a # Some things don't make sense on non-Unix platforms.
1390 del Popen3, Popen4
140n/a
1410 def popen2(cmd, bufsize=-1, mode='t'):
142n/a """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
143n/a be a sequence, in which case arguments will be passed directly to the
144n/a program without shell intervention (as with os.spawnv()). If 'cmd' is a
145n/a string it will be passed to the shell (as with os.system()). If
146n/a 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
147n/a file objects (child_stdout, child_stdin) are returned."""
1480 w, r = os.popen2(cmd, mode, bufsize)
1490 return r, w
150n/a
1510 def popen3(cmd, bufsize=-1, mode='t'):
152n/a """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
153n/a be a sequence, in which case arguments will be passed directly to the
154n/a program without shell intervention (as with os.spawnv()). If 'cmd' is a
155n/a string it will be passed to the shell (as with os.system()). If
156n/a 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
157n/a file objects (child_stdout, child_stdin, child_stderr) are returned."""
1580 w, r, e = os.popen3(cmd, mode, bufsize)
1590 return r, w, e
160n/a
1610 def popen4(cmd, bufsize=-1, mode='t'):
162n/a """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
163n/a be a sequence, in which case arguments will be passed directly to the
164n/a program without shell intervention (as with os.spawnv()). If 'cmd' is a
165n/a string it will be passed to the shell (as with os.system()). If
166n/a 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
167n/a file objects (child_stdout_stderr, child_stdin) are returned."""
1680 w, r = os.popen4(cmd, mode, bufsize)
1690 return r, w
170n/aelse:
1711 def popen2(cmd, bufsize=-1, mode='t'):
172n/a """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
173n/a be a sequence, in which case arguments will be passed directly to the
174n/a program without shell intervention (as with os.spawnv()). If 'cmd' is a
175n/a string it will be passed to the shell (as with os.system()). If
176n/a 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
177n/a file objects (child_stdout, child_stdin) are returned."""
1781 inst = Popen3(cmd, False, bufsize)
1791 return inst.fromchild, inst.tochild
180n/a
1811 def popen3(cmd, bufsize=-1, mode='t'):
182n/a """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
183n/a be a sequence, in which case arguments will be passed directly to the
184n/a program without shell intervention (as with os.spawnv()). If 'cmd' is a
185n/a string it will be passed to the shell (as with os.system()). If
186n/a 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
187n/a file objects (child_stdout, child_stdin, child_stderr) are returned."""
1882 inst = Popen3(cmd, True, bufsize)
1892 return inst.fromchild, inst.tochild, inst.childerr
190n/a
1911 def popen4(cmd, bufsize=-1, mode='t'):
192n/a """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
193n/a be a sequence, in which case arguments will be passed directly to the
194n/a program without shell intervention (as with os.spawnv()). If 'cmd' is a
195n/a string it will be passed to the shell (as with os.system()). If
196n/a 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
197n/a file objects (child_stdout_stderr, child_stdin) are returned."""
1980 inst = Popen4(cmd, bufsize)
1990 return inst.fromchild, inst.tochild
200n/a
2011 __all__.extend(["Popen3", "Popen4"])