Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,14 @@ def badopener(fname, flags):
open('non-existent', 'r', opener=badopener)
self.assertEqual(str(cm.exception), 'opener returned -2')

def test_opener_invalid_fd(self):
# Check that OSError is raised with error code EBADF if the
# opener returns an invalid file descriptor (see gh-82212).
fd = os_helper.make_bad_fd()
with self.assertRaises(OSError) as cm:
self.open('foo', opener=lambda name, flags: fd)
self.assertEqual(cm.exception.errno, errno.EBADF)

def test_fileio_closefd(self):
# Issue #4841
with self.open(__file__, 'rb') as f1, \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a possible assertion failure in :class:`io.FileIO` when the opener
returns an invalid file descriptor.
6 changes: 5 additions & 1 deletion Modules/_io/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,12 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
ret = -1;
if (!fd_is_own)
self->fd = -1;
if (self->fd >= 0)
if (self->fd >= 0) {
PyObject *exc, *val, *tb;
PyErr_Fetch(&exc, &val, &tb);
internal_close(self);
_PyErr_ChainExceptions(exc, val, tb);
}

done:
#ifdef MS_WINDOWS
Expand Down