Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Restore str force-casting behaviour; reduce diff a little.
  • Loading branch information
barneygale committed Feb 4, 2023
commit e49e7197c18543f8b71c197b20eda7c1858dd1c2
37 changes: 22 additions & 15 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,21 +269,6 @@ def __new__(cls, *args, **kwargs):
def __reduce__(self):
return (self.__class__, (self._fspath,))

def __init__(self, *args):
if not args:
path = ''
elif len(args) == 1:
path = os.fspath(args[0])
else:
path = self._flavour.join(*args)

if not isinstance(path, str):
raise TypeError(
"argument should be a str object or an os.PathLike "
"object returning str, not %r"
% type(path))
self._fspath = path

@classmethod
def _parse_path(cls, path):
if not path:
Expand All @@ -300,6 +285,28 @@ def _parse_path(cls, path):
parsed = [sys.intern(x) for x in unfiltered_parsed if x and x != '.']
return drv, root, parsed

def __init__(self, *args):
parts = []
for a in args:
if isinstance(a, PurePath):
parts += a._parts
else:
a = os.fspath(a)
if isinstance(a, str):
# Force-cast str subclasses to str (issue #21127)
parts.append(str(a))
else:
raise TypeError(
"argument should be a str object or an os.PathLike "
"object returning str, not %r"
% type(a))
if not parts:
self._fspath = ''
elif len(parts) == 1:
self._fspath = os.fspath(parts[0])
else:
self._fspath = self._flavour.join(*parts)

def _load_parts(self):
drv, root, parts = self._parse_path(self._fspath)
self._drv = drv
Expand Down