Skip to content

Commit 1d4b6ba

Browse files
bpo-37400: Fix test_os.test_chown() (GH-14374) (GH-14378)
Use os.getgroups() rather than grp.getgrall() to get groups. Rename also the test to test_chown_gid(). (cherry picked from commit d7c87d9) Co-authored-by: Victor Stinner <[email protected]>
1 parent 01b2394 commit 1d4b6ba

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

Lib/test/test_os.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,6 @@
4141
import _winapi
4242
except ImportError:
4343
_winapi = None
44-
try:
45-
import grp
46-
groups = [g.gr_gid for g in grp.getgrall() if getpass.getuser() in g.gr_mem]
47-
if hasattr(os, 'getgid'):
48-
process_gid = os.getgid()
49-
if process_gid not in groups:
50-
groups.append(process_gid)
51-
except ImportError:
52-
groups = []
5344
try:
5445
import pwd
5546
all_users = [u.pw_uid for u in pwd.getpwall()]
@@ -1238,13 +1229,19 @@ def test_chown_uid_gid_arguments_must_be_index(self):
12381229
self.assertIsNone(os.chown(support.TESTFN, uid, gid))
12391230
self.assertIsNone(os.chown(support.TESTFN, -1, -1))
12401231

1241-
@unittest.skipUnless(len(groups) > 1, "test needs more than one group")
1242-
def test_chown(self):
1232+
@unittest.skipUnless(hasattr(os, 'getgroups'), 'need os.getgroups')
1233+
def test_chown_gid(self):
1234+
groups = os.getgroups()
1235+
if len(groups) < 2:
1236+
self.skipTest("test needs at least 2 groups")
1237+
12431238
gid_1, gid_2 = groups[:2]
12441239
uid = os.stat(support.TESTFN).st_uid
1240+
12451241
os.chown(support.TESTFN, uid, gid_1)
12461242
gid = os.stat(support.TESTFN).st_gid
12471243
self.assertEqual(gid, gid_1)
1244+
12481245
os.chown(support.TESTFN, uid, gid_2)
12491246
gid = os.stat(support.TESTFN).st_gid
12501247
self.assertEqual(gid, gid_2)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix test_os.test_chown(): use os.getgroups() rather than grp.getgrall()
2+
to get groups. Rename also the test to test_chown_gid().

0 commit comments

Comments
 (0)