[Python-checkins] bpo-38031: Fix a possible assertion failure in _io.FileIO() (GH-GH-5688)
miss-islington
webhook-mailer at python.org
Fri Nov 25 08:20:06 EST 2022
https://github.com/python/cpython/commit/c06f74f1d66290b22de3f6d6e1fe32dd3e0786e1
commit: c06f74f1d66290b22de3f6d6e1fe32dd3e0786e1
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-11-25T05:20:00-08:00
summary:
bpo-38031: Fix a possible assertion failure in _io.FileIO() (GH-GH-5688)
(cherry picked from commit d386115039e75c332c8471c239cf7dc5dee791a7)
Co-authored-by: Zackery Spytz <zspytz at gmail.com>
files:
A Misc/NEWS.d/next/Core and Builtins/2019-09-04-19-09-49.bpo-38031.Yq4L72.rst
M Lib/test/test_io.py
M Modules/_io/fileio.c
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index daccbae5b4a1..e445f47d2145 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -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, \
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-09-04-19-09-49.bpo-38031.Yq4L72.rst b/Misc/NEWS.d/next/Core and Builtins/2019-09-04-19-09-49.bpo-38031.Yq4L72.rst
new file mode 100644
index 000000000000..b5964375962f
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2019-09-04-19-09-49.bpo-38031.Yq4L72.rst
@@ -0,0 +1,2 @@
+Fix a possible assertion failure in :class:`io.FileIO` when the opener
+returns an invalid file descriptor.
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index 8b1cff56d75f..4496609afcbc 100644
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -492,8 +492,12 @@ _Py_COMP_DIAG_POP
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
More information about the Python-checkins
mailing list