Skip to content

Commit 4761f54

Browse files
committed
Tighten up vfspath()
1 parent e76de04 commit 4761f54

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

Lib/pathlib/_os.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,18 +215,25 @@ def vfspath(path):
215215
Return the string representation of a virtual path object.
216216
"""
217217
try:
218-
return os.fspath(path)
218+
path_str = os.fspath(path)
219219
except TypeError:
220220
pass
221-
cls = type(path)
221+
else:
222+
if isinstance(path_str, str):
223+
return path_str
224+
225+
path_type = type(path)
222226
try:
223-
attr = cls.__vfspath__
227+
path_str = path_type.__vfspath__(path)
224228
except AttributeError:
225-
pass
229+
if hasattr(path_type, '__fspath__'):
230+
raise
226231
else:
227-
return attr(path)
232+
if isinstance(path_str, str):
233+
return path_str
228234

229-
raise TypeError(f"{cls.__name__} can't be converted to a path")
235+
raise TypeError("expected str, os.PathLike[str] or JoinablePath "
236+
"object, not " + path_type.__name__)
230237

231238

232239
def ensure_distinct_paths(source, target):
@@ -334,7 +341,7 @@ class _PathInfoBase:
334341
__slots__ = ('_path', '_stat_result', '_lstat_result')
335342

336343
def __init__(self, path):
337-
self._path = os.fspath(path)
344+
self._path = str(path)
338345

339346
def __repr__(self):
340347
path_type = "WindowsPath" if os.name == "nt" else "PosixPath"

Lib/test/test_pathlib/test_join_windows.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from .support import is_pypi
99
from .support.lexical_path import LexicalWindowsPath
1010

11-
1211
if is_pypi:
1312
from pathlib_abc import vfspath
1413
else:

0 commit comments

Comments
 (0)