summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/io.py8
-rw-r--r--Lib/os.py38
-rw-r--r--Lib/subprocess.py11
-rw-r--r--Lib/test/test_io.py2
-rw-r--r--Lib/test/test_tempfile.py2
-rw-r--r--Modules/_fileio.c6
-rw-r--r--Modules/posixmodule.c1544
7 files changed, 59 insertions, 1552 deletions
diff --git a/Lib/io.py b/Lib/io.py
index 9cbc11c..222197e 100644
--- a/Lib/io.py
+++ b/Lib/io.py
@@ -120,6 +120,8 @@ def open(file, mode="r", buffering=None, *, encoding=None, newline=None):
(appending and "a" or "") +
(updating and "+" or ""))
if buffering is None:
+ buffering = -1
+ if buffering < 0:
buffering = DEFAULT_BUFFER_SIZE
# XXX Should default to line buffering if os.isatty(raw.fileno())
try:
@@ -446,8 +448,8 @@ class BufferedIOBase(IOBase):
implementation, but wrap one.
"""
- def read(self, n: int = -1) -> bytes:
- """read(n: int = -1) -> bytes. Read and return up to n bytes.
+ def read(self, n: int = None) -> bytes:
+ """read(n: int = None) -> bytes. Read and return up to n bytes.
If the argument is omitted, None, or negative, reads and
returns all data until EOF.
@@ -717,6 +719,8 @@ class BufferedWriter(_BufferedIOMixin):
self._write_buf = b""
def write(self, b):
+ if not isinstance(b, bytes):
+ b = bytes(b)
# XXX we can implement some more tricks to try and avoid partial writes
if len(self._write_buf) > self.buffer_size:
# We're full, so let's pre-flush the buffer
diff --git a/Lib/os.py b/Lib/os.py
index 5ddf07c..0ced882 100644
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -696,3 +696,41 @@ if not _exists("urandom"):
bs += read(_urandomfd, n - len(bs))
close(_urandomfd)
return bs
+
+# Supply os.popen()
+def popen(cmd, mode="r", buffering=None):
+ if not isinstance(cmd, basestring):
+ raise TypeError("invalid cmd type (%s, expected string)" % type(cmd))
+ if mode not in ("r", "w"):
+ raise ValueError("invalid mode %r" % mode)
+ import subprocess, io
+ if mode == "r":
+ proc = subprocess.Popen(cmd,
+ shell=True,
+ stdout=subprocess.PIPE,
+ bufsize=buffering)
+ return _wrap_close(io.TextIOWrapper(proc.stdout), proc)
+ else:
+ proc = subprocess.Popen(cmd,
+ shell=True,
+ stdin=subprocess.PIPE,
+ bufsize=buffering)
+ return _wrap_close(io.TextIOWrapper(proc.stdin), proc)
+
+# Helper for popen() -- a proxy for a file whose close waits for the process
+class _wrap_close:
+ def __init__(self, stream, proc):
+ self._stream = stream
+ self._proc = proc
+ def close(self):
+ self._stream.close()
+ return self._proc.wait() << 8 # Shift left to match old behavior
+ def __getattr__(self, name):
+ return getattr(self._stream, name)
+
+# Supply os.fdopen() (used by subprocess!)
+def fdopen(fd, mode="r", buffering=-1):
+ if not isinstance(fd, int):
+ raise TypeError("invalid fd type (%s, expected integer)" % type(fd))
+ import io
+ return io.open(fd, mode, buffering)
diff --git a/Lib/subprocess.py b/Lib/subprocess.py
index 2d02df6..fe64cc0 100644
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -246,11 +246,11 @@ A more real-world example would look like this:
try:
retcode = call("mycmd" + " myarg", shell=True)
if retcode < 0:
- print >>sys.stderr, "Child was terminated by signal", -retcode
+ print("Child was terminated by signal", -retcode, file=sys.stderr)
else:
- print >>sys.stderr, "Child returned", retcode
-except OSError, e:
- print >>sys.stderr, "Execution failed:", e
+ print("Child returned", retcode, file=sys.stderr)
+except OSError as e:
+ print("Execution failed:", e, file=sys.stderr)
Replacing os.spawn*
@@ -539,6 +539,8 @@ class Popen(object):
os.close(errread)
errread = None
+ if bufsize == 0:
+ bufsize = 1 # Nearly unbuffered (XXX for now)
if p2cwrite is not None:
self.stdin = os.fdopen(p2cwrite, 'wb', bufsize)
if c2pread is not None:
@@ -1007,6 +1009,7 @@ class Popen(object):
if data:
os.waitpid(self.pid, 0)
child_exception = pickle.loads(data)
+ print("exc:", child_exception)
raise child_exception
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index d1c0f68..c98d61f 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -478,7 +478,7 @@ class TextIOWrapperTest(unittest.TestCase):
[ '\r\n', input_lines ],
]
- encodings = ('utf-8', 'bz2')
+ encodings = ('utf-8', 'latin-1')
# Try a range of pad sizes to test the case where \r is the last
# character in TextIOWrapper._pending_line.
diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py
index f8b5bdf..8c2eb23 100644
--- a/Lib/test/test_tempfile.py
+++ b/Lib/test/test_tempfile.py
@@ -730,7 +730,7 @@ class test_SpooledTemporaryFile(TC):
write("a" * 35)
write("b" * 35)
seek(0, 0)
- self.failUnless(read(70) == 'a'*35 + 'b'*35)
+ self.assertEqual(read(70), 'a'*35 + 'b'*35)
test_classes.append(test_SpooledTemporaryFile)
diff --git a/Modules/_fileio.c b/Modules/_fileio.c
index 2bda155..c46f17e 100644
--- a/Modules/_fileio.c
+++ b/Modules/_fileio.c
@@ -375,6 +375,12 @@ fileio_read(PyFileIOObject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "i", &size))
return NULL;
+ if (size < 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "negative read count");
+ return NULL;
+ }
+
bytes = PyBytes_FromStringAndSize(NULL, size);
if (bytes == NULL)
return NULL;
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 1900656..a2068ac 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -112,7 +112,6 @@ corresponding Unix manual entries for more information on calls.");
#define HAVE_GETCWD 1
#define HAVE_OPENDIR 1
#define HAVE_PIPE 1
-#define HAVE_POPEN 1
#define HAVE_SYSTEM 1
#define HAVE_WAIT 1
#else
@@ -121,7 +120,6 @@ corresponding Unix manual entries for more information on calls.");
#define HAVE_SPAWNV 1
#define HAVE_EXECV 1
#define HAVE_PIPE 1
-#define HAVE_POPEN 1
#define HAVE_SYSTEM 1
#define HAVE_CWAIT 1
#define HAVE_FSYNC 1
@@ -145,9 +143,6 @@ corresponding Unix manual entries for more information on calls.");
#define HAVE_KILL 1
#define HAVE_OPENDIR 1
#define HAVE_PIPE 1
-#ifndef __rtems__
-#define HAVE_POPEN 1
-#endif
#define HAVE_SYSTEM 1
#define HAVE_WAIT 1
#define HAVE_TTYNAME 1
@@ -194,7 +189,6 @@ extern int link(const char *, const char *);
extern int rename(const char *, const char *);
extern int stat(const char *, struct stat *);
extern int unlink(const char *);
-extern int pclose(FILE *);
#ifdef HAVE_SYMLINK
extern int symlink(const char *, const char *);
#endif /* HAVE_SYMLINK */
@@ -261,8 +255,6 @@ extern int lstat(const char *, struct stat *);
#include "osdefs.h"
#include <windows.h>
#include <shellapi.h> /* for ShellExecute() */
-#define popen _popen
-#define pclose _pclose
#endif /* _MSC_VER */
#if defined(PYCC_VACPP) && defined(PYOS_OS2)
@@ -3904,1470 +3896,6 @@ posix_plock(PyObject *self, PyObject *args)
#endif
-#ifdef HAVE_POPEN
-PyDoc_STRVAR(posix_popen__doc__,
-"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
-Open a pipe to/from a command returning a file object.");
-
-#if defined(PYOS_OS2)
-#if defined(PYCC_VACPP)
-static int
-async_system(const char *command)
-{
- char errormsg[256], args[1024];
- RESULTCODES rcodes;
- APIRET rc;
-
- char *shell = getenv("COMSPEC");
- if (!shell)
- shell = "cmd";
-
- /* avoid overflowing the argument buffer */
- if (strlen(shell) + 3 + strlen(command) >= 1024)
- return ERROR_NOT_ENOUGH_MEMORY
-
- args[0] = '\0';
- strcat(args, shell);
- strcat(args, "/c ");
- strcat(args, command);
-
- /* execute asynchronously, inheriting the environment */
- rc = DosExecPgm(errormsg,
- sizeof(errormsg),
- EXEC_ASYNC,
- args,
- NULL,
- &rcodes,
- shell);
- return rc;
-}
-
-static FILE *
-popen(const char *command, const char *mode, int pipesize, int *err)
-{
- int oldfd, tgtfd;
- HFILE pipeh[2];
- APIRET rc;
-
- /* mode determines which of stdin or stdout is reconnected to
- * the pipe to the child
- */
- if (strchr(mode, 'r') != NULL) {
- tgt_fd = 1; /* stdout */
- } else if (strchr(mode, 'w')) {
- tgt_fd = 0; /* stdin */
- } else {
- *err = ERROR_INVALID_ACCESS;
- return NULL;
- }
-
- /* setup the pipe */
- if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) {
- *err = rc;
- return NULL;
- }
-
- /* prevent other threads accessing stdio */
- DosEnterCritSec();
-
- /* reconnect stdio and execute child */
- oldfd = dup(tgtfd);
- close(tgtfd);
- if (dup2(pipeh[tgtfd], tgtfd) == 0) {
- DosClose(pipeh[tgtfd]);
- rc = async_system(command);
- }
-
- /* restore stdio */
- dup2(oldfd, tgtfd);
- close(oldfd);
-
- /* allow other threads access to stdio */
- DosExitCritSec();
-
- /* if execution of child was successful return file stream */
- if (rc == NO_ERROR)
- return fdopen(pipeh[1 - tgtfd], mode);
- else {
- DosClose(pipeh[1 - tgtfd]);
- *err = rc;
- return NULL;
- }
-}
-
-static PyObject *
-posix_popen(PyObject *self, PyObject *args)
-{
- char *name;
- char *mode = "r";
- int err, bufsize = -1;
- FILE *fp;
- PyObject *f;
- if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
- return NULL;
- Py_BEGIN_ALLOW_THREADS
- fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
- Py_END_ALLOW_THREADS
- if (fp == NULL)
- return os2_error(err);
-
- f = PyFile_FromFile(fp, name, mode, fclose);
- if (f != NULL)
- PyFile_SetBufSize(f, bufsize);
- return f;
-}
-
-#elif defined(PYCC_GCC)
-
-/* standard posix version of popen() support */
-static PyObject *
-posix_popen(PyObject *self, PyObject *args)
-{
- char *name;
- char *mode = "r";
- int bufsize = -1;
- FILE *fp;
- PyObject *f;
- if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
- return NULL;
- Py_BEGIN_ALLOW_THREADS
- fp = popen(name, mode);
- Py_END_ALLOW_THREADS
- if (fp == NULL)
- return posix_error();
- f = PyFile_FromFile(fp, name, mode, pclose);
- if (f != NULL)
- PyFile_SetBufSize(f, bufsize);
- return f;
-}
-
-/* fork() under OS/2 has lots'o'warts
- * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
- * most of this code is a ripoff of the win32 code, but using the
- * capabilities of EMX's C library routines
- */
-
-/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
-#define POPEN_1 1
-#define POPEN_2 2
-#define POPEN_3 3
-#define POPEN_4 4
-
-static PyObject *_PyPopen(char *, int, int, int);
-static int _PyPclose(FILE *file);
-
-/*
- * Internal dictionary mapping popen* file pointers to process handles,
- * for use when retrieving the process exit code. See _PyPclose() below
- * for more information on this dictionary's use.
- */
-static PyObject *_PyPopenProcs = NULL;
-
-/* os2emx version of popen2()
- *
- * The result of this function is a pipe (file) connected to the
- * process's stdin, and a pipe connected to the process's stdout.
- */
-
-static PyObject *
-os2emx_popen2(PyObject *self, PyObject *args)
-{
- PyObject *f;
- int tm=0;
-
- char *cmdstring;
- char *mode = "t";
- int bufsize = -1;
- if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
- return NULL;
-
- if (*mode == 't')
- tm = O_TEXT;
- else if (*mode != 'b') {
- PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
- return NULL;
- } else
- tm = O_BINARY;
-
- f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
-
- return f;
-}
-
-/*
- * Variation on os2emx.popen2
- *
- * The result of this function is 3 pipes - the process's stdin,
- * stdout and stderr
- */
-
-static PyObject *
-os2emx_popen3(PyObject *self, PyObject *args)
-{
- PyObject *f;
- int tm = 0;
-
- char *cmdstring;
- char *mode = "t";
- int bufsize = -1;
- if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
- return NULL;
-
- if (*mode == 't')
- tm = O_TEXT;
- else if (*mode != 'b') {
- PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
- return NULL;
- } else
- tm = O_BINARY;
-
- f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
-
- return f;
-}
-
-/*
- * Variation on os2emx.popen2
- *
- * The result of this function is 2 pipes - the processes stdin,
- * and stdout+stderr combined as a single pipe.
- */
-
-static PyObject *
-os2emx_popen4(PyObject *self, PyObject *args)
-{
- PyObject *f;
- int tm = 0;
-
- char *cmdstring;
- char *mode = "t";
- int bufsize = -1;
- if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
- return NULL;
-
- if (*mode == 't')
- tm = O_TEXT;
- else if (*mode != 'b') {
- PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
- return NULL;
- } else
- tm = O_BINARY;
-
- f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
-
- return f;
-}
-
-/* a couple of structures for convenient handling of multiple
- * file handles and pipes
- */
-struct file_ref
-{
- int handle;
- int flags;
-};
-
-struct pipe_ref
-{
- int rd;
- int wr;
-};
-
-/* The following code is derived from the win32 code */
-
-static PyObject *
-_PyPopen(char *cmdstring, int mode, int n, int bufsize)
-{
- struct file_ref stdio[3];
- struct pipe_ref p_fd[3];
- FILE *p_s[3];
- int file_count, i, pipe_err, pipe_pid;
- char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
- PyObject *f, *p_f[3];
-
- /* file modes for subsequent fdopen's on pipe handles */
- if (mode == O_TEXT)
- {
- rd_mode = "rt";
- wr_mode = "wt";
- }
- else
- {
- rd_mode = "rb";
- wr_mode = "wb";
- }
-
- /* prepare shell references */
- if ((shell = getenv("EMXSHELL")) == NULL)
- if ((shell = getenv("COMSPEC")) == NULL)
- {
- errno = ENOENT;
- return posix_error();
- }
-
- sh_name = _getname(shell);
- if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
- opt = "/c";
- else
- opt = "-c";
-
- /* save current stdio fds + their flags, and set not inheritable */
- i = pipe_err = 0;
- while (pipe_err >= 0 && i < 3)
- {
- pipe_err = stdio[i].handle = dup(i);
- stdio[i].flags = fcntl(i, F_GETFD, 0);
- fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
- i++;
- }
- if (pipe_err < 0)
- {
- /* didn't get them all saved - clean up and bail out */
- int saved_err = errno;
- while (i-- > 0)
- {
- close(stdio[i].handle);
- }
- errno = saved_err;
- return posix_error();
- }
-
- /* create pipe ends */
- file_count = 2;
- if (n == POPEN_3)
- file_count = 3;
- i = pipe_err = 0;
- while ((pipe_err == 0) && (i < file_count))
- pipe_err = pipe((int *)&p_fd[i++]);
- if (pipe_err < 0)
- {
- /* didn't get them all made - clean up and bail out */
- while (i-- > 0)
- {
- close(p_fd[i].wr);
- close(p_fd[i].rd);
- }
- errno = EPIPE;
- return posix_error();
- }
-
- /* change the actual standard IO streams over temporarily,
- * making the retained pipe ends non-inheritable
- */
- pipe_err = 0;
-
- /* - stdin */
- if (dup2(p_fd[0].rd, 0) == 0)
- {
- close(p_fd[0].rd);
- i = fcntl(p_fd[0].wr, F_GETFD, 0);
- fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
- if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
- {
- close(p_fd[0].wr);
- pipe_err = -1;
- }
- }
- else
- {
- pipe_err = -1;
- }
-
- /* - stdout */
- if (pipe_err == 0)
- {
- if (dup2(p_fd[1].wr, 1) == 1)
- {
- close(p_fd[1].wr);
- i = fcntl(p_fd[1].rd, F_GETFD, 0);
- fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
- if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
- {
- close(p_fd[1].rd);
- pipe_err = -1;
- }
- }
- else
- {
- pipe_err = -1;
- }
- }
-
- /* - stderr, as required */
- if (pipe_err == 0)
- switch (n)
- {
- case POPEN_3:
- {
- if (dup2(p_fd[2].wr, 2) == 2)
- {
- close(p_fd[2].wr);
- i = fcntl(p_fd[2].rd, F_GETFD, 0);
- fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
- if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
- {
- close(p_fd[2].rd);
- pipe_err = -1;
- }
- }
- else
- {
- pipe_err = -1;
- }
- break;
- }
-
- case POPEN_4:
- {
- if (dup2(1, 2) != 2)
- {
- pipe_err = -1;
- }
- break;
- }
- }
-
- /* spawn the child process */
- if (pipe_err == 0)
- {
- pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
- if (pipe_pid == -1)
- {
- pipe_err = -1;
- }
- else
- {
- /* save the PID into the FILE structure
- * NOTE: this implementation doesn't actually
- * take advantage of this, but do it for
- * completeness - AIM Apr01
- */
- for (i = 0; i < file_count; i++)
- p_s[i]->_pid = pipe_pid;
- }
- }
-
- /* reset standard IO to normal */
- for (i = 0; i < 3; i++)
- {
- dup2(stdio[i].handle, i);
- fcntl(i, F_SETFD, stdio[i].flags);
- close(stdio[i].handle);
- }
-
- /* if any remnant problems, clean up and bail out */
- if (pipe_err < 0)
- {
- for (i = 0; i < 3; i++)
- {
- close(p_fd[i].rd);
- close(p_fd[i].wr);
- }
- errno = EPIPE;
- return posix_error_with_filename(cmdstring);
- }
-
- /* build tuple of file objects to return */
- if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
- PyFile_SetBufSize(p_f[0], bufsize);
- if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
- PyFile_SetBufSize(p_f[1], bufsize);
- if (n == POPEN_3)
- {
- if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
- PyFile_SetBufSize(p_f[0], bufsize);
- f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
- }
- else
- f = PyTuple_Pack(2, p_f[0], p_f[1]);
-
- /*
- * Insert the files we've created into the process dictionary
- * all referencing the list with the process handle and the
- * initial number of files (see description below in _PyPclose).
- * Since if _PyPclose later tried to wait on a process when all
- * handles weren't closed, it could create a deadlock with the
- * child, we spend some energy here to try to ensure that we
- * either insert all file handles into the dictionary or none
- * at all. It's a little clumsy with the various popen modes
- * and variable number of files involved.
- */
- if (!_PyPopenProcs)
- {
- _PyPopenProcs = PyDict_New();
- }
-
- if (_PyPopenProcs)
- {
- PyObject *procObj, *pidObj, *intObj, *fileObj[3];
- int ins_rc[3];
-
- fileObj[0] = fileObj[1] = fileObj[2] = NULL;
- ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
-
- procObj = PyList_New(2);
- pidObj = PyInt_FromLong((long) pipe_pid);
- intObj = PyInt_FromLong((long) file_count);
-
- if (procObj && pidObj && intObj)
- {
- PyList_SetItem(procObj, 0, pidObj);
- PyList_SetItem(procObj, 1, intObj);
-
- fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
- if (fileObj[0])
- {
- ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
- fileObj[0],
- procObj);
- }
- fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
- if (fileObj[1])
- {
- ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
- fileObj[1],
- procObj);
- }
- if (file_count >= 3)
- {
- fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
- if (fileObj[2])
- {
- ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
- fileObj[2],
- procObj);
- }
- }
-
- if (ins_rc[0] < 0 || !fileObj[0] ||
- ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
- ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
- {
- /* Something failed - remove any dictionary
- * entries that did make it.
- */
- if (!ins_rc[0] && fileObj[0])
- {
- PyDict_DelItem(_PyPopenProcs,
- fileObj[0]);
- }
- if (!ins_rc[1] && fileObj[1])
- {
- PyDict_DelItem(_PyPopenProcs,
- fileObj[1]);
- }
- if (!ins_rc[2] && fileObj[2])
- {
- PyDict_DelItem(_PyPopenProcs,
- fileObj[2]);
- }
- }
- }
-
- /*
- * Clean up our localized references for the dictionary keys
- * and value since PyDict_SetItem will Py_INCREF any copies
- * that got placed in the dictionary.
- */
- Py_XDECREF(procObj);
- Py_XDECREF(fileObj[0]);
- Py_XDECREF(fileObj[1]);
- Py_XDECREF(fileObj[2]);
- }
-
- /* Child is launched. */
- return f;
-}
-
-/*
- * Wrapper for fclose() to use for popen* files, so we can retrieve the
- * exit code for the child process and return as a result of the close.
- *
- * This function uses the _PyPopenProcs dictionary in order to map the
- * input file pointer to information about the process that was
- * originally created by the popen* call that created the file pointer.
- * The dictionary uses the file pointer as a key (with one entry
- * inserted for each file returned by the original popen* call) and a
- * single list object as the value for all files from a single call.
- * The list object contains the Win32 process handle at [0], and a file
- * count at [1], which is initialized to the total number of file
- * handles using that list.
- *
- * This function closes whichever handle it is passed, and decrements
- * the file count in the dictionary for the process handle pointed to
- * by this file. On the last close (when the file count reaches zero),
- * this function will wait for the child process and then return its
- * exit code as the result of the close() operation. This permits the
- * files to be closed in any order - it is always the close() of the
- * final handle that will return the exit code.
- *
- * NOTE: This function is currently called with the GIL released.
- * hence we use the GILState API to manage our state.
- */
-
-static int _PyPclose(FILE *file)
-{
- int result;
- int exit_code;
- int pipe_pid;
- PyObject *procObj, *pidObj, *intObj, *fileObj;
- int file_count;
-#ifdef WITH_THREAD
- PyGILState_STATE state;
-#endif
-
- /* Close the file handle first, to ensure it can't block the
- * child from exiting if it's the last handle.
- */
- result = fclose(file);
-
-#ifdef WITH_THREAD
- state = PyGILState_Ensure();
-#endif
- if (_PyPopenProcs)
- {
- if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
- (procObj = PyDict_GetItem(_PyPopenProcs,
- fileObj)) != NULL &&
- (pidObj = PyList_GetItem(procObj,0)) != NULL &&
- (intObj = PyList_GetItem(procObj,1)) != NULL)
- {
- pipe_pid = (int) PyInt_AsLong(pidObj);
- file_count = (int) PyInt_AsLong(intObj);
-
- if (file_count > 1)
- {
- /* Still other files referencing process */
- file_count--;
- PyList_SetItem(procObj,1,
- PyInt_FromLong((long) file_count));
- }
- else
- {
- /* Last file for this process */
- if (result != EOF &&
- waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
- {
- /* extract exit status */
- if (WIFEXITED(exit_code))
- {
- result = WEXITSTATUS(exit_code);
- }
- else
- {
- errno = EPIPE;
- result = -1;
- }
- }
- else
- {
- /* Indicate failure - this will cause the file object
- * to raise an I/O error and translate the last
- * error code from errno. We do have a problem with
- * last errors that overlap the normal errno table,
- * but that's a consistent problem with the file object.
- */
- result = -1;
- }
- }
-
- /* Remove this file pointer from dictionary */
- PyDict_DelItem(_PyPopenProcs, fileObj);
-
- if (PyDict_Size(_PyPopenProcs) == 0)
- {
- Py_DECREF(_PyPopenProcs);
- _PyPopenProcs = NULL;
- }
-
- } /* if object retrieval ok */
-
- Py_XDECREF(fileObj);
- } /* if _PyPopenProcs */
-
-#ifdef WITH_THREAD
- PyGILState_Release(state);
-#endif
- return result;
-}
-
-#endif /* PYCC_??? */
-
-#elif defined(MS_WINDOWS)
-
-/*
- * Portable 'popen' replacement for Win32.
- *
- * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
- * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
- * Return code handling by David Bolen <db3l@fitlinxx.com>.
- */
-
-#include <malloc.h>
-#include <io.h>
-#include <fcntl.h>
-
-/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
-#define POPEN_1 1
-#define POPEN_2 2
-#define POPEN_3 3
-#define POPEN_4 4
-
-static PyObject *_PyPopen(char *, int, int);
-static int _PyPclose(FILE *file);
-
-/*
- * Internal dictionary mapping popen* file pointers to process handles,
- * for use when retrieving the process exit code. See _PyPclose() below
- * for more information on this dictionary's use.
- */
-static PyObject *_PyPopenProcs = NULL;
-
-
-/* popen that works from a GUI.
- *
- * The result of this function is a pipe (file) connected to the
- * processes stdin or stdout, depending on the requested mode.
- */
-
-static PyObject *
-posix_popen(PyObject *self, PyObject *args)
-{
- PyObject *f;
- int tm = 0;
-
- char *cmdstring;
- char *mode = "r";
- int bufsize = -1;
- if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
- return NULL;
-
- if (*mode == 'r')
- tm = _O_RDONLY;
- else if (*mode != 'w') {
- PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
- return NULL;
- } else
- tm = _O_WRONLY;
-
- if (bufsize != -1) {
- PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
- return NULL;
- }
-
- if (*(mode+1) == 't')
- f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
- else if (*(mode+1) == 'b')
- f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
- else
- f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
-
- return f;
-}
-
-/* Variation on win32pipe.popen
- *
- * The result of this function is a pipe (file) connected to the
- * process's stdin, and a pipe connected to the process's stdout.
- */
-
-static PyObject *
-win32_popen2(PyObject *self, PyObject *args)
-{
- PyObject *f;
- int tm=0;
-
- char *cmdstring;
- char *mode = "t";
- int bufsize = -1;
- if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
- return NULL;
-
- if (*mode == 't')
- tm = _O_TEXT;
- else if (*mode != 'b') {
- PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
- return NULL;
- } else
- tm = _O_BINARY;
-
- if (bufsize != -1) {
- PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
- return NULL;
- }
-
- f = _PyPopen(cmdstring, tm, POPEN_2);
-
- return f;
-}
-
-/*
- * Variation on <om win32pipe.popen>
- *
- * The result of this function is 3 pipes - the process's stdin,
- * stdout and stderr
- */
-
-static PyObject *
-win32_popen3(PyObject *self, PyObject *args)
-{
- PyObject *f;
- int tm = 0;
-
- char *cmdstring;
- char *mode = "t";
- int bufsize = -1;
- if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
- return NULL;
-
- if (*mode == 't')
- tm = _O_TEXT;
- else if (*mode != 'b') {
- PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
- return NULL;
- } else
- tm = _O_BINARY;
-
- if (bufsize != -1) {
- PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
- return NULL;
- }
-
- f = _PyPopen(cmdstring, tm, POPEN_3);
-
- return f;
-}
-
-/*
- * Variation on win32pipe.popen
- *
- * The result of this function is 2 pipes - the processes stdin,
- * and stdout+stderr combined as a single pipe.
- */
-
-static PyObject *
-win32_popen4(PyObject *self, PyObject *args)
-{
- PyObject *f;
- int tm = 0;
-
- char *cmdstring;
- char *mode = "t";
- int bufsize = -1;
- if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
- return NULL;
-
- if (*mode == 't')
- tm = _O_TEXT;
- else if (*mode != 'b') {
- PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
- return NULL;
- } else
- tm = _O_BINARY;
-
- if (bufsize != -1) {
- PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
- return NULL;
- }
-
- f = _PyPopen(cmdstring, tm, POPEN_4);
-
- return f;
-}
-
-static BOOL
-_PyPopenCreateProcess(char *cmdstring,
- HANDLE hStdin,
- HANDLE hStdout,
- HANDLE hStderr,
- HANDLE *hProcess)
-{
- PROCESS_INFORMATION piProcInfo;
- STARTUPINFO siStartInfo;
- DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
- char *s1,*s2, *s3 = " /c ";
- const char *szConsoleSpawn = "w9xpopen.exe";
- int i;
- Py_ssize_t x;
-
- if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
- char *comshell;
-
- s1 = (char *)alloca(i);
- if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
- /* x < i, so x fits into an integer */
- return (int)x;
-
- /* Explicitly check if we are using COMMAND.COM. If we are
- * then use the w9xpopen hack.
- */
- comshell = s1 + x;
- while (comshell >= s1 && *comshell != '\\')
- --comshell;
- ++comshell;
-
- if (GetVersion() < 0x80000000 &&
- _stricmp(comshell, "command.com") != 0) {
- /* NT/2000 and not using command.com. */
- x = i + strlen(s3) + strlen(cmdstring) + 1;
- s2 = (char *)alloca(x);
- ZeroMemory(s2, x);
- PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
- }
- else {
- /*
- * Oh gag, we're on Win9x or using COMMAND.COM. Use
- * the workaround listed in KB: Q150956
- */
- char modulepath[_MAX_PATH];
- struct stat statinfo;
- GetModuleFileName(NULL, modulepath, sizeof(modulepath));
- for (x = i = 0; modulepath[i]; i++)
- if (modulepath[i] == SEP)
- x = i+1;
- modulepath[x] = '\0';
- /* Create the full-name to w9xpopen, so we can test it exists */
- strncat(modulepath,
- szConsoleSpawn,
- (sizeof(modulepath)/sizeof(modulepath[0]))
- -strlen(modulepath));
- if (stat(modulepath, &statinfo) != 0) {
- size_t mplen = sizeof(modulepath)/sizeof(modulepath[0]);
- /* Eeek - file-not-found - possibly an embedding
- situation - see if we can locate it in sys.prefix
- */
- strncpy(modulepath,
- Py_GetExecPrefix(),
- mplen);
- modulepath[mplen-1] = '\0';
- if (modulepath[strlen(modulepath)-1] != '\\')
- strcat(modulepath, "\\");
- strncat(modulepath,
- szConsoleSpawn,
- mplen-strlen(modulepath));
- /* No where else to look - raise an easily identifiable
- error, rather than leaving Windows to report
- "file not found" - as the user is probably blissfully
- unaware this shim EXE is used, and it will confuse them.
- (well, it confused me for a while ;-)
- */
- if (stat(modulepath, &statinfo) != 0) {
- PyErr_Format(PyExc_RuntimeError,
- "Can not locate '%s' which is needed "
- "for popen to work with your shell "
- "or platform.",
- szConsoleSpawn);
- return FALSE;
- }
- }
- x = i + strlen(s3) + strlen(cmdstring) + 1 +
- strlen(modulepath) +
- strlen(szConsoleSpawn) + 1;
-
- s2 = (char *)alloca(x);
- ZeroMemory(s2, x);
- /* To maintain correct argument passing semantics,
- we pass the command-line as it stands, and allow
- quoting to be applied. w9xpopen.exe will then
- use its argv vector, and re-quote the necessary
- args for the ultimate child process.
- */
- PyOS_snprintf(
- s2, x,
- "\"%s\" %s%s%s",
- modulepath,
- s1,
- s3,
- cmdstring);
- /* Not passing CREATE_NEW_CONSOLE has been known to
- cause random failures on win9x. Specifically a
- dialog:
- "Your program accessed mem currently in use at xxx"
- and a hopeful warning about the stability of your
- system.
- Cost is Ctrl+C wont kill children, but anyone
- who cares can have a go!
- */
- dwProcessFlags |= CREATE_NEW_CONSOLE;
- }
- }
-
- /* Could be an else here to try cmd.exe / command.com in the path
- Now we'll just error out.. */
- else {
- PyErr_SetString(PyExc_RuntimeError,
- "Cannot locate a COMSPEC environment variable to "
- "use as the shell");
- return FALSE;
- }
-
- ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
- siStartInfo.cb = sizeof(STARTUPINFO);
- siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
- siStartInfo.hStdInput = hStdin;
- siStartInfo.hStdOutput = hStdout;
- siStartInfo.hStdError = hStderr;
- siStartInfo.wShowWindow = SW_HIDE;
-
- if (CreateProcess(NULL,
- s2,
- NULL,
- NULL,
- TRUE,
- dwProcessFlags,
- NULL,
- NULL,
- &siStartInfo,
- &piProcInfo) ) {
- /* Close the handles now so anyone waiting is woken. */
- CloseHandle(piProcInfo.hThread);
-
- /* Return process handle */
- *hProcess = piProcInfo.hProcess;
- return TRUE;
- }
- win32_error("CreateProcess", s2);
- return FALSE;
-}
-
-/* The following code is based off of KB: Q190351 */
-
-static PyObject *
-_PyPopen(char *cmdstring, int mode, int n)
-{
- HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
- hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
- hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
-
- SECURITY_ATTRIBUTES saAttr;
- BOOL fSuccess;
- int fd1, fd2, fd3;
- FILE *f1, *f2, *f3;
- long file_count;
- PyObject *f;
-
- saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
- saAttr.bInheritHandle = TRUE;
- saAttr.lpSecurityDescriptor = NULL;
-
- if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
- return win32_error("CreatePipe", NULL);
-
- /* Create new output read handle and the input write handle. Set
- * the inheritance properties to FALSE. Otherwise, the child inherits
- * these handles; resulting in non-closeable handles to the pipes
- * being created. */
- fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
- GetCurrentProcess(), &hChildStdinWrDup, 0,
- FALSE,
- DUPLICATE_SAME_ACCESS);
- if (!fSuccess)
- return win32_error("DuplicateHandle", NULL);
-
- /* Close the inheritable version of ChildStdin
- that we're using. */
- CloseHandle(hChildStdinWr);
-
- if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
- return win32_error("CreatePipe", NULL);
-
- fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
- GetCurrentProcess(), &hChildStdoutRdDup, 0,
- FALSE, DUPLICATE_SAME_ACCESS);
- if (!fSuccess)
- return win32_error("DuplicateHandle", NULL);
-
- /* Close the inheritable version of ChildStdout
- that we're using. */
- CloseHandle(hChildStdoutRd);
-
- if (n != POPEN_4) {
- if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
- return win32_error("CreatePipe", NULL);
- fSuccess = DuplicateHandle(GetCurrentProcess(),
- hChildStderrRd,
- GetCurrentProcess(),
- &hChildStderrRdDup, 0,
- FALSE, DUPLICATE_SAME_ACCESS);
- if (!fSuccess)
- return win32_error("DuplicateHandle", NULL);
- /* Close the inheritable version of ChildStdErr that we're using. */
- CloseHandle(hChildStderrRd);
- }
-
- switch (n) {
- case POPEN_1:
- switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
- case _O_WRONLY | _O_TEXT:
- /* Case for writing to child Stdin in text mode. */
- fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
- f1 = _fdopen(fd1, "w");
- f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
- PyFile_SetBufSize(f, 0);
- /* We don't care about these pipes anymore, so close them. */
- CloseHandle(hChildStdoutRdDup);
- CloseHandle(hChildStderrRdDup);
- break;
-
- case _O_RDONLY | _O_TEXT:
- /* Case for reading from child Stdout in text mode. */
- fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
- f1 = _fdopen(fd1, "r");
- f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
- PyFile_SetBufSize(f, 0);
- /* We don't care about these pipes anymore, so close them. */
- CloseHandle(hChildStdinWrDup);
- CloseHandle(hChildStderrRdDup);
- break;
-
- case _O_RDONLY | _O_BINARY:
- /* Case for readinig from child Stdout in binary mode. */
- fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
- f1 = _fdopen(fd1, "rb");
- f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
- PyFile_SetBufSize(f, 0);
- /* We don't care about these pipes anymore, so close them. */
- CloseHandle(hChildStdinWrDup);
- CloseHandle(hChildStderrRdDup);
- break;
-
- case _O_WRONLY | _O_BINARY:
- /* Case for writing to child Stdin in binary mode. */
- fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
- f1 = _fdopen(fd1, "wb");
- f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
- PyFile_SetBufSize(f, 0);
- /* We don't care about these pipes anymore, so close them. */
- CloseHandle(hChildStdoutRdDup);
- CloseHandle(hChildStderrRdDup);
- break;
- }
- file_count = 1;
- break;
-
- case POPEN_2:
- case POPEN_4:
- {
- char *m1, *m2;
- PyObject *p1, *p2;
-
- if (mode & _O_TEXT) {
- m1 = "r";
- m2 = "w";
- } else {
- m1 = "rb";
- m2 = "wb";
- }
-
- fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
- f1 = _fdopen(fd1, m2);
- fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
- f2 = _fdopen(fd2, m1);
- p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
- PyFile_SetBufSize(p1, 0);
- p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
- PyFile_SetBufSize(p2, 0);
-
- if (n != 4)
- CloseHandle(hChildStderrRdDup);
-
- f = PyTuple_Pack(2,p1,p2);
- Py_XDECREF(p1);
- Py_XDECREF(p2);
- file_count = 2;
- break;
- }
-
- case POPEN_3:
- {
- char *m1, *m2;
- PyObject *p1, *p2, *p3;
-
- if (mode & _O_TEXT) {
- m1 = "r";
- m2 = "w";
- } else {
- m1 = "rb";
- m2 = "wb";
- }
-
- fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
- f1 = _fdopen(fd1, m2);
- fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
- f2 = _fdopen(fd2, m1);
- fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode);
- f3 = _fdopen(fd3, m1);
- p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
- p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
- p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
- PyFile_SetBufSize(p1, 0);
- PyFile_SetBufSize(p2, 0);
- PyFile_SetBufSize(p3, 0);
- f = PyTuple_Pack(3,p1,p2,p3);
- Py_XDECREF(p1);
- Py_XDECREF(p2);
- Py_XDECREF(p3);
- file_count = 3;
- break;
- }
- }
-
- if (n == POPEN_4) {
- if (!_PyPopenCreateProcess(cmdstring,
- hChildStdinRd,
- hChildStdoutWr,
- hChildStdoutWr,
- &hProcess))
- return NULL;
- }
- else {
- if (!_PyPopenCreateProcess(cmdstring,
- hChildStdinRd,
- hChildStdoutWr,
- hChildStderrWr,
- &hProcess))
- return NULL;
- }
-
- /*
- * Insert the files we've created into the process dictionary
- * all referencing the list with the process handle and the
- * initial number of files (see description below in _PyPclose).
- * Since if _PyPclose later tried to wait on a process when all
- * handles weren't closed, it could create a deadlock with the
- * child, we spend some energy here to try to ensure that we
- * either insert all file handles into the dictionary or none
- * at all. It's a little clumsy with the various popen modes
- * and variable number of files involved.
- */
- if (!_PyPopenProcs) {
- _PyPopenProcs = PyDict_New();
- }
-
- if (_PyPopenProcs) {
- PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
- int ins_rc[3];
-
- fileObj[0] = fileObj[1] = fileObj[2] = NULL;
- ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
-
- procObj = PyList_New(2);
- hProcessObj = PyLong_FromVoidPtr(hProcess);
- intObj = PyInt_FromLong(file_count);
-
- if (procObj && hProcessObj && intObj) {
- PyList_SetItem(procObj,0,hProcessObj);
- PyList_SetItem(procObj,1,intObj);
-
- fileObj[0] = PyLong_FromVoidPtr(f1);
- if (fileObj[0]) {
- ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
- fileObj[0],
- procObj);
- }
- if (file_count >= 2) {
- fileObj[1] = PyLong_FromVoidPtr(f2);
- if (fileObj[1]) {
- ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
- fileObj[1],
- procObj);
- }
- }
- if (file_count >= 3) {
- fileObj[2] = PyLong_FromVoidPtr(f3);
- if (fileObj[2]) {
- ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
- fileObj[2],
- procObj);
- }
- }
-
- if (ins_rc[0] < 0 || !fileObj[0] ||
- ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
- ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
- /* Something failed - remove any dictionary
- * entries that did make it.
- */
- if (!ins_rc[0] && fileObj[0]) {
- PyDict_DelItem(_PyPopenProcs,
- fileObj[0]);
- }
- if (!ins_rc[1] && fileObj[1]) {
- PyDict_DelItem(_PyPopenProcs,
- fileObj[1]);
- }
- if (!ins_rc[2] && fileObj[2]) {
- PyDict_DelItem(_PyPopenProcs,
- fileObj[2]);
- }
- }
- }
-
- /*
- * Clean up our localized references for the dictionary keys
- * and value since PyDict_SetItem will Py_INCREF any copies
- * that got placed in the dictionary.
- */
- Py_XDECREF(procObj);
- Py_XDECREF(fileObj[0]);
- Py_XDECREF(fileObj[1]);
- Py_XDECREF(fileObj[2]);
- }
-
- /* Child is launched. Close the parents copy of those pipe
- * handles that only the child should have open. You need to
- * make sure that no handles to the write end of the output pipe
- * are maintained in this process or else the pipe will not close
- * when the child process exits and the ReadFile will hang. */
-
- if (!CloseHandle(hChildStdinRd))
- return win32_error("CloseHandle", NULL);
-
- if (!CloseHandle(hChildStdoutWr))
- return win32_error("CloseHandle", NULL);
-
- if ((n != 4) && (!CloseHandle(hChildStderrWr)))
- return win32_error("CloseHandle", NULL);
-
- return f;
-}
-
-/*
- * Wrapper for fclose() to use for popen* files, so we can retrieve the
- * exit code for the child process and return as a result of the close.
- *
- * This function uses the _PyPopenProcs dictionary in order to map the
- * input file pointer to information about the process that was
- * originally created by the popen* call that created the file pointer.
- * The dictionary uses the file pointer as a key (with one entry
- * inserted for each file returned by the original popen* call) and a
- * single list object as the value for all files from a single call.
- * The list object contains the Win32 process handle at [0], and a file
- * count at [1], which is initialized to the total number of file
- * handles using that list.
- *
- * This function closes whichever handle it is passed, and decrements
- * the file count in the dictionary for the process handle pointed to
- * by this file. On the last close (when the file count reaches zero),
- * this function will wait for the child process and then return its
- * exit code as the result of the close() operation. This permits the
- * files to be closed in any order - it is always the close() of the
- * final handle that will return the exit code.
- *
- * NOTE: This function is currently called with the GIL released.
- * hence we use the GILState API to manage our state.
- */
-
-static int _PyPclose(FILE *file)
-{
- int result;
- DWORD exit_code;
- HANDLE hProcess;
- PyObject *procObj, *hProcessObj, *intObj, *fileObj;
- long file_count;
-#ifdef WITH_THREAD
- PyGILState_STATE state;
-#endif
-
- /* Close the file handle first, to ensure it can't block the
- * child from exiting if it's the last handle.
- */
- result = fclose(file);
-#ifdef WITH_THREAD
- state = PyGILState_Ensure();
-#endif
- if (_PyPopenProcs) {
- if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
- (procObj = PyDict_GetItem(_PyPopenProcs,
- fileObj)) != NULL &&
- (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
- (intObj = PyList_GetItem(procObj,1)) != NULL) {
-
- hProcess = PyLong_AsVoidPtr(hProcessObj);
- file_count = PyInt_AsLong(intObj);
-
- if (file_count > 1) {
- /* Still other files referencing process */
- file_count--;
- PyList_SetItem(procObj,1,
- PyInt_FromLong(file_count));
- } else {
- /* Last file for this process */
- if (result != EOF &&
- WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
- GetExitCodeProcess(hProcess, &exit_code)) {
- /* Possible truncation here in 16-bit environments, but
- * real exit codes are just the lower byte in any event.
- */
- result = exit_code;
- } else {
- /* Indicate failure - this will cause the file object
- * to raise an I/O error and translate the last Win32
- * error code from errno. We do have a problem with
- * last errors that overlap the normal errno table,
- * but that's a consistent problem with the file object.
- */
- if (result != EOF) {
- /* If the error wasn't from the fclose(), then
- * set errno for the file object error handling.
- */
- errno = GetLastError();
- }
- result = -1;
- }
-
- /* Free up the native handle at this point */
- CloseHandle(hProcess);
- }
-
- /* Remove this file pointer from dictionary */
- PyDict_DelItem(_PyPopenProcs, fileObj);
-
- if (PyDict_Size(_PyPopenProcs) == 0) {
- Py_DECREF(_PyPopenProcs);
- _PyPopenProcs = NULL;
- }
-
- } /* if object retrieval ok */
-
- Py_XDECREF(fileObj);
- } /* if _PyPopenProcs */
-
-#ifdef WITH_THREAD
- PyGILState_Release(state);
-#endif
- return result;
-}
-
-#else /* which OS? */
-static PyObject *
-posix_popen(PyObject *self, PyObject *args)
-{
- char *name;
- char *mode = "r";
- int bufsize = -1;
- FILE *fp;
- PyObject *f;
- if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
- return NULL;
- /* Strip mode of binary or text modifiers */
- if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
- mode = "r";
- else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
- mode = "w";
- Py_BEGIN_ALLOW_THREADS
- fp = popen(name, mode);
- Py_END_ALLOW_THREADS
- if (fp == NULL)
- return posix_error();
- f = PyFile_FromFile(fp, name, mode, pclose);
- if (f != NULL)
- PyFile_SetBufSize(f, bufsize);
- return f;
-}
-
-#endif /* PYOS_??? */
-#endif /* HAVE_POPEN */
#ifdef HAVE_SETUID
@@ -6225,62 +4753,6 @@ posix_fstat(PyObject *self, PyObject *args)
return _pystat_fromstructstat(&st);
}
-
-PyDoc_STRVAR(posix_fdopen__doc__,
-"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
-Return an open file object connected to a file descriptor.");
-
-static PyObject *
-posix_fdopen(PyObject *self, PyObject *args)
-{
- int fd;
- char *orgmode = "r";
- int bufsize = -1;
- FILE *fp;
- PyObject *f;
- char *mode;
- if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize))
- return NULL;
-
- /* Sanitize mode. See fileobject.c */
- mode = PyMem_MALLOC(strlen(orgmode)+3);
- if (!mode) {
- PyErr_NoMemory();
- return NULL;
- }
- strcpy(mode, orgmode);
- if (_PyFile_SanitizeMode(mode)) {
- PyMem_FREE(mode);
- return NULL;
- }
- Py_BEGIN_ALLOW_THREADS
-#if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
- if (mode[0] == 'a') {
- /* try to make sure the O_APPEND flag is set */
- int flags;
- flags = fcntl(fd, F_GETFL);
- if (flags != -1)
- fcntl(fd, F_SETFL, flags | O_APPEND);
- fp = fdopen(fd, mode);
- if (fp == NULL && flags != -1)
- /* restore old mode if fdopen failed */
- fcntl(fd, F_SETFL, flags);
- } else {
- fp = fdopen(fd, mode);
- }
-#else
- fp = fdopen(fd, mode);
-#endif
- Py_END_ALLOW_THREADS
- PyMem_FREE(mode);
- if (fp == NULL)
- return posix_error();
- f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose);
- if (f != NULL)
- PyFile_SetBufSize(f, bufsize);
- return f;
-}
-
PyDoc_STRVAR(posix_isatty__doc__,
"isatty(fd) -> bool\n\n\
Return True if the file descriptor 'fd' is an open file descriptor\n\
@@ -8265,21 +6737,6 @@ static PyMethodDef posix_methods[] = {
#ifdef HAVE_PLOCK
{"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
#endif /* HAVE_PLOCK */
-#ifdef HAVE_POPEN
- {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
-#ifdef MS_WINDOWS
- {"popen2", win32_popen2, METH_VARARGS},
- {"popen3", win32_popen3, METH_VARARGS},
- {"popen4", win32_popen4, METH_VARARGS},
- {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
-#else
-#if defined(PYOS_OS2) && defined(PYCC_GCC)
- {"popen2", os2emx_popen2, METH_VARARGS},
- {"popen3", os2emx_popen3, METH_VARARGS},
- {"popen4", os2emx_popen4, METH_VARARGS},
-#endif
-#endif
-#endif /* HAVE_POPEN */
#ifdef HAVE_SETUID
{"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
#endif /* HAVE_SETUID */
@@ -8342,7 +6799,6 @@ static PyMethodDef posix_methods[] = {
{"read", posix_read, METH_VARARGS, posix_read__doc__},
{"write", posix_write, METH_VARARGS, posix_write__doc__},
{"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
- {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
{"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
#ifdef HAVE_PIPE
{"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},