-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
[WIP] GH-65238: Preserve trailing slash in pathlib #112363
Closed
Closed
Conversation
This file contains 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
__str__()
,__fspath__()
and related representations include any trailing slashglob()
patterns ending with a slash will now generate results ending with a slash, matchingglob.glob()
behaviourmatch()
now observes trailing slashes, and so its pattern language exactly matches that ofglob()
.To manipulate a trailing slash, we add these new methods/properties:
has_trailing_sep
- read-only boolean indicating whether a trailing slash is presentwith_trailing_sep()
- returns a new path with a trailing slash presentwithout_trailing_sep()
- returns a new path with a trailing slash omittedBackwards compatibility
Empty segments given to the
PurePath
initialiser do not generate new segments, sostr(PurePath("foo", ""))
results in"foo"
, not"foo/"
.Methods concerned with dirnames and basenames ignore any trailing slash:
name
,stem
,suffix
andsuffixes
retrieve the last non-empty path segment, and so any trailing slash are ignoredwith_name()
,with_stem()
andwith_suffix()
replace the last non-empty path segmentparent
andparents
ignore any trailing slashrelative_to()
andis_relative_to()
ignore any trailing slashes in self and other..parts
tuple works exactly as before, and doesn't distinguish paths with trailing separatorsDependencies
Path.glob()
expectations in pathlib tests #112365test_posixpath
andtest_ntpath
#112676Future work
Once this lands, we can take advantage of the fact that pathlib does not mangle paths. Thus:
__fspath__()
return an unnormalized path for a tasty speed boost in many applicationspathlib.PurePath.__fspath__()
#102783pathlib
fromshutil
, which means we can add methods likechown()
,move()
andrmtree()
without code duplication