FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-42780: fix set_inheritable() for O_PATH file descriptors on Linux by cptpcrd · Pull Request #24172 · python/cpython · GitHub

Repository navigation

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

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
27 changes: 27 additions & 0 deletions Lib/test/test_os.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3881,6 +3881,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):
Comment thread
cptpcrd marked this conversation as resolved.
fd = os_helper.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)
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix os.set_inheritable() for O_PATH file descriptors on Linux.
7 changes: 7 additions & 0 deletions Python/fileutils.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,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);
Expand Down

Back | FazBrowse Home | New Git URL