Skip to content

Commit 2bd4ff0

Browse files
authored
GH-128520: pathlib ABCs: tweak protocol for virtual path strings (#134104)
Adjust `pathlib._os.vfspath()` so that it doesn't try `os.fsdecode()`. I don't know that supporting `os.PathLike` arguments is a good idea, so it's best to leave it out for now.
1 parent 10a925c commit 2bd4ff0

File tree

3 files changed

+11
-17
lines changed

3 files changed

+11
-17
lines changed

Lib/pathlib/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,6 @@ def __reduce__(self):
188188
def __repr__(self):
189189
return "{}({!r})".format(self.__class__.__name__, self.as_posix())
190190

191-
def __fspath__(self):
192-
return str(self)
193-
194191
def __bytes__(self):
195192
"""Return the bytes representation of the path. This is only
196193
recommended to use under Unix."""
@@ -259,6 +256,9 @@ def __str__(self):
259256
self._tail) or '.'
260257
return self._str
261258

259+
__fspath__ = __str__
260+
__vfspath__ = __str__
261+
262262
@classmethod
263263
def _format_parsed_parts(cls, drv, root, tail):
264264
if drv or root:

Lib/pathlib/_os.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -210,24 +210,18 @@ def magic_open(path, mode='r', buffering=-1, encoding=None, errors=None,
210210
raise TypeError(f"{cls.__name__} can't be opened with mode {mode!r}")
211211

212212

213-
def vfspath(path):
213+
def vfspath(obj):
214214
"""
215215
Return the string representation of a virtual path object.
216216
"""
217+
cls = type(obj)
217218
try:
218-
return os.fsdecode(path)
219-
except TypeError:
220-
pass
221-
222-
path_type = type(path)
223-
try:
224-
return path_type.__vfspath__(path)
219+
vfspath_method = cls.__vfspath__
225220
except AttributeError:
226-
if hasattr(path_type, '__vfspath__'):
227-
raise
228-
229-
raise TypeError("expected str, bytes, os.PathLike or JoinablePath "
230-
"object, not " + path_type.__name__)
221+
cls_name = cls.__name__
222+
raise TypeError(f"expected JoinablePath object, not {cls_name}") from None
223+
else:
224+
return vfspath_method(obj)
231225

232226

233227
def ensure_distinct_paths(source, target):

Lib/test/test_pathlib/support/zip_path.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,4 +334,4 @@ def symlink_to(self, target, target_is_directory=False):
334334
zinfo.external_attr = stat.S_IFLNK << 16
335335
if target_is_directory:
336336
zinfo.external_attr |= 0x10
337-
self.zip_file.writestr(zinfo, vfspath(target))
337+
self.zip_file.writestr(zinfo, target)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy