-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-64978: Add chown()
to pathlib.Path
#31212
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
Changes from all commits
8906802
5bb6b63
dbdbbbe
d4070a3
f9a611b
06aebeb
cfae2d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1127,6 +1127,12 @@ def lchmod(self, mode): | |||||||||
""" | ||||||||||
self.chmod(mode, follow_symlinks=False) | ||||||||||
|
||||||||||
def chown(self, uid, gid, *, follow_symlinks=True): | ||||||||||
""" | ||||||||||
Change the owner and group id of path to the numeric uid and gid, like os.chown(). | ||||||||||
""" | ||||||||||
Comment on lines
+1131
to
+1133
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please follow PEP 8.
Suggested change
|
||||||||||
os.chown(self, uid, gid, follow_symlinks=follow_symlinks) | ||||||||||
|
||||||||||
def unlink(self, missing_ok=False): | ||||||||||
""" | ||||||||||
Remove this file or link. | ||||||||||
|
@@ -1393,3 +1399,6 @@ class WindowsPath(Path, PureWindowsPath): | |||||||||
|
||||||||||
def is_mount(self): | ||||||||||
raise NotImplementedError("Path.is_mount() is unsupported on this system") | ||||||||||
|
||||||||||
def chown(self, uid, gid, *, follow_symlinks=True): | ||||||||||
raise NotImplementedError("Path.chown() is unsupported on this system") |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -21,6 +21,15 @@ | |||||||||||||||
except ImportError: | ||||||||||||||||
grp = pwd = None | ||||||||||||||||
|
||||||||||||||||
try: | ||||||||||||||||
import pwd | ||||||||||||||||
all_users = [u.pw_uid for u in pwd.getpwall()] | ||||||||||||||||
except (ImportError, AttributeError): | ||||||||||||||||
all_users = [] | ||||||||||||||||
Comment on lines
+24
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks to be a copy-and-paste from |
||||||||||||||||
|
||||||||||||||||
root_in_posix = False | ||||||||||||||||
if hasattr(os, 'geteuid'): | ||||||||||||||||
root_in_posix = (os.geteuid() == 0) | ||||||||||||||||
Comment on lines
+30
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This looks to be a copy-and-paste from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure I can move it and use it from there as a helper module ... |
||||||||||||||||
|
||||||||||||||||
class _BaseFlavourTest(object): | ||||||||||||||||
|
||||||||||||||||
|
@@ -1874,6 +1883,50 @@ def test_chmod(self): | |||||||||||||||
p.chmod(new_mode) | ||||||||||||||||
self.assertEqual(p.stat().st_mode, new_mode) | ||||||||||||||||
|
||||||||||||||||
@unittest.skipUnless(root_in_posix and len(all_users) > 1, | ||||||||||||||||
"test needs root privilege and more than one user") | ||||||||||||||||
def test_chown_with_root(self): | ||||||||||||||||
# original uid and gid | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please follow PEP 8.
Suggested change
|
||||||||||||||||
p = self.cls(BASE) / 'fileA' | ||||||||||||||||
uid = p.stat().st_uid | ||||||||||||||||
gid = p.stat().st_gid | ||||||||||||||||
|
||||||||||||||||
# get users and groups for testing | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
uid_1, uid_2 = all_users[:2] | ||||||||||||||||
groups = os.getgroups() | ||||||||||||||||
if len(groups) < 2: | ||||||||||||||||
self.skipTest("test needs at least 2 groups") | ||||||||||||||||
gid_1, gid_2 = groups[:2] | ||||||||||||||||
|
||||||||||||||||
p.chown(uid=uid_1, gid=gid_1) | ||||||||||||||||
self.assertEqual(p.stat().st_uid, uid_1) | ||||||||||||||||
self.assertEqual(p.stat().st_gid, gid_1) | ||||||||||||||||
p.chown(uid=uid_2, gid=gid_2) | ||||||||||||||||
self.assertEqual(p.stat().st_uid, uid_2) | ||||||||||||||||
self.assertEqual(p.stat().st_gid, gid_2) | ||||||||||||||||
|
||||||||||||||||
# Set back to original | ||||||||||||||||
p.chown(uid=uid, gid=gid) | ||||||||||||||||
|
||||||||||||||||
@unittest.skipUnless(not root_in_posix and len(all_users) > 1, | ||||||||||||||||
"test needs non-root account and more than one user") | ||||||||||||||||
def test_chown_without_permission(self): | ||||||||||||||||
p = self.cls(BASE) / 'fileA' | ||||||||||||||||
|
||||||||||||||||
new_uid = 503 | ||||||||||||||||
new_gid = 503 | ||||||||||||||||
with self.assertRaises(PermissionError): | ||||||||||||||||
p.chown(uid=new_uid, gid=new_gid) | ||||||||||||||||
|
||||||||||||||||
@only_nt | ||||||||||||||||
def test_chown_windows(self): | ||||||||||||||||
p = self.cls(BASE) / 'fileA' | ||||||||||||||||
|
||||||||||||||||
new_uid = 503 | ||||||||||||||||
new_gid = 503 | ||||||||||||||||
with self.assertRaises(NotImplementedError): | ||||||||||||||||
p.chown(uid=new_uid, gid=new_gid) | ||||||||||||||||
|
||||||||||||||||
# On Windows, os.chmod does not follow symlinks (issue #15411) | ||||||||||||||||
@only_posix | ||||||||||||||||
def test_chmod_follow_symlinks_true(self): | ||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Add :meth:`pathlib.Path.chown` method that changes file ownership. | ||
Patch by Jaspar Stach. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.