Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 12 additions & 1 deletion Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
from operator import attrgetter
from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO
from urllib.parse import quote_from_bytes as urlquote_from_bytes
try:
from nt import _getfullpathname
except ImportError:
_getfullpathname = None


__all__ = [
Expand Down Expand Up @@ -827,7 +831,14 @@ def absolute(self):
"""
if self.is_absolute():
return self
return self._from_parts([os.getcwd()] + self._parts)
elif self._drv and _getfullpathname:
try:
cwd = _getfullpathname(self._drv)
except (ValueError, OSError):
cwd = os.getcwd()
else:
cwd = os.getcwd()
return self._from_parts([cwd] + self._parts)

def resolve(self, strict=False):
"""
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 @@ -3002,6 +3002,16 @@ def test_absolute(self):
self.assertEqual(str(P('a', 'b', 'c').absolute()),
os.path.join(share, 'a', 'b', 'c'))

drive = os.path.splitdrive(BASE)[0]
with os_helper.change_cwd(BASE):
# Relative path with drive
self.assertEqual(str(P(drive).absolute()), BASE)
self.assertEqual(str(P(drive + 'foo').absolute()), os.path.join(BASE, 'foo'))

# Relative path with root
self.assertEqual(str(P('\\').absolute()), drive + '\\')
self.assertEqual(str(P('\\foo').absolute()), drive + '\\foo')


def test_glob(self):
P = self.cls
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix handling of drive-relative paths (like 'C:' and 'C:foo') in
:meth:`pathlib.Path.absolute`. This method now calls
``nt._getfullpathname()`` on Windows.