diff options
author | Hynek Schlawack <hs@ox.cx> | 2012-06-21 18:58:31 (GMT) |
---|---|---|
committer | Hynek Schlawack <hs@ox.cx> | 2012-06-21 18:58:31 (GMT) |
commit | 69168354c254f72b7a13d35aff8bb02752d00d26 (patch) | |
tree | d0ca31048f26181e9cba9a8ea1c1fd69fa2ae70c /Modules/_io | |
parent | a759d4e9f40fd0b757600d5fd3666cdddee113fe (diff) | |
parent | 9ed8b4e4cac4f38109c3e08a8bfd19b7f6ad1c88 (diff) | |
download | cpython-69168354c254f72b7a13d35aff8bb02752d00d26.zip cpython-69168354c254f72b7a13d35aff8bb02752d00d26.tar.gz cpython-69168354c254f72b7a13d35aff8bb02752d00d26.tar.bz2 |
#10053: Don't close FDs when FileIO.__init__ fails
Loosely based on the work by Hirokazu Yamamoto.
Diffstat (limited to 'Modules/_io')
-rw-r--r-- | Modules/_io/fileio.c | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c index 726d17b..31e2994 100644 --- a/Modules/_io/fileio.c +++ b/Modules/_io/fileio.c @@ -227,6 +227,7 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds) int flags = 0; int fd = -1; int closefd = 1; + int fd_is_own = 0; assert(PyFileIO_Check(oself)); if (self->fd >= 0) { @@ -376,6 +377,7 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds) #endif self->fd = open(name, flags, 0666); Py_END_ALLOW_THREADS + fd_is_own = 1; } else { PyObject *fdobj = PyObject_CallFunction( opener, "Oi", nameobj, flags); @@ -393,6 +395,7 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds) if (self->fd == -1) { goto error; } + fd_is_own = 1; } if (self->fd < 0) { @@ -421,13 +424,8 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds) end of file (otherwise, it might be done only on the first write()). */ PyObject *pos = portable_lseek(self->fd, NULL, 2); - if (pos == NULL) { - if (closefd) { - close(self->fd); - self->fd = -1; - } + if (pos == NULL) goto error; - } Py_DECREF(pos); } @@ -435,6 +433,8 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds) error: ret = -1; + if (!fd_is_own) + self->fd = -1; if (self->fd >= 0) internal_close(self); |