summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorHynek Schlawack <hs@ox.cx>2012-06-21 17:45:19 (GMT)
committerHynek Schlawack <hs@ox.cx>2012-06-21 17:45:19 (GMT)
commit9bd4bf2a3d0426ee3830e89854ecf4bea03ac830 (patch)
treef75e9f2cb4374fd0e4ce88578e0b2552876ace70 /Modules
parent4215d2738a44be9eca09eb7b48c31ed805c8fb60 (diff)
downloadcpython-9bd4bf2a3d0426ee3830e89854ecf4bea03ac830.zip
cpython-9bd4bf2a3d0426ee3830e89854ecf4bea03ac830.tar.gz
cpython-9bd4bf2a3d0426ee3830e89854ecf4bea03ac830.tar.bz2
#10053: Don't close FDs when FileIO.__init__ fails
Loosely based on the work by Hirokazu Yamamoto.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_io/fileio.c12
1 files changed, 6 insertions, 6 deletions
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index d1941df..e23fc6e 100644
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -195,6 +195,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) {
@@ -345,6 +346,7 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
#endif
self->fd = open(name, flags, 0666);
Py_END_ALLOW_THREADS
+ fd_is_own = 1;
if (self->fd < 0) {
#ifdef MS_WINDOWS
if (widename != NULL)
@@ -366,19 +368,17 @@ 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);
}
goto done;
error:
+ if (!fd_is_own)
+ self->fd = -1;
+
ret = -1;
done: