Fix the last known case where pathlib can mangle the meaning of a path. This brings pathlib in line with IEEE Std 1003.1-2017, where trailing slashes are meaningful to path resolution and should not be discarded.
Changes
In several important respects, paths with trailing slashes behave differently to their slash-less counterparts:
Paths with and without trailing slashes compare unequal and generate different hash codes
__str__(), __fspath__() and related representations include any trailing slash
glob() patterns ending with a slash will now generate results ending with a slash, matching glob.glob() behaviour
match() now observes trailing slashes, and so its pattern language exactly matches that of glob().
To manipulate a trailing slash, we add these new methods/properties:
has_trailing_sep - read-only boolean indicating whether a trailing slash is present
with_trailing_sep() - returns a new path with a trailing slash present
without_trailing_sep() - returns a new path with a trailing slash omitted
Backwards compatibility
Empty segments given to the PurePath initialiser do not generate new segments, so str(PurePath("foo", "")) results in "foo", not "foo/".
Methods concerned with dirnames and basenames ignore any trailing slash:
name, stem, suffix and suffixes retrieve the last non-empty path segment, and so any trailing slash are ignored
with_name(), with_stem() and with_suffix() replace the last non-empty path segment
parent and parents ignore any trailing slash
relative_to() and is_relative_to() ignore any trailing slashes in self and other.
The .parts tuple works exactly as before, and doesn't distinguish paths with trailing separators
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.
Fix the last known case where pathlib can mangle the meaning of a path. This brings pathlib in line with IEEE Std 1003.1-2017, where trailing slashes are meaningful to path resolution and should not be discarded.
Changes
In several important respects, paths with trailing slashes behave differently to their slash-less counterparts:
To manipulate a trailing slash, we add these new methods/properties:
Backwards compatibility
Empty segments given to the PurePath initialiser do not generate new segments, so str(PurePath("foo", "")) results in "foo", not "foo/".
Methods concerned with dirnames and basenames ignore any trailing slash:
Dependencies
Future work
Once this lands, we can take advantage of the fact that pathlib does not mangle paths. Thus: