8000 bpo-24132: Direct sub-classing of pathlib.Path by kfollstad · Pull Request #26438 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-24132: Direct sub-classing of pathlib.Path #26438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
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
bpo-24132: Replace additional hardcoded references in pathlib
Replace hardcorded references to pathlib.Path so that the base
class and factory functionality of Path can be seperated.
  • Loading branch information
kfollstad committed May 28, 2021
commit ed5f237ca46d150cc2343e64814453be8f54267d
36 changes: 20 additions & 16 deletions Lib/pathlib.py
< 44E8 td id="diff-fa525485738fc33d05b06c159172ff1f319c26e88d8c6bb39f7dbaae4dc4105cL1376" data-line-number="1376" class="blob-num blob-num-context js-linkable-line-number">
Original file line number Diff line number Diff line change
Expand Up @@ -326,21 +326,24 @@ def touch(self, path, mode=0o666, exist_ok=True):
readlink = os.readlink
else:
def readlink(self, path):
raise NotImplementedError("os.readlink() not available on this system")
raise NotImplementedError("os.readlink() is not available "
"on this system")

def owner(self, path):
try:
import pwd
return pwd.getpwuid(self.stat(path).st_uid).pw_name
except ImportError:
raise NotImplementedError("Path.owner() is unsupported on this system")
raise NotImplementedError(f"{self.__class__.__name__}.owner() "
f"is unsupported on this system")

def group(self, path):
try:
import grp
return grp.getgrgid(self.stat(path).st_gid).gr_name
except ImportError:
raise NotImplementedError("Path.group() is unsupported on this system")
raise NotImplementedError(f"{self.__class__.__name__}.group() "
f"is unsupported on this system")

getcwd = os.getcwd

Expand Down Expand Up @@ -1012,7 +1015,7 @@ def __exit__(self, t, v, tb):
# In previous versions of pathlib, this method marked this path as
# closed; subsequent attempts to perform I/O would raise an IOError.
# This functionality was never documented, and had the effect of
# making Path objects mutable, contrary to PEP 428. In Python 3.9 the
# making path objects mutable, contrary to PEP 428. In Python 3.9 the
# _closed attribute was removed, and this method made a no-op.
# This method and __enter__()/__exit__() should be deprecated and
# removed in the future.
Expand Down Expand Up @@ -1059,7 +1062,7 @@ def glob(self, pattern):
"""Iterate over this subtree and yield all existing files (of any
kind, including directories) matching the given relative pattern.
"""
sys.audit("pathlib.Path.glob", self, pattern)
sys.audit(f"pathlib.{self.__class__.__name__}.glob", self, pattern)
if not pattern:
raise ValueError("Unacceptable pattern: {!r}".format(pattern))
drv, root, pattern_parts = self._flavour.parse_parts((pattern,))
Expand All @@ -1074,7 +1077,7 @@ def rglob(self, pattern):
directories) matching the given relative pattern, anywhere in
this subtree.
"""
sys.audit("pathlib.Path.rglob", self, pattern)
sys.audit(f"pathlib.{self.__class__.__name__}.rglob", self, pattern)
drv, root, pattern_parts = self._flavour.parse_parts((pattern,))
if drv or root:
raise NotImplementedError("Non-relative patterns are unsupported")
Expand Down Expand Up @@ -1262,9 +1265,9 @@ def rename(self, target):

The target path may be absolute or relative. Relative paths are
interpreted relative to the current working directory, *not* the
directory of the Path object.
directory of this object.

Returns the new Path instance pointing to the target path.
Returns the new class instance pointing to the target path.
"""
self._accessor.rename(self, target)
return self.__class__(target)
Expand All @@ -1275,9 +1278,9 @@ def replace(self, target):

The target path may be absolute or relative. Relative paths are
interpreted relative to the current working directory, *not* the
directory of the Path object.
directory of this object.

Returns the new Path instance pointing to the target path.
Returns the new class instance pointing to the target path.
"""
self._accessor.replace(self, target)
return self.__class__(target)
Expand All @@ -1303,15 +1306,16 @@ def link_to(self, target):

Note this function does not make this path a hard link to *target*,
despite the implication of the function and argument names. The order
of arguments (target, link) is the reverse of Path.symlink_to, but
of arguments (target, link) is the reverse of symlink_to, but
matches that of os.link.

Deprecated since Python 3.10 and scheduled for removal in Python 3.12.
Use `hardlink_to()` instead.
"""
warnings.warn("pathlib.Path.link_to() is deprecated and is scheduled "
"for removal in Python 3.12. "
"Use pathlib.Path.hardlink_to() instead.",
classname = self.__class__.__name__
warnings.warn(f"pathlib.{classname}.link_to() is deprecated and is "
f"scheduled for removal in Python 3.12. "
f"Use pathlib.{classname}.hardlink_to() instead.",
DeprecationWarning, stacklevel=2)
self._accessor.link(self, target)

Expand Down Expand Up @@ -1370,8 +1374,8 @@ def is_mount(self):
Check if this path is a POSIX mount point
"""
if os.name != "posix":
raise NotImplementedError("Path.is_mount() is "
"unsupported on this system")
raise NotImplementedError(f"{self.__class__.__name__}.is_mount() "
f"is unsupported on this system")
# Need to exist and be a dir
if not self.exists() or not self.is_dir():
return False
Expand Down
0