Skip to content

Commit 22386bb

Browse files
authored
bpo-39901: Move pathlib.Path.owner() and group() implementations into the path accessor. (GH-18844)
1 parent 06a3554 commit 22386bb

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

Lib/pathlib.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,20 @@ def symlink(a, b, target_is_directory):
447447
def readlink(self, path):
448448
return os.readlink(path)
449449

450+
def owner(self, path):
451+
try:
452+
import pwd
453+
return pwd.getpwuid(self.stat(path).st_uid).pw_name
454+
except ImportError:
455+
raise NotImplementedError("Path.owner() is unsupported on this system")
456+
457+
def group(self, path):
458+
try:
459+
import grp
460+
return grp.getgrgid(self.stat(path).st_gid).gr_name
461+
except ImportError:
462+
raise NotImplementedError("Path.group() is unsupported on this system")
463+
450464

451465
_normal_accessor = _NormalAccessor()
452466

@@ -1202,15 +1216,13 @@ def owner(self):
12021216
"""
12031217
Return the login name of the file owner.
12041218
"""
1205-
import pwd
1206-
return pwd.getpwuid(self.stat().st_uid).pw_name
1219+
return self._accessor.owner(self)
12071220

12081221
def group(self):
12091222
"""
12101223
Return the group name of the file gid.
12111224
"""
1212-
import grp
1213-
return grp.getgrgid(self.stat().st_gid).gr_name
1225+
return self._accessor.group(self)
12141226

12151227
def open(self, mode='r', buffering=-1, encoding=None,
12161228
errors=None, newline=None):
@@ -1544,11 +1556,5 @@ class WindowsPath(Path, PureWindowsPath):
15441556
"""
15451557
__slots__ = ()
15461558

1547-
def owner(self):
1548-
raise NotImplementedError("Path.owner() is unsupported on this system")
1549-
1550-
def group(self):
1551-
raise NotImplementedError("Path.group() is unsupported on this system")
1552-
15531559
def is_mount(self):
15541560
raise NotImplementedError("Path.is_mount() is unsupported on this system")

0 commit comments

Comments
 (0)