GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||
GeezerVison is showing github.com — gh-106046: Improve error message from `os.fspath` if `__fspath__` is set to `None` by AlexWaygood · Pull Request #106082 · python/cpython Conversation
|
|
|
||
| func = _PyObject_LookupSpecial(o, &_Py_ID(__fspath__)); | ||
| if (NULL == func) { | ||
| if ((NULL == func) || (func == Py_None)) { |
There was a problem 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 write if (func = NULL), and it doesn't do what you want (it always sets func to NULL). The Yoda condition prevents this, because if (NULL = func) is probably a syntax error. However, Yoda conditions aren't that useful these days as compilers will warn about if (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 where Py_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 putting Py_XDECREF(func) inside the conditional block (the X meaning that func may be NULL).
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
- The
NULL == funchere 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).
Thanks for explaining this! I wondered what the rationale was here for writing it like this!
- There is a
Py_IsNonefunction (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_Nonemight not be a singleton.
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 == :)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
Improve error message fromos.fspath if __fspath__ is set to None
There was an error while loading. Please reload this page.