diff options
author | Barney Gale <barney.gale@gmail.com> | 2020-04-17 16:41:07 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-17 16:41:07 (GMT) |
commit | 22386bb4ef740ee92d34c87b8cb90d681423a853 (patch) | |
tree | 60349bdbc02e6efa902b3a21fd63db74118646e7 /Lib/pathlib.py | |
parent | 06a35542aad15666cace307d841a95e33f3cbee6 (diff) | |
download | cpython-22386bb4ef740ee92d34c87b8cb90d681423a853.zip cpython-22386bb4ef740ee92d34c87b8cb90d681423a853.tar.gz cpython-22386bb4ef740ee92d34c87b8cb90d681423a853.tar.bz2 |
bpo-39901: Move `pathlib.Path.owner()` and `group()` implementations into the path accessor. (GH-18844)
Diffstat (limited to 'Lib/pathlib.py')
-rw-r--r-- | Lib/pathlib.py | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 96b8b59..d2053e6 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -447,6 +447,20 @@ class _NormalAccessor(_Accessor): def readlink(self, path): return os.readlink(path) + 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") + + 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") + _normal_accessor = _NormalAccessor() @@ -1202,15 +1216,13 @@ class Path(PurePath): """ Return the login name of the file owner. """ - import pwd - return pwd.getpwuid(self.stat().st_uid).pw_name + return self._accessor.owner(self) def group(self): """ Return the group name of the file gid. """ - import grp - return grp.getgrgid(self.stat().st_gid).gr_name + return self._accessor.group(self) def open(self, mode='r', buffering=-1, encoding=None, errors=None, newline=None): @@ -1544,11 +1556,5 @@ class WindowsPath(Path, PureWindowsPath): """ __slots__ = () - def owner(self): - raise NotImplementedError("Path.owner() is unsupported on this system") - - def group(self): - raise NotImplementedError("Path.group() is unsupported on this system") - def is_mount(self): raise NotImplementedError("Path.is_mount() is unsupported on this system") |