8000 bpo-38671: Make sure to return an absolute path in pathlib._WindowsFlavour.resolve() by uranusjr · Pull Request #17716 · python/cpython · GitHub
[go: up one dir, main page]

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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use early returns to reduce indentation level
  • Loading branch information
uranusjr committed Apr 27, 2021
commit 612b7f6c4a8f7175a7d3d85842408be3bc92ad0a
36 changes: 17 additions & 19 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,27 +189,25 @@ 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 = os.path.abspath(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 abspath()
return path
else:
s = path = os.path.abspath(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:
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
0