-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
pathlib: inconsistent handling of rootless Windows paths #94909
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
Comments
Related to #64107, which improved the |
Here's a patch that will be applicable when #31691 lands. It uses diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index bd86403c8e..ad3c03f1bd 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -266,10 +266,8 @@ def _parse_parts(cls, parts):
sep = cls._flavour.sep
altsep = cls._flavour.altsep
drv = root = ''
- it = reversed(parts)
- for part in it:
- if not part:
- continue
+ if parts:
+ part = cls._flavour.join(*parts)
if altsep:
part = part.replace(altsep, sep)
drv, rel = cls._flavour.splitdrive(part)
@@ -289,20 +287,6 @@ def _parse_parts(cls, parts):
else:
if rel and rel != '.':
parsed.append(sys.intern(rel))
- if drv or root:
- if not drv:
- # If no drive is present, try to find one in the previous
- # parts. This makes the result of parsing e.g.
- # ("C:", "/", "a") reasonably intuitive.
- for part in it:
- if not part:
- continue
- if altsep:
- part = part.replace(altsep, sep)
- drv = cls._flavour.splitdrive(part)[0]
- if drv:
- break
- break
if drv or root:
parsed.append(drv + root)
parsed.reverse() |
I definitely agree that this is sufficiently complex that |
…ib (GH-95450) Have pathlib use `os.path.join()` to join arguments to the `PurePath` initialiser, which fixes a minor bug when handling relative paths with drives. Previously: ```python >>> from pathlib import PureWindowsPath >>> a = 'C:/a/b' >>> b = 'C:x/y' >>> PureWindowsPath(a, b) PureWindowsPath('C:x/y') ``` Now: ```python >>> PureWindowsPath(a, b) PureWindowsPath('C:/a/b/x/y') ```
The fix only went into |
Seems to be the only case where the
PureWindowsPath
constuctor andjoinpath()
disagree when given the same arguments. This has implications for future work, so I'd love to make them consistent.Affects all supported versions of Python.
The text was updated successfully, but these errors were encountered: