Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Closed
barneygale opened this issue Jul 16, 2022 · 5 comments · Fixed by #95450
Closed

pathlib: inconsistent handling of rootless Windows paths #94909

barneygale opened this issue Jul 16, 2022 · 5 comments · Fixed by #95450
Assignees
Labels
OS-windows stdlib Python modules in the Lib dir topic-pathlib type-bug An unexpected behavior, bug, or error

Comments

@barneygale
Copy link
Contributor

barneygale commented Jul 16, 2022

>>> from pathlib import PureWindowsPath
>>> a = 'C:/a/b'
>>> b = 'C:x/y'
>>> PureWindowsPath(a, b)
PureWindowsPath('C:x/y')
>>> PureWindowsPath(a) / b
PureWindowsPath('C:/a/b/x/y')

Seems to be the only case where the PureWindowsPath constuctor and joinpath() 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.

@barneygale barneygale added the type-bug An unexpected behavior, bug, or error label Jul 16, 2022
@barneygale
Copy link
Contributor Author

Related to #64107, which improved the joinpath() behaviour.

@barneygale
Copy link
Contributor Author

barneygale commented Jul 16, 2022

Here's a patch that will be applicable when #31691 lands. It uses ntpath.join() to join the path parts, and consequently scans left-to-right without an early exit from the loop. Optimizations are possible, I just went for minimal diff:

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()

@eryksun eryksun added OS-windows stdlib Python modules in the Lib dir topic-pathlib labels Jul 17, 2022
@ericvsmith
Copy link
Member

I definitely agree that this is sufficiently complex that pathlib should use ntpath wherever possible.

@barneygale
Copy link
Contributor Author

In fact, this would be good to fix before #31691 lands, as it makes backporting easier. PR available: #95450.

@brettcannon brettcannon self-assigned this Aug 5, 2022
miss-islington pushed a commit that referenced this issue Aug 12, 2022
…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')
```
@brettcannon
Copy link
Member

The fix only went into main to minimize risk of breaking and out of simplicity since this fixes an edge case only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OS-windows stdlib Python modules in the Lib dir topic-pathlib type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants