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

Skip to content

bpo-38671: Make sure to return an absolute path in pathlib._WindowsFlavour.resolve() #17716

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
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


if os.name == 'nt':
from nt import _getfinalpathname
from nt import _getfinalpathname, _getfullpathname
else:
_getfinalpathname = None

Expand Down Expand Up @@ -189,26 +189,26 @@ def compile_pattern(self, pattern):
def resolve(self, path, strict=False):
s = str(path)
if not s:
return path._accessor.getcwd()
s = path._accessor.getcwd()
if _getfinalpathname is None:
return None # Means fallback on absolute
if strict:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 189-191 make a bad assumption that the current working directory (CWD) is a final path. The CWD is an absolute path but not a final path. The assignment should be something like s = str(path) or '.'. In practice it may never be an issue since PurePath.__str__ assigns and returns self._str = self._format_parsed_parts(self._drv, self._root, self._parts) or '.'.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CWD is an absolute path but not a final path.

TIL.

I like the or '.' approach since it avoids special-casing things, but want @pitrou to take a look since this is inherited from the initial pathlib implementation.

return self._ext_to_normal(_getfinalpathname(s))
s = path = _getfullpathname(s)
previous_s = None
if _getfinalpathname is not None:
if strict:
return self._ext_to_normal(_getfinalpathname(s))
tail_parts = [] # End of the path after the first one not found
while True:
try:
s = self._ext_to_normal(_getfinalpathname(s))
except FileNotFoundError:
previous_s = s
s, tail = os.path.split(s)
tail_parts.append(tail)
if previous_s == s:
# Root reached, fallback to _getfullpathname()
return path
else:
tail_parts = [] # End of the path after the first one not found
while True:
try:
s = self._ext_to_normal(_getfinalpathname(s))
except FileNotFoundError:
previous_s = s
s, tail = os.path.split(s)
tail_parts.append(tail)
if previous_s == s:
return path
else:
return os.path.join(s, *reversed(tail_parts))
# Means fallback on absolute
return None
return os.path.join(s, *reversed(tail_parts))

def _split_extended_path(self, s, ext_prefix=ext_namespace_prefix):
prefix = ''
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1799,6 +1799,16 @@ def test_resolve_dot(self):
# Non-strict
self.assertEqual(r.resolve(strict=False), p / '3' / '4')

def test_resolve_nonexist_relative_issue38671(self):
p = self.cls('non', 'exist')

old_cwd = os.getcwd()
os.chdir(BASE)
try:
self.assertEqual(p.resolve(), self.cls(BASE, p))
finally:
os.chdir(old_cwd)

def test_with(self):
p = self.cls(BASE)
it = p.iterdir()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Ensure pathlib.Path.resolve(strict=False) returns an absolute path on
Windows when the path is relative and does not exist.