bpo-39901: Move `pathlib.Path.owner()` and `group()` implementations into the path accessor. (GH-18844)

This commit is contained in:
Barney Gale 2020-04-17 17:41:07 +01:00 committed by GitHub
parent 06a35542aa
commit 22386bb4ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 10 deletions

View File

@ -447,6 +447,20 @@ class _NormalAccessor(_Accessor):
def readlink(self, path): def readlink(self, path):
return os.readlink(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() _normal_accessor = _NormalAccessor()
@ -1202,15 +1216,13 @@ class Path(PurePath):
""" """
Return the login name of the file owner. Return the login name of the file owner.
""" """
import pwd return self._accessor.owner(self)
return pwd.getpwuid(self.stat().st_uid).pw_name
def group(self): def group(self):
""" """
Return the group name of the file gid. Return the group name of the file gid.
""" """
import grp return self._accessor.group(self)
return grp.getgrgid(self.stat().st_gid).gr_name
def open(self, mode='r', buffering=-1, encoding=None, def open(self, mode='r', buffering=-1, encoding=None,
errors=None, newline=None): errors=None, newline=None):
@ -1544,11 +1556,5 @@ class WindowsPath(Path, PureWindowsPath):
""" """
__slots__ = () __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): def is_mount(self):
raise NotImplementedError("Path.is_mount() is unsupported on this system") raise NotImplementedError("Path.is_mount() is unsupported on this system")