Repository navigation
Conversation
| <dir_fd>`. | ||
|
|
||
| On MacOS and Linux, *path* can also be a file descriptor referring to a | ||
| symbolic link. In that case, *dir_fd* must be ``None``, and the return |
There was a problem hiding this comment.
Another sensible behaviour would be to ignore dir_fd completely, but I think raising an error here is the better alternative.
|
|
||
| On MacOS and Linux, *path* can also be a file descriptor referring to a | ||
| symbolic link. In that case, *dir_fd* must be ``None``, and the return | ||
| value will be a ``bytes`` object. |
There was a problem hiding this comment.
This just so happened - maybe a str is more reasonable?
There was a problem hiding this comment.
Yes, readlins returns str (with surogatepass encoding, so you can get the bytes out losslessly).
There was a problem hiding this comment.
Updated: The implementation returns a "string" (via PyUnicode_DecodeFSDefaultAndSize), the tests check for equality with a string ("symlink" rather than b"symlink"), and the docs are updated, too.
Documentation build overview
53 files changed ·
|
|
One open question is whether |
d6a5c5c to
d7fa0ad
Compare
…ink(). This feature is only supported on Linux, Android and MacOS, other OSs should raise a NotImplementedError().
d7fa0ad to
8196b59
Compare
|
|
||
| On MacOS and Linux, *path* can also be a file descriptor referring to a | ||
| symbolic link. In that case, *dir_fd* must be ``None``, and the return | ||
| value will be a ``bytes`` object. |
There was a problem hiding this comment.
Yes, readlins returns str (with surogatepass encoding, so you can get the bytes out losslessly).
| } | ||
|
|
||
| if (path->is_fd) { | ||
| #if defined(__APPLE__) && defined(_Py_HAVE_FREADLINK) |
There was a problem hiding this comment.
I think we only want _Py_HAVE_FREADLINK here -- if any other platform has it, it's the function to call.
There was a problem hiding this comment.
Removed the defined(__APPLE__) check.
| <dir_fd>`. | ||
|
|
||
| On MacOS and Linux, *path* can also be a file descriptor referring to a | ||
| symbolic link. In that case, *dir_fd* must be ``None``, and the return |
There was a problem hiding this comment.
It would be nice to document how to get such a file descriptor (O_SYMLINK and O_PATH|O_NOFOLLOW).
| int readlinkat_unavailable = 0; | ||
| #endif | ||
| #ifdef _Py_HAVE_FREADLINK | ||
| int freadlink_unavailable = 0; |
There was a problem hiding this comment.
AFAICS, we don't need the *_unavailable variables. The pattern is sometimes used because we can't raise exceptions inside Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADS, but here, moving the Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADS to tightly wrap each freadlink/readlinkat/readlink call would allow setting the exception & returning direclty in the else blocks.
There was a problem hiding this comment.
Done. I dropped the freadlink_unavailable variable and the error is now raised directly in the else branch.
I also "inlined" all the invocations of Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS, each syscall (freadlink, readlinkat in the path->is_fd case, readlinkat in the old dir_fd case, readlink) are now directly surrounded by those macros.
This is technically a bit of a refactor and slightly out-of-scope of the issue - but I hope it's okay to piggy-back this refactor onto this MR.
| // Linux: readlinkat(dir_fd, "", ...) reads the symbolic link | ||
| // pointed to by dir_fd | ||
| dir_fd = path->fd; | ||
| path->narrow = ""; |
There was a problem hiding this comment.
Putting the values in these variables is misleading.
Instead, could you add a separate block below with a call to readlinkat(path->fd, "", ...) & the error handling?
There was a problem hiding this comment.
Done. We now have a if (path->is_fd) { ... } else if (path->dir_fd != DEFAULT_DIR_FD) { ... } else { ... } structure.
The different implementations of this feature (freadlink on MacOS, readlinkat on Linux/Android, NotImplementedError on Android) are now all inside the if (path->is_fd) { ... } branch.
| if (path->is_fd) { | ||
| #if defined(__APPLE__) && defined(_Py_HAVE_FREADLINK) | ||
| /* nop, freadlink is called below */ | ||
| #elif defined(__linux__) && defined(HAVE_READLINKAT) |
There was a problem hiding this comment.
I think it would be useful to make this a configure variable, something like _Py_READLINKAT_SUPPORTS_EMPTY_PATH, only set on Linux.
Then, when someone ports CPython to another system that uses this (or to ancient Linux that doesn't), they can just change pyconfig.h or the configure script.
Alternately, use defined(HAVE_READLINKAT) && defined(O_PATH). O_PATH and empty pathname support were added in Linux 2.6.39; they look like it's essentially a single feature.
There was a problem hiding this comment.
I tried with defined(HAVE_READLINKAT) && defined(O_PATH), but that broke on the emscripten build. (Apparently both Macros are defined there even though readlink is not supported on that platform AFAIK.)
So now _Py_READLINKAT_SUPPORTS_EMPTY_PATH is set automatically on Linux and Android (that have O_PATH) above. Anyone who really wants it can still force that variable.
I didn't add anything about this to the autoconfig part - I hope that's okay. If you want it threaded through/into the configure.ac somehow, I can look into that.
| (Contributed by Md Arif in :gh:`152936`.) | ||
|
|
||
| * :func:`os.readlink` now accepts a file descriptor referring to a symlink on | ||
| Linux, Android and MacOS. |
There was a problem hiding this comment.
Could you add Android to the other docs?
| _support_readlink_with_fd = hasattr(os, 'readlink') and ( | ||
| "HAVE_FREADLINK" in posix._have_functions # MacOS | ||
| or ( | ||
| os.readlink in os.supports_dir_fd | ||
| and sys.platform in ["linux", "android"] | ||
| ) | ||
| ) |
There was a problem hiding this comment.
One open question is whether os.readlink() should be added to supports_fd. I don't have an opinion on that, happy to take others' lead here.
I'd say add them. O_SYMLINK/O_PATH|O_NOFOLLOW are the way to get fds referring to a symlink on their respective platform, so this can be just os.readlink in os.supports_dir_fd.
(Yes, I changed my mind after going through the code.)
There was a problem hiding this comment.
Then, please also test that os.readlink in os.supports_dir_fd holds on MacOS, Linux & Android.
There was a problem hiding this comment.
Done. os.readlink is now added automatically to os.supports_fd. I also added a test that checks presence/absence on all platforms where both os.readlink and os.supports_fd are defined.
There was a problem hiding this comment.
_support_readlink_with_fd should now be equivalent to hasattr(os, 'readlink') and (os.readlink in os.supports_fd). Or more fancy, (getattr(os, 'readlink', None) in os.supports_fd)
The in expression can be used directly, as in the test above; no need for a _support_readlink_with_fd wariable.
There was a problem hiding this comment.
I'm updating this, but I'd prefer to keep the _support_readlink_with_fd variable (just for avoiding code/logic duplication).
As a side effect, I'm now removing the self.assertEqual(os.readlink in os.supports_fd, self._support_readlink_with_fd) test that you asked for, since this test would be tautological. (It currently sits behind a @unittest.skipUnless(hasattr(os, "readlink")) guard.)
There was a problem hiding this comment.
Okay, rather than removing the test completely I added something closer to what you originally asked for:
@unittest.skipUnless(sys.platform in ["linux", "android", "darwin", "ios"],
"feature not supported on this platform")
def test_readlink_is_marked_as_supports_fd(self):
self.assertIn(os.readlink, os.supports_fd)…ddress feedback from MR. * os.readlink() now returns a `str` (rather than `bytes`) when called with a file descriptor. * os.readlink now added to `os.supports_fd` on the platforms where this feature is supported. * Small refactor inside `os.readlink`: drop *_unavailable variables, generate error messages inline. Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADS around each syscall site. * Docs: Android is now mentioned as supported platform for this feature in all relevant places.
Emscripten apparrently defines O_PATH and readlinkat - even though it doesn't actually support them. This broke auto-detection of whether os.readlink supports FDs - which broke the tests.
|
Thank you for your review @encukou . I updated this PR based on your comments. Please re-review when you can spare some minutes. |
vstinner
left a comment
There was a problem hiding this comment.
macOS should be written "macOS", not "MacOS".
After changing return type of os.readlink when called with an fd from bytes to string, missed one place in the docs that needed updating.
* Simplified/unified error handling in case reading fds referring to symlinks is not supported. * Docs: centralized the information on the return type of os.readlink in a single paragraph. * Docs/comments: rewrite from "MacOS" to "macOS" (lower-case).
6094134 to
20fa1e2
Compare
vstinner
left a comment
There was a problem hiding this comment.
Overall, the change LGTM. I just have another minor comment.
@encukou: I let you double check the change :-)
I was curious and replaced readlinkat(path->fd, "", buffer, MAXPATHLEN) with readlinkat(path->fd, "", buffer, MAXPATHLEN). With this change, the function fails with OSError: [Errno 14] Bad address: 6. So no, it's not possible to pass NULL: passing an empty string is the right argument.
| @unittest.skipUnless(hasattr(os, "supports_fd") and hasattr(os, "readlink"), | ||
| "feature not supported on this platform") |
There was a problem hiding this comment.
According to other tests, hasattr(os, "supports_fd") test is not needed, the attribute can always be used.
| @unittest.skipUnless(hasattr(os, "supports_fd") and hasattr(os, "readlink"), | |
| "feature not supported on this platform") | |
| @unittest.skipUnless(hasattr(os, "readlink"), "need os.readlink") |
There was a problem hiding this comment.
Sorry, I missed this comment. I just pushed an update: 6c8f5f1
Originally I wanted to be careful (because the supports_fd set is only defined within an if _exists("_have_functions") guard), but it appears that the _have_functions list is defined unconditionally (at least in the posixmodule.c).
I will, after the 3.15.0 release. Please don't merge yet. |
…s-in-os-readlink and resolved merge conflicts.
|
Resolved a merge conflict with commit cfd6cf7 on a "clinic end generated code" line: cfd6cf71547e#diff-f4ec448d9bc73d373452e97931de916ca83058ae93bce4a5853b8ac18c9d2cfa |
encukou
left a comment
There was a problem hiding this comment.
Thank you! This is looking great, I have a few cleanups to suggest:
| _support_readlink_with_fd = hasattr(os, 'readlink') and ( | ||
| "HAVE_FREADLINK" in posix._have_functions # MacOS | ||
| or ( | ||
| os.readlink in os.supports_dir_fd | ||
| and sys.platform in ["linux", "android"] | ||
| ) | ||
| ) |
There was a problem hiding this comment.
_support_readlink_with_fd should now be equivalent to hasattr(os, 'readlink') and (os.readlink in os.supports_fd). Or more fancy, (getattr(os, 'readlink', None) in os.supports_fd)
The in expression can be used directly, as in the test above; no need for a _support_readlink_with_fd wariable.
| def test_readlink_with_fd_throws_not_implemented_error(self): | ||
| # on unsupported platforms, we may not even be able to get a | ||
| # file descriptor for a symlink, so use a fd for an ordinary file | ||
| os_helper.create_empty_file(os_helper.TESTFN) | ||
| self.addCleanup(os_helper.unlink, os_helper.TESTFN) | ||
| fd = os.open(os_helper.TESTFN, os.O_RDONLY) | ||
| self.addCleanup(os.close, fd) | ||
| with self.assertRaises(NotImplementedError): | ||
| os.readlink(fd) |
There was a problem hiding this comment.
Could you also test that _open_symlink_as_fd raises?
Such a test might be too eager, if it is we* can always remove it. But, if a platform has either O_SYMLINK or O_NOFOLLOW | O_PATH, it would be weird if it didn't also support readlink with fd.
* (or someone porting to a new platform, after the tests warn them about the issue)
There was a problem hiding this comment.
Hmm, from what I can see iOS has O_SYMLINK in SDK version 9.3: https://github.com/xybp888/iOS-SDKs/blob/ad607cb07fe4ad1c9b91cf970bd228c2a9253207/iPhoneOS9.3.sdk/usr/include/sys/fcntl.h#L154
I also found that apparently iOS does have freadlink starting in version 16.0: https://github.com/apple-oss-distributions/xnu/blob/f6217f891ac0bb64f3d375211650a4c1ff8ca1ea/bsd/sys/unistd.h#L192 . So I'll update this MR to support freadlink on iOS >= 16.0, but the test you suggest would break on iOS < 16.0.
| On Linux, Android and macOS, path may be a file descriptor referring to | ||
| a symlink. If it is, dir_fd must be None, and the return value will be a | ||
| string object. (File descriptors for symlinks can be obtained with | ||
|
|
||
| os.open(..., os.O_RDONLY | os.O_PATH | os.O_NOFOLLOW) | ||
|
|
||
| on Linux and Android, and: | ||
|
|
||
| os.open(..., os.O_RDONLY | os.O_SYMLINK) | ||
|
|
||
| on macOS.) |
There was a problem hiding this comment.
- "the return value will be a string object" was already mentioned.
- The usage notes are better left to the main docs.
| On Linux, Android and macOS, path may be a file descriptor referring to | |
| a symlink. If it is, dir_fd must be None, and the return value will be a | |
| string object. (File descriptors for symlinks can be obtained with | |
| os.open(..., os.O_RDONLY | os.O_PATH | os.O_NOFOLLOW) | |
| on Linux and Android, and: | |
| os.open(..., os.O_RDONLY | os.O_SYMLINK) | |
| on macOS.) | |
| On Linux, Android and macOS, path may be a file descriptor referring to | |
| a symlink. If it is, dir_fd must be None. |
There was a problem hiding this comment.
done. (Also added iOS to the docs here, in the os.rst, whatsnew/3.16.rst and the NEWS.d.)
| if (path->is_fd || PyUnicode_Check(path->object)) | ||
| return PyUnicode_DecodeFSDefaultAndSize(buffer, length); | ||
| else | ||
| return PyBytes_FromStringAndSize(buffer, length); |
There was a problem hiding this comment.
Update to PEP-7 style when touching the code:
| if (path->is_fd || PyUnicode_Check(path->object)) | |
| return PyUnicode_DecodeFSDefaultAndSize(buffer, length); | |
| else | |
| return PyBytes_FromStringAndSize(buffer, length); | |
| if (path->is_fd || PyUnicode_Check(path->object)) { | |
| return PyUnicode_DecodeFSDefaultAndSize(buffer, length); | |
| } | |
| else { | |
| return PyBytes_FromStringAndSize(buffer, length); | |
| } |
…ink with a FD referring to a symlink. Also some code/test cleanup.
See issue #157899 and https://discuss.python.org/t/support-file-descriptors-in-os-readlink/109101/6 for context.
CC @encukou