-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-106046: Improve error message from os.fspath
if __fspath__
is set to None
#106082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3ec02c8
gh-106046: Improve error message from `os.fspath` if `__fspath__` is …
AlexWaygood 1e9ebd4
Update Lib/test/test_os.py
AlexWaygood dbfb232
Revert change to make error message more consistent: it makes other t…
AlexWaygood 982d663
Merge branch 'fspath' of https://github.com/AlexWaygood/cpython into …
AlexWaygood 1dcf5e3
Document it
AlexWaygood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Library/2023-06-23-22-52-24.gh-issue-106046.OdLiLJ.rst
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,2 @@ | ||
Improve the error message from :func:`os.fspath` if called on an object | ||
where ``__fspath__`` is set to ``None``. Patch by Alex Waygood. |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few thoughts, no change requested:
NULL == func
here is a "Yoda condition". People write that because C lets you writeif (func = NULL)
, and it doesn't do what you want (it always sets func to NULL). The Yoda condition prevents this, becauseif (NULL = func)
is probably a syntax error. However, Yoda conditions aren't that useful these days as compilers will warn aboutif (func = NULL)
. Arguably you're introducing an inconsistency here as one of the two conditions on this line is Yoda and the other isn't, but I think that's fine.Py_IsNone
function (https://docs.python.org/3/c-api/structures.html#c.Py_IsNone) that could be used instead of== Py_None
. I think the motivation is to allow alternate C APIs wherePy_None
might not be a singleton. I don't know if there is any push to use this function in CPython itself, though I see a few calls to it. However, there are already several== Py_None
checks in this file, so it seems fine to add a few more.func
. However, now Py_None is immortal and we no longer have to worry about refcounting for it. Otherwise, I'd have suggested puttingPy_XDECREF(func)
inside the conditional block (theX
meaning thatfunc
may be NULL).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for explaining this! I wondered what the rationale was here for writing it like this!
Huh. I checked the API docs for
Py_None
before filing this PR, and they didn't mention this function — they recommended using==
: https://docs.python.org/3/c-api/none.html. I guess I'll leave this as it is for now, since as you say, there are a bunch of other places in this file that use==
:)