Skip to content
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
Use nt._getfullpathname instead of os.path.abspath
  • Loading branch information
uranusjr committed Apr 27, 2021
commit 8f20c10011e42757a881e1bbdd8f929ef144b458
7 changes: 4 additions & 3 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 @@ -194,7 +194,7 @@ def resolve(self, path, strict=False):
return None # Means fallback on absolute
Comment thread
uranusjr marked this conversation as resolved.
Outdated
if strict:

Copy link
Copy Markdown
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
Copy Markdown
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)
s = path = _getfullpathname(s)
previous_s = None
tail_parts = [] # End of the path after the first one not found
while True:
Expand All @@ -204,7 +204,8 @@ def resolve(self, path, strict=False):
previous_s = s
s, tail = os.path.split(s)
tail_parts.append(tail)
if previous_s == s: # Root reached, fallback to abspath()
if previous_s == s:
# Root reached, fallback to _getfullpathname()
return path
else:
return os.path.join(s, *reversed(tail_parts))
Expand Down