Skip to content

Add suffix function to the fs module #14802

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

joukewitteveen
Copy link
Contributor

When iterating over a list of files, it can be convenient to check suffixes. You could already do fs.name(file).endswith('.txt'), but fs.suffix(file) == '.txt' is just that tiny bit nicer. We also get the invariant fs.name(file) == fs.stem(file) + fs.suffix(file).

@joukewitteveen joukewitteveen requested a review from jpakkane as a code owner July 14, 2025 14:18
@joukewitteveen joukewitteveen force-pushed the fs-suffix branch 2 times, most recently from 74acc58 to 6e80fb8 Compare July 14, 2025 15:51
@joukewitteveen
Copy link
Contributor Author

The corner case of an 'empty-but-present suffix' (e.g. foo/bar.) will change behavior in Python 3.14. I've removed our test for it.

@dcbaker dcbaker added this to the 1.9 milestone Jul 18, 2025
@dcbaker
Copy link
Member

dcbaker commented Jul 18, 2025

Everything looks good to me other than the one very small nit, so this should be good to go.

@bonzini
Copy link
Collaborator

bonzini commented Jul 19, 2025

The corner case of an 'empty-but-present suffix' (e.g. foo/bar.) will change behavior in Python 3.14.

Should Meson instead use already os.path.splitext so that it already uses the 3.14 behavior?

@nirbheek
Copy link
Member

Should Meson instead use already os.path.splitext so that it already uses the 3.14 behavior?

I agree, and also we should have a test for this so the behaviour stays the same regardless of Python version.

@dcbaker
Copy link
Member

dcbaker commented Jul 19, 2025

pathlib for concrete paths has a lot of ugly and painful corners (the purepaths are much better), I'd prefer to use os.path in general if possible

@joukewitteveen
Copy link
Contributor Author

I'll try to find some time end of next week to redo the module in terms of os.path 👍.

@joukewitteveen joukewitteveen force-pushed the fs-suffix branch 2 times, most recently from 2df195c to 3fc80eb Compare July 23, 2025 06:19
@joukewitteveen
Copy link
Contributor Author

There was a single call to mesonlib.path_is_in_root that still required importing pathlib.Path. I don't think this PR is the place to change the signature of path_is_in_root, so I kept it. Otherwise, the module now directly calls into os.path. I used the opportunity to unquote all type specifiers (all function bodies were touched).

@joukewitteveen
Copy link
Contributor Author

The failing checks are due to a bug in ntpath.isabs that was fixed in Python 3.13.

I'm not sure what to do. We cannot use os.path.splitroot, because it is only available from Python 3.12 onward. Using a PurePath for just this function seems excessive. I'll need to think a bit, suggestions are welcome!

@bonzini
Copy link
Collaborator

bonzini commented Jul 23, 2025

Using a PurePath for just this function seems excessive

Maybe but I don't see any alternative. Just use a PurePath and add a link to python/cpython#44626 in a comment. Thanks for looking into the failures!

@joukewitteveen
Copy link
Contributor Author

How about this fix? We would otherwise (and currently are) be affected by a related bug where incomplete UNC paths (which are absolute because they don't depend on the current working directory) would not be considered absolute.

@joukewitteveen
Copy link
Contributor Author

The previous test failure was due to an implicit assumption that replace_suffix changed the alternate path separator on Windows (/) to \. Regardless of whether we want that, I think the assumption should not be part of the specific test that failed, so I watered that test down (by using \ both before and after replacement, the test passes with both implementations).

Let me know if we want to reinstate the separator-changing behavior of replace_suffix. I think we shouldn't.

@joukewitteveen
Copy link
Contributor Author

The CI failures seem unrelated. Before my last push, where I corrected some authorship details, all checks passed. Maybe someone can restart the unsuccessful checks?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants