summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorHynek Schlawack <hs@ox.cx>2012-06-21 18:20:25 (GMT)
committerHynek Schlawack <hs@ox.cx>2012-06-21 18:20:25 (GMT)
commit9ed8b4e4cac4f38109c3e08a8bfd19b7f6ad1c88 (patch)
treefbe94f34da9f31f1057dacf23a2e54caafd94240 /Modules
parent103e8113e4bb4ad3687d641f660481c72904d571 (diff)
downloadcpython-9ed8b4e4cac4f38109c3e08a8bfd19b7f6ad1c88.zip
cpython-9ed8b4e4cac4f38109c3e08a8bfd19b7f6ad1c88.tar.gz
cpython-9ed8b4e4cac4f38109c3e08a8bfd19b7f6ad1c88.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.c11
1 files changed, 5 insertions, 6 deletions
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index c51d697..6feca21 100644
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -224,6 +224,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) {
@@ -362,6 +363,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)
@@ -388,13 +390,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);
}
@@ -402,6 +399,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);