bpo-40038: pathlib: remove partial support for preserving accessor when modifying a path (GH-19342)

This commit is contained in:
Barney Gale 2021-04-07 01:26:37 +01:00 committed by GitHub
parent 986da8effc
commit 2219187cab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -697,7 +697,7 @@ class PurePath(object):
return cls._flavour.parse_parts(parts) return cls._flavour.parse_parts(parts)
@classmethod @classmethod
def _from_parts(cls, args, init=True): def _from_parts(cls, args):
# We need to call _parse_args on the instance, so as to get the # We need to call _parse_args on the instance, so as to get the
# right flavour. # right flavour.
self = object.__new__(cls) self = object.__new__(cls)
@ -705,18 +705,14 @@ class PurePath(object):
self._drv = drv self._drv = drv
self._root = root self._root = root
self._parts = parts self._parts = parts
if init:
self._init()
return self return self
@classmethod @classmethod
def _from_parsed_parts(cls, drv, root, parts, init=True): def _from_parsed_parts(cls, drv, root, parts):
self = object.__new__(cls) self = object.__new__(cls)
self._drv = drv self._drv = drv
self._root = root self._root = root
self._parts = parts self._parts = parts
if init:
self._init()
return self return self
@classmethod @classmethod
@ -726,10 +722,6 @@ class PurePath(object):
else: else:
return cls._flavour.join(parts) return cls._flavour.join(parts)
def _init(self):
# Overridden in concrete Path
pass
def _make_child(self, args): def _make_child(self, args):
drv, root, parts = self._parse_args(args) drv, root, parts = self._parse_args(args)
drv, root, parts = self._flavour.join_parsed_parts( drv, root, parts = self._flavour.join_parsed_parts(
@ -1069,29 +1061,18 @@ class Path(PurePath):
object. You can also instantiate a PosixPath or WindowsPath directly, object. You can also instantiate a PosixPath or WindowsPath directly,
but cannot instantiate a WindowsPath on a POSIX system or vice versa. but cannot instantiate a WindowsPath on a POSIX system or vice versa.
""" """
__slots__ = ( _accessor = _normal_accessor
'_accessor', __slots__ = ()
)
def __new__(cls, *args, **kwargs): def __new__(cls, *args, **kwargs):
if cls is Path: if cls is Path:
cls = WindowsPath if os.name == 'nt' else PosixPath cls = WindowsPath if os.name == 'nt' else PosixPath
self = cls._from_parts(args, init=False) self = cls._from_parts(args)
if not self._flavour.is_supported: if not self._flavour.is_supported:
raise NotImplementedError("cannot instantiate %r on your system" raise NotImplementedError("cannot instantiate %r on your system"
% (cls.__name__,)) % (cls.__name__,))
self._init()
return self return self
def _init(self,
# Private non-constructor arguments
template=None,
):
if template is not None:
self._accessor = template._accessor
else:
self._accessor = _normal_accessor
def _make_child_relpath(self, part): def _make_child_relpath(self, part):
# This is an optimization used for dir walking. `part` must be # This is an optimization used for dir walking. `part` must be
# a single part relative to this path. # a single part relative to this path.
@ -1192,9 +1173,7 @@ class Path(PurePath):
return self return self
# FIXME this must defer to the specific flavour (and, under Windows, # FIXME this must defer to the specific flavour (and, under Windows,
# use nt._getfullpathname()) # use nt._getfullpathname())
obj = self._from_parts([os.getcwd()] + self._parts, init=False) return self._from_parts([os.getcwd()] + self._parts)
obj._init(template=self)
return obj
def resolve(self, strict=False): def resolve(self, strict=False):
""" """
@ -1210,9 +1189,7 @@ class Path(PurePath):
s = str(self.absolute()) s = str(self.absolute())
# Now we have no symlinks in the path, it's safe to normalize it. # Now we have no symlinks in the path, it's safe to normalize it.
normed = self._flavour.pathmod.normpath(s) normed = self._flavour.pathmod.normpath(s)
obj = self._from_parts((normed,), init=False) return self._from_parts((normed,))
obj._init(template=self)
return obj
def stat(self): def stat(self):
""" """
@ -1284,9 +1261,7 @@ class Path(PurePath):
Return the path to which the symbolic link points. Return the path to which the symbolic link points.
""" """
path = self._accessor.readlink(self) path = self._accessor.readlink(self)
obj = self._from_parts((path,), init=False) return self._from_parts((path,))
obj._init(template=self)
return obj
def touch(self, mode=0o666, exist_ok=True): def touch(self, mode=0o666, exist_ok=True):
""" """