From dc7c128d74876bf0bcef61e34bd858a675b7a43c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Tue, 24 Mar 2009 13:21:53 +0000 Subject: [PATCH] http://bugs.python.org/issue5544 Someone may have closed the file descriptor, with something like f = open('test.test', 'w') os.close(f.fileno()) f.close() Protect against this by checking fd on windows before closing. --- Modules/_fileio.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Modules/_fileio.c b/Modules/_fileio.c index 88ee54c5f8..00065611e3 100644 --- a/Modules/_fileio.c +++ b/Modules/_fileio.c @@ -77,11 +77,15 @@ internal_close(PyFileIOObject *self) if (self->fd >= 0) { int fd = self->fd; self->fd = -1; - Py_BEGIN_ALLOW_THREADS - err = close(fd); - if (err < 0) - save_errno = errno; - Py_END_ALLOW_THREADS + /* fd is accessible and someone else may have closed it */ + if (_PyVerify_fd(fd)) { + Py_BEGIN_ALLOW_THREADS + err = close(fd); + if (err < 0) + save_errno = errno; + Py_END_ALLOW_THREADS + } else + save_errno = errno; } if (err < 0) { errno = save_errno; -- 2.40.0