Skip to content

Commit 12d174b

Browse files
bpo-37400: Fix test_os.test_chown() (GH-14374)
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 25fbe33 commit 12d174b

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
@@ -42,15 +42,6 @@
4242
import _winapi
4343
except ImportError:
4444
_winapi = None
45-
try:
46-
import grp
47-
groups = [g.gr_gid for g in grp.getgrall() if getpass.getuser() in g.gr_mem]
48-
if hasattr(os, 'getgid'):
49-
process_gid = os.getgid()
50-
if process_gid not in groups:
51-
groups.append(process_gid)
52-
except ImportError:
53-
groups = []
5445
try:
5546
import pwd
5647
all_users = [u.pw_uid for u in pwd.getpwall()]
@@ -1320,13 +1311,19 @@ def test_chown_uid_gid_arguments_must_be_index(self):
13201311
self.assertIsNone(os.chown(support.TESTFN, uid, gid))
13211312
self.assertIsNone(os.chown(support.TESTFN, -1, -1))
13221313

1323-
@unittest.skipUnless(len(groups) > 1, "test needs more than one group")
1324-
def test_chown(self):
1314+
@unittest.skipUnless(hasattr(os, 'getgroups'), 'need os.getgroups')
1315+
def test_chown_gid(self):
1316+
groups = os.getgroups()
1317+
if len(groups) < 2:
1318+
self.skipTest("test needs at least 2 groups")
1319+
13251320
gid_1, gid_2 = groups[:2]
13261321
uid = os.stat(support.TESTFN).st_uid
1322+
13271323
os.chown(support.TESTFN, uid, gid_1)
13281324
gid = os.stat(support.TESTFN).st_gid
13291325
self.assertEqual(gid, gid_1)
1326+
13301327
os.chown(support.TESTFN, uid, gid_2)
13311328
gid = os.stat(support.TESTFN).st_gid
13321329
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)