GH-128520: pathlib ABCs: tweak protocol for virtual path strings (#13… · python/cpython@2bd4ff0

GitHub

Original file line numberDiff line numberDiff line change@@ -188,9 +188,6 @@ def __reduce__(self):

188188def__repr__(self):

189189return"{}({!r})".format(self.__class__.__name__, self.as_posix())

190190191-def__fspath__(self):

192-returnstr(self)

193-194191def__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):

259256self._tail) or'.'

260257returnself._str

261258259+__fspath__=__str__

260+__vfspath__=__str__

261+262262@classmethod

263263def_format_parsed_parts(cls, drv, root, tail):

264264ifdrvorroot:

Original file line numberDiff line numberDiff line change@@ -210,24 +210,18 @@ def magic_open(path, mode='r', buffering=-1, encoding=None, errors=None,

210210raiseTypeError(f"{cls.__name__} can't be opened with mode {mode!r}")

211211212212213-defvfspath(path):

213+defvfspath(obj):

214214"""

215215 Return the string representation of a virtual path object.

216216 """

217+cls=type(obj)

217218try:

218-returnos.fsdecode(path)

219-exceptTypeError:

220-pass

221-222-path_type=type(path)

223-try:

224-returnpath_type.__vfspath__(path)

219+vfspath_method=cls.__vfspath__

225220exceptAttributeError:

226-ifhasattr(path_type, '__vfspath__'):

227-raise

228-229-raiseTypeError("expected str, bytes, os.PathLike or JoinablePath "

230-"object, not "+path_type.__name__)

221+cls_name=cls.__name__

222+raiseTypeError(f"expected JoinablePath object, not {cls_name}") fromNone

223+else:

224+returnvfspath_method(obj)

231225232226233227defensure_distinct_paths(source, target):

Original file line numberDiff line numberDiff line change@@ -334,4 +334,4 @@ def symlink_to(self, target, target_is_directory=False):

334334zinfo.external_attr=stat.S_IFLNK<<16

335335iftarget_is_directory:

336336zinfo.external_attr|=0x10

337-self.zip_file.writestr(zinfo, vfspath(target))

337+self.zip_file.writestr(zinfo, target)