Skip to content
Merged
3 changes: 3 additions & 0 deletions Lib/_pyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,7 @@ def __init__(self, file, mode='r', closefd=True, opener=None):
"""
if self._fd >= 0:
# Have to close the existing file first.
self._stat_atopen = None
try:
if self._closefd:
os.close(self._fd)
Expand Down Expand Up @@ -1585,6 +1586,7 @@ def __init__(self, file, mode='r', closefd=True, opener=None):
except:
if owned_fd is not None:
os.close(owned_fd)
self._stat_atopen = None
raise
self._fd = fd

Expand Down Expand Up @@ -1756,6 +1758,7 @@ def close(self):
called more than once without error.
"""
if not self.closed:
self._stat_atopen = None
try:
if self._closefd:
os.close(self._fd)
Expand Down
29 changes: 20 additions & 9 deletions Modules/_io/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ fileio_dealloc_warn(PyObject *op, PyObject *source)

/* Returns 0 on success, -1 with exception set on failure. */
static int
internal_close(fileio *self)
internal_close(fileio *self, bool clear_stat)
{
int err = 0;
int save_errno = 0;
Expand All @@ -130,6 +130,11 @@ internal_close(fileio *self)
save_errno = errno;
_Py_END_SUPPRESS_IPH
Py_END_ALLOW_THREADS

}
if (clear_stat && self->stat_atopen != NULL) {
PyMem_Free(self->stat_atopen);
self->stat_atopen = NULL;
}
if (err < 0) {
errno = save_errno;
Expand Down Expand Up @@ -178,7 +183,7 @@ _io_FileIO_close_impl(fileio *self, PyTypeObject *cls)
PyErr_Clear();
}
}
rc = internal_close(self);
rc = internal_close(self, true);
if (res == NULL) {
_PyErr_ChainExceptions1(exc);
}
Expand Down Expand Up @@ -267,9 +272,12 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
#endif
if (self->fd >= 0) {
if (self->closefd) {
/* Have to close the existing file first. */
if (internal_close(self) < 0)
/* Have to close the existing file first.

This leaves the stat so we can reuse the memory. */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that this optimization is worth it, I would prefer to always clear the cache in close().

if (internal_close(self, false) < 0) {
return -1;
}
}
else
self->fd = -1;
Expand Down Expand Up @@ -455,12 +463,15 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
#endif
}

PyMem_Free(self->stat_atopen);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure which is preferred here. The Free + New back to back feels weird, but version with the conditional feels like "extra work/code that should never be executed" (but that the compiler / tools that run currently won't validate...).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe if (ptr != NULL) free(ptr) would be more regular and avoid the 4 lines long comment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to that, dropped comment and simplified / back to always calling PyMem_New

self->stat_atopen = PyMem_New(struct _Py_stat_struct, 1);
/* Reuse stat_atopen memory if possible. */
if (self->stat_atopen == NULL) {
PyErr_NoMemory();
goto error;
self->stat_atopen = PyMem_New(struct _Py_stat_struct, 1);
if (self->stat_atopen == NULL) {
PyErr_NoMemory();
goto error;
}
}

Py_BEGIN_ALLOW_THREADS
fstat_result = _Py_fstat_noraise(self->fd, self->stat_atopen);
Py_END_ALLOW_THREADS
Expand Down Expand Up @@ -520,7 +531,7 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
self->fd = -1;
if (self->fd >= 0) {
PyObject *exc = PyErr_GetRaisedException();
internal_close(self);
internal_close(self, true);
_PyErr_ChainExceptions1(exc);
}
if (self->stat_atopen != NULL) {
Expand Down