mirror of
https://github.com/python/cpython.git
synced 2025-11-09 18:11:38 +00:00
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.
This commit is contained in:
parent
10a925c86d
commit
2bd4ff0700
3 changed files with 11 additions and 17 deletions
|
|
@ -188,9 +188,6 @@ def __reduce__(self):
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "{}({!r})".format(self.__class__.__name__, self.as_posix())
|
return "{}({!r})".format(self.__class__.__name__, self.as_posix())
|
||||||
|
|
||||||
def __fspath__(self):
|
|
||||||
return str(self)
|
|
||||||
|
|
||||||
def __bytes__(self):
|
def __bytes__(self):
|
||||||
"""Return the bytes representation of the path. This is only
|
"""Return the bytes representation of the path. This is only
|
||||||
recommended to use under Unix."""
|
recommended to use under Unix."""
|
||||||
|
|
@ -259,6 +256,9 @@ def __str__(self):
|
||||||
self._tail) or '.'
|
self._tail) or '.'
|
||||||
return self._str
|
return self._str
|
||||||
|
|
||||||
|
__fspath__ = __str__
|
||||||
|
__vfspath__ = __str__
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _format_parsed_parts(cls, drv, root, tail):
|
def _format_parsed_parts(cls, drv, root, tail):
|
||||||
if drv or root:
|
if drv or root:
|
||||||
|
|
|
||||||
|
|
@ -210,24 +210,18 @@ def magic_open(path, mode='r', buffering=-1, encoding=None, errors=None,
|
||||||
raise TypeError(f"{cls.__name__} can't be opened with mode {mode!r}")
|
raise TypeError(f"{cls.__name__} can't be opened with mode {mode!r}")
|
||||||
|
|
||||||
|
|
||||||
def vfspath(path):
|
def vfspath(obj):
|
||||||
"""
|
"""
|
||||||
Return the string representation of a virtual path object.
|
Return the string representation of a virtual path object.
|
||||||
"""
|
"""
|
||||||
|
cls = type(obj)
|
||||||
try:
|
try:
|
||||||
return os.fsdecode(path)
|
vfspath_method = cls.__vfspath__
|
||||||
except TypeError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
path_type = type(path)
|
|
||||||
try:
|
|
||||||
return path_type.__vfspath__(path)
|
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
if hasattr(path_type, '__vfspath__'):
|
cls_name = cls.__name__
|
||||||
raise
|
raise TypeError(f"expected JoinablePath object, not {cls_name}") from None
|
||||||
|
else:
|
||||||
raise TypeError("expected str, bytes, os.PathLike or JoinablePath "
|
return vfspath_method(obj)
|
||||||
"object, not " + path_type.__name__)
|
|
||||||
|
|
||||||
|
|
||||||
def ensure_distinct_paths(source, target):
|
def ensure_distinct_paths(source, target):
|
||||||
|
|
|
||||||
|
|
@ -334,4 +334,4 @@ def symlink_to(self, target, target_is_directory=False):
|
||||||
zinfo.external_attr = stat.S_IFLNK << 16
|
zinfo.external_attr = stat.S_IFLNK << 16
|
||||||
if target_is_directory:
|
if target_is_directory:
|
||||||
zinfo.external_attr |= 0x10
|
zinfo.external_attr |= 0x10
|
||||||
self.zip_file.writestr(zinfo, vfspath(target))
|
self.zip_file.writestr(zinfo, target)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue