From c3127b865d7bb8655d4d4e258e1361d75cbf9a4e Mon Sep 17 00:00:00 2001 From: barneygale Date: Sat, 2 Sep 2023 17:00:37 +0100 Subject: [PATCH] Improve `UnsupportedOperation` exception message. --- Lib/pathlib.py | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index b2633fa69c9b11..127a1dbe84da4a 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -803,12 +803,18 @@ class _PathBase(PurePath): __bytes__ = None __fspath__ = None # virtual paths have no local file system representation + def _unsupported(self, method_name): + msg = f"{type(self).__name__}.{method_name}() is unsupported" + if isinstance(self, Path): + msg += " on this system" + raise UnsupportedOperation(msg) + def stat(self, *, follow_symlinks=True): """ Return the result of the stat() system call on this path, like os.stat() does. """ - raise UnsupportedOperation(f"{type(self).__name__}.stat()") + return self._unsupported("stat") def lstat(self): """ @@ -1008,7 +1014,7 @@ def open(self, mode='r', buffering=-1, encoding=None, Open the file pointed by this path and return a file object, as the built-in open() function does. """ - raise UnsupportedOperation(f"{type(self).__name__}.open()") + return self._unsupported("open") def read_bytes(self): """ @@ -1051,7 +1057,7 @@ def iterdir(self): The children are yielded in arbitrary order, and the special entries '.' and '..' are not included. """ - raise UnsupportedOperation(f"{type(self).__name__}.iterdir()") + return self._unsupported("iterdir") def _scandir(self): # os.scandir() returns an object that can be used as a context manager @@ -1218,7 +1224,7 @@ def absolute(self): Use resolve() to resolve symlinks and remove '..' segments. """ - raise UnsupportedOperation(f"{type(self).__name__}.absolute()") + return self._unsupported("absolute") @classmethod def cwd(cls): @@ -1229,7 +1235,7 @@ def expanduser(self): """ Return a new path with expanded ~ and ~user constructs (as returned by os.path.expanduser) """ - raise UnsupportedOperation(f"{type(self).__name__}.expanduser()") + return self._unsupported("expanduser") @classmethod def home(cls): @@ -1241,7 +1247,7 @@ def readlink(self): """ Return the path to which the symbolic link points. """ - raise UnsupportedOperation(f"{type(self).__name__}.readlink()") + return self._unsupported("readlink") def resolve(self, strict=False): """ @@ -1312,7 +1318,7 @@ def symlink_to(self, target, target_is_directory=False): Make this path a symlink pointing to the target path. Note the order of arguments (link, target) is the reverse of os.symlink. """ - raise UnsupportedOperation(f"{type(self).__name__}.symlink_to()") + return self._unsupported("symlink_to") def hardlink_to(self, target): """ @@ -1320,19 +1326,19 @@ def hardlink_to(self, target): Note the order of arguments (self, target) is the reverse of os.link's. """ - raise UnsupportedOperation(f"{type(self).__name__}.hardlink_to()") + return self._unsupported("hardlink_to") def touch(self, mode=0o666, exist_ok=True): """ Create this file with the given access mode, if it doesn't exist. """ - raise UnsupportedOperation(f"{type(self).__name__}.touch()") + return self._unsupported("touch") def mkdir(self, mode=0o777, parents=False, exist_ok=False): """ Create a new directory at this given path. """ - raise UnsupportedOperation(f"{type(self).__name__}.mkdir()") + return self._unsupported("mkdir") def rename(self, target): """ @@ -1344,7 +1350,7 @@ def rename(self, target): Returns the new Path instance pointing to the target path. """ - raise UnsupportedOperation(f"{type(self).__name__}.rename()") + return self._unsupported("rename") def replace(self, target): """ @@ -1356,13 +1362,13 @@ def replace(self, target): Returns the new Path instance pointing to the target path. """ - raise UnsupportedOperation(f"{type(self).__name__}.replace()") + return self._unsupported("replace") def chmod(self, mode, *, follow_symlinks=True): """ Change the permissions of the path, like os.chmod(). """ - raise UnsupportedOperation(f"{type(self).__name__}.chmod()") + return self._unsupported("chmod") def lchmod(self, mode): """ @@ -1376,29 +1382,29 @@ def unlink(self, missing_ok=False): Remove this file or link. If the path is a directory, use rmdir() instead. """ - raise UnsupportedOperation(f"{type(self).__name__}.unlink()") + return self._unsupported("unlink") def rmdir(self): """ Remove this directory. The directory must be empty. """ - raise UnsupportedOperation(f"{type(self).__name__}.rmdir()") + return self._unsupported("rmdir") def owner(self): """ Return the login name of the file owner. """ - raise UnsupportedOperation(f"{type(self).__name__}.owner()") + return self._unsupported("owner") def group(self): """ Return the group name of the file gid. """ - raise UnsupportedOperation(f"{type(self).__name__}.group()") + return self._unsupported("group") def as_uri(self): """Return the path as a URI.""" - raise UnsupportedOperation(f"{type(self).__name__}.as_uri()") + return self._unsupported("as_uri") class Path(_PathBase):