mirror of
https://github.com/python/cpython.git
synced 2025-11-25 21:11:09 +00:00
bpo-38031: Fix a possible assertion failure in _io.FileIO() (#GH-5688)
This commit is contained in:
parent
b1dcdefc3a
commit
d386115039
3 changed files with 15 additions and 1 deletions
|
|
@ -888,6 +888,14 @@ class IOTest(unittest.TestCase):
|
||||||
open('non-existent', 'r', opener=badopener)
|
open('non-existent', 'r', opener=badopener)
|
||||||
self.assertEqual(str(cm.exception), 'opener returned -2')
|
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):
|
def test_fileio_closefd(self):
|
||||||
# Issue #4841
|
# Issue #4841
|
||||||
with self.open(__file__, 'rb') as f1, \
|
with self.open(__file__, 'rb') as f1, \
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix a possible assertion failure in :class:`io.FileIO` when the opener
|
||||||
|
returns an invalid file descriptor.
|
||||||
|
|
@ -485,8 +485,12 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
|
||||||
ret = -1;
|
ret = -1;
|
||||||
if (!fd_is_own)
|
if (!fd_is_own)
|
||||||
self->fd = -1;
|
self->fd = -1;
|
||||||
if (self->fd >= 0)
|
if (self->fd >= 0) {
|
||||||
|
PyObject *exc, *val, *tb;
|
||||||
|
PyErr_Fetch(&exc, &val, &tb);
|
||||||
internal_close(self);
|
internal_close(self);
|
||||||
|
_PyErr_ChainExceptions(exc, val, tb);
|
||||||
|
}
|
||||||
|
|
||||||
done:
|
done:
|
||||||
#ifdef MS_WINDOWS
|
#ifdef MS_WINDOWS
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue