- added popen.popen2/popen3/popen4 support for
windows. - added optional mode argument to popen2/popen3 for unix; if the second argument is an integer, it's assumed to be the buffer size. - changed nt.popen2/popen3/popen4 return values to match the popen2 module (stdout first, not stdin).
This commit is contained in:
parent
766ccdcf18
commit
bb7eeff44a
@ -89,31 +89,55 @@ class Popen3:
|
|||||||
_active.remove(self)
|
_active.remove(self)
|
||||||
return self.sts
|
return self.sts
|
||||||
|
|
||||||
def popen2(cmd, bufsize=-1):
|
try:
|
||||||
"""Execute the shell command 'cmd' in a sub-process. If 'bufsize' is
|
from os import popen2
|
||||||
specified, it sets the buffer size for the I/O pipes. The file objects
|
except NameError:
|
||||||
(child_stdout, child_stdin) are returned."""
|
def popen2(cmd, mode='t', bufsize=-1):
|
||||||
_cleanup()
|
"""Execute the shell command 'cmd' in a sub-process. If 'bufsize' is
|
||||||
inst = Popen3(cmd, 0, bufsize)
|
specified, it sets the buffer size for the I/O pipes. The file objects
|
||||||
return inst.fromchild, inst.tochild
|
(child_stdout, child_stdin) are returned."""
|
||||||
|
if type(mode) is type(0) and bufsize == -1:
|
||||||
|
bufsize = mode
|
||||||
|
mode = 't'
|
||||||
|
assert mode in ('t', 'b')
|
||||||
|
_cleanup()
|
||||||
|
inst = Popen3(cmd, 0, bufsize)
|
||||||
|
return inst.fromchild, inst.tochild
|
||||||
|
|
||||||
def popen3(cmd, bufsize=-1):
|
try:
|
||||||
"""Execute the shell command 'cmd' in a sub-process. If 'bufsize' is
|
from os import popen3
|
||||||
specified, it sets the buffer size for the I/O pipes. The file objects
|
except NameError:
|
||||||
(child_stdout, child_stdin, child_stderr) are returned."""
|
def popen3(cmd, mode='t', bufsize=-1):
|
||||||
_cleanup()
|
"""Execute the shell command 'cmd' in a sub-process. If 'bufsize' is
|
||||||
inst = Popen3(cmd, 1, bufsize)
|
specified, it sets the buffer size for the I/O pipes. The file objects
|
||||||
return inst.fromchild, inst.tochild, inst.childerr
|
(child_stdout, child_stdin, child_stderr) are returned."""
|
||||||
|
if type(mode) is type(0) and bufsize == -1:
|
||||||
|
bufsize = mode
|
||||||
|
mode = 't'
|
||||||
|
assert mode in ('t', 'b')
|
||||||
|
_cleanup()
|
||||||
|
inst = Popen3(cmd, 1, bufsize)
|
||||||
|
return inst.fromchild, inst.tochild, inst.childerr
|
||||||
|
|
||||||
|
try:
|
||||||
|
from os import popen4
|
||||||
|
except NameError:
|
||||||
|
pass # not on unix
|
||||||
|
|
||||||
def _test():
|
def _test():
|
||||||
teststr = "abc\n"
|
teststr = "abc\n"
|
||||||
print "testing popen2..."
|
print "testing popen2..."
|
||||||
r, w = popen2('cat')
|
r, w = popen2('cat')
|
||||||
|
print r, w
|
||||||
w.write(teststr)
|
w.write(teststr)
|
||||||
w.close()
|
w.close()
|
||||||
assert r.read() == teststr
|
assert r.read() == teststr
|
||||||
print "testing popen3..."
|
print "testing popen3..."
|
||||||
r, w, e = popen3(['cat'])
|
try:
|
||||||
|
r, w, e = popen3(['cat'])
|
||||||
|
except:
|
||||||
|
r, w, e = popen3('cat')
|
||||||
|
print r, w, e
|
||||||
w.write(teststr)
|
w.write(teststr)
|
||||||
w.close()
|
w.close()
|
||||||
assert r.read() == teststr
|
assert r.read() == teststr
|
||||||
|
@ -2149,12 +2149,12 @@ static PyObject *_PyPopen(char *, int, int);
|
|||||||
static PyObject *
|
static PyObject *
|
||||||
posix_popen(PyObject *self, PyObject *args)
|
posix_popen(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
int bufsize = -1;
|
|
||||||
PyObject *f, *s;
|
PyObject *f, *s;
|
||||||
int tm = 0;
|
int tm = 0;
|
||||||
|
|
||||||
char *cmdstring;
|
char *cmdstring;
|
||||||
char *mode = "r";
|
char *mode = "r";
|
||||||
|
int bufsize = -1;
|
||||||
if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
|
if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
@ -2174,9 +2174,9 @@ posix_popen(PyObject *self, PyObject *args)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (*(mode+1) == 't')
|
if (*(mode+1) == 't')
|
||||||
f = _PyPopen(cmdstring, tm | _O_TEXT , POPEN_1);
|
f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
|
||||||
else if (*(mode+1) == 'b')
|
else if (*(mode+1) == 'b')
|
||||||
f = _PyPopen(cmdstring, tm | _O_BINARY , POPEN_1);
|
f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
|
||||||
else
|
else
|
||||||
f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
|
f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
|
||||||
|
|
||||||
@ -2221,9 +2221,9 @@ win32_popen2(PyObject *self, PyObject *args)
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* Variation on <om win32pipe.popen>
|
* Variation on <om win32pipe.popen>
|
||||||
|
*
|
||||||
* The result of this function is 3 pipes - the process's stdin,
|
* The result of this function is 3 pipes - the process's stdin,
|
||||||
* stdout and stderr
|
* stdout and stderr
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
@ -2288,7 +2288,7 @@ win32_popen4(PyObject *self, PyObject *args)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
f = _PyPopen(cmdstring, tm , POPEN_4);
|
f = _PyPopen(cmdstring, tm, POPEN_4);
|
||||||
|
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
@ -2516,7 +2516,7 @@ _PyPopen(char *cmdstring, int mode, int n)
|
|||||||
if (n != 4)
|
if (n != 4)
|
||||||
CloseHandle(hChildStderrRdDup);
|
CloseHandle(hChildStderrRdDup);
|
||||||
|
|
||||||
f = Py_BuildValue("OO",p1,p2);
|
f = Py_BuildValue("OO",p2,p1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2545,7 +2545,7 @@ _PyPopen(char *cmdstring, int mode, int n)
|
|||||||
PyFile_SetBufSize(p1, 0);
|
PyFile_SetBufSize(p1, 0);
|
||||||
PyFile_SetBufSize(p2, 0);
|
PyFile_SetBufSize(p2, 0);
|
||||||
PyFile_SetBufSize(p3, 0);
|
PyFile_SetBufSize(p3, 0);
|
||||||
f = Py_BuildValue("OOO",p1,p2,p3);
|
f = Py_BuildValue("OOO",p2,p1,p3);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user