Repository navigation
Support file descriptors in os.readlink() #157899
Description
Feature or enhancement
Proposal:
I'd like to add support for file descriptors pointing to a symlink in os.readlink() . For example:
# setup
import os
open_flags = os.O_RDONLY
if hasattr(os, "O_SYMLINK"): # MacOS
open_flags |= os.O_SYMLINK
elif hasattr(os, "O_PATH") and hasattr(os, "O_NOFOLLOW"): # Linux
open_flags |= os.O_PATH | os.O_NOFOLLOW
else:
raise NotImplementedError("can't get a file descriptor pointing to a symlink on this platform")
fd = os.open("path/to/some/symlink", open_flags)
# the actual new feature: passing a file descriptor for a symlink to os.readlink()
link_target = os.readlink(fd)Of course, you can already read a symlink directly using the path with os.readlink(), and os.readlink() already supports the optional dir_fd parameter, but if all you have if a FD for the symlink itself, right now Python doesn't have a real way to read the symlink.
(As @Rosuav pointed out in the discourse, on Linux you can work around this with os.readlink("", fd), but that solution doesn't work on MacOS.)
Regarding implementation:
- On Linux, we simply call
readlinkat(fd, "", ..., ...). That should work, according to the man page:Since Linux 2.6.39, pathname can be an empty string, in which case the
call operates on the symbolic link referred to by dirfd (which should
have been obtained using open(2) with the O_PATH and O_NOFOLLOW flags). - On MacOS, we pass the FD to the
freadlinkfunction. - On other OSs, I'd simply throw a
NotImplementedErrorifos.readlink()gets called with an integer.
Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
https://discuss.python.org/t/support-file-descriptors-in-os-readlink/109101/6
Linked PRs
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Activity
pythongh-157899: Add support for fds pointing to symlinks in os.readl…
pythongh-157899: Add support for fds pointing to symlinks in os.readl…
pythongh-157899: Add support for fds pointing to symlinks in os.readl…
pythongh-157899: technical: rename HAVE_FREADLINK C macro to _Py_HAVE…
pythongh-157899: fix docs.
pythongh-157899: fix docs.
pythongh-157899: removed unnecessary guard for test.
pythongh-157899: merged upstream/main into feature/pythongh-157899-fd…
pythongh-157899: added iOS as supported platform for calling os.readl…
gh-157899: Add support for fds pointing to symlinks in os.readlink(). (…
pythongh-157899: Define HAVE_FREADLINK_RUNTIME on FreeBSD 16
gh-157899: Define HAVE_FREADLINK_RUNTIME on FreeBSD 16 (GH-159397)
gh-157899: Skip test_readlink_is_marked_as_supports_fd() on older mac…
pythongh-157899: Factorize support._requires_unix_version() code
pythongh-157899: Factorize support._requires_unix_version() code
gh-157899: Factorize support._requires_unix_version() code (GH-159523)