GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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

@AlexWaygood

@AlexWaygood AlexWaygood commented Jun 25, 2023 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Closes #106046

  • Issue: Improve error message from os.fspath if __fspath__ is set to None #106046

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@AlexWaygood
pythongh-106046: Improve error message from os.fspath if `__fspath_…
3ec02c8
…_` is set to `None`
@AlexWaygood AlexWaygood added type-feature A feature request or enhancement 3.13 only security fixes labels Jun 25, 2023
@AlexWaygood
AlexWaygood requested review from JelleZijlstra and barneygale June 25, 2023 13:54
@bedevere-bot bedevere-bot added the awaiting core review label Jun 25, 2023
@bedevere-bot bedevere-bot mentioned this pull request Jun 25, 2023
Improve error message from os.fspath if __fspath__ is set to None #106046
Closed
JelleZijlstra
JelleZijlstra reviewed Jun 25, 2023
View reviewed changes
Comment thread Lib/test/test_os.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

AlexWaygood and others added 4 commits June 25, 2023 15:10
@AlexWaygood @JelleZijlstra
Update Lib/test/test_os.py
1e9ebd4
Co-authored-by: Jelle Zijlstra <[email protected]>
@AlexWaygood
Revert change to make error message more consistent: it makes other t…
dbfb232
…ests fail elsewhere
@AlexWaygood
Merge branch 'fspath' of https://github.com/AlexWaygood/cpython into …
982d663
…fspath
@AlexWaygood
Document it
1dcf5e3
@AlexWaygood
AlexWaygood requested a review from JelleZijlstra June 25, 2023 15:14
barneygale
barneygale approved these changes Jun 25, 2023
View reviewed changes

@barneygale barneygale left a comment

Copy link
Copy Markdown
Contributor

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 so much for looking at this :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AlexWaygood reacted with heart emoji
All reactions
  • ❤️ 1 reaction
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting core review labels Jun 25, 2023
JelleZijlstra
JelleZijlstra approved these changes Jun 25, 2023
View reviewed changes
Comment thread Modules/posixmodule.c

func = _PyObject_LookupSpecial(o, &_Py_ID(__fspath__));
if (NULL == func) {
if ((NULL == func) || (func == Py_None)) {

@JelleZijlstra JelleZijlstra Jun 25, 2023

Copy link
Copy Markdown
Member

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:

  • The parentheses are technically redundant, but it's OK to keep them for extra clarity.
  • The 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.
  • There is a 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.
  • I already mentioned this on Discord, but before 3.12 this would have introduced a refleak, as the branch never decrefs 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.

Uh oh!

There was an error while loading. Please reload this page.

AlexWaygood reacted with heart emoji
All reactions
  • ❤️ 1 reaction

@AlexWaygood AlexWaygood Jun 25, 2023

Copy link
Copy Markdown
Member Author

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.

  • The 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).

Thanks for explaining this! I wondered what the rationale was here for writing it like this!

  • There is a 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.

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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@AlexWaygood
AlexWaygood merged commit 93a970f into python:main Jun 25, 2023
@bedevere-bot bedevere-bot removed the awaiting merge label Jun 25, 2023
@AlexWaygood
AlexWaygood deleted the fspath branch June 25, 2023 23:06
@barneygale barneygale mentioned this pull request Oct 11, 2023
GH-110109: Add pathlib._PurePathBase #110670
Merged
@barneygale barneygale mentioned this pull request Feb 14, 2025
Question: recommended approach for PurePosixPath-like class that is not os.PathLike barneygale/pathlib-abc#29
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@JelleZijlstra JelleZijlstra JelleZijlstra approved these changes
@barneygale barneygale barneygale approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

3.13 only security fixes type-feature A feature request or enhancement

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Improve error message from os.fspath if __fspath__ is set to None

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@AlexWaygood @JelleZijlstra @barneygale @bedevere-bot
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.