[Python-checkins] bpo-42780: Fix set_inheritable() for O_PATH file descriptors on Linux (GH-24172) (GH-24278)
vstinner
webhook-mailer at python.org
Thu Jan 21 05:47:05 EST 2021
https://github.com/python/cpython/commit/6893523bed4ad701838715380659c245eec71d48
commit: 6893523bed4ad701838715380659c245eec71d48
branch: 3.9
author: cptpcrd <31829097+cptpcrd at users.noreply.github.com>
committer: vstinner <vstinner at python.org>
date: 2021-01-21T11:46:35+01:00
summary:
bpo-42780: Fix set_inheritable() for O_PATH file descriptors on Linux (GH-24172) (GH-24278)
(cherry picked from commit 7dc71c425cf6aa6a4070a418dce5d95ca435c79f)
files:
A Misc/NEWS.d/next/Library/2021-01-08-15-49-20.bpo-42780.rtqi6B.rst
M Lib/test/test_os.py
M Python/fileutils.c
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index bf1cb5f511255..35933e9c8c3a9 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -3659,6 +3659,33 @@ def test_set_inheritable_cloexec(self):
self.assertEqual(fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC,
0)
+ @unittest.skipUnless(hasattr(os, 'O_PATH'), "need os.O_PATH")
+ def test_get_set_inheritable_o_path(self):
+ fd = os.open(__file__, os.O_PATH)
+ self.addCleanup(os.close, fd)
+ self.assertEqual(os.get_inheritable(fd), False)
+
+ os.set_inheritable(fd, True)
+ self.assertEqual(os.get_inheritable(fd), True)
+
+ os.set_inheritable(fd, False)
+ self.assertEqual(os.get_inheritable(fd), False)
+
+ def test_get_set_inheritable_badf(self):
+ fd = support.make_bad_fd()
+
+ with self.assertRaises(OSError) as ctx:
+ os.get_inheritable(fd)
+ self.assertEqual(ctx.exception.errno, errno.EBADF)
+
+ with self.assertRaises(OSError) as ctx:
+ os.set_inheritable(fd, True)
+ self.assertEqual(ctx.exception.errno, errno.EBADF)
+
+ with self.assertRaises(OSError) as ctx:
+ os.set_inheritable(fd, False)
+ self.assertEqual(ctx.exception.errno, errno.EBADF)
+
def test_open(self):
fd = os.open(__file__, os.O_RDONLY)
self.addCleanup(os.close, fd)
diff --git a/Misc/NEWS.d/next/Library/2021-01-08-15-49-20.bpo-42780.rtqi6B.rst b/Misc/NEWS.d/next/Library/2021-01-08-15-49-20.bpo-42780.rtqi6B.rst
new file mode 100644
index 0000000000000..a491690507129
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-01-08-15-49-20.bpo-42780.rtqi6B.rst
@@ -0,0 +1 @@
+Fix os.set_inheritable() for O_PATH file descriptors on Linux.
diff --git a/Python/fileutils.c b/Python/fileutils.c
index 397ac34e8073d..ddc090988f158 100644
--- a/Python/fileutils.c
+++ b/Python/fileutils.c
@@ -1165,6 +1165,13 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
return 0;
}
+#ifdef __linux__
+ if (errno == EBADF) {
+ // On Linux, ioctl(FIOCLEX) will fail with EBADF for O_PATH file descriptors
+ // Fall through to the fcntl() path
+ }
+ else
+#endif
if (errno != ENOTTY && errno != EACCES) {
if (raise)
PyErr_SetFromErrno(PyExc_OSError);
More information about the Python-checkins
mailing list