gh-155358: Use named attributes with pwd and grp modules (#155362)

* Replace pwd[0] with pwd.pw_name
* Replace pwd[2] with pwd.pw_uid
* Replace grp[0] with grp.gr_name
* Replace grp[2] with grp.gr_gid
* Replace pwd[3] with pwd.pw_gid
This commit is contained in:
Victor Stinner authored and GitHub committed 2026-08-10 20:30:40 +02:00
1 parent 38235c0e80
commit f8cfa0cd59
13 files changed
+27 -24

No files matched your search

+1 -1
View File
@@ -452,7 +452,7 @@ process and user.
process. For most purposes, it is more useful to use
:func:`getpass.getuser` since the latter checks the environment variables
:envvar:`LOGNAME` or :envvar:`USERNAME` to find out who the user is, and
falls back to ``pwd.getpwuid(os.getuid())[0]`` to get the login name of the
falls back to ``pwd.getpwuid(os.getuid()).pw_name`` to get the login name of the
current real user id.
.. availability:: Unix, Windows, not WASI.
+1 -1
View File
@@ -428,7 +428,7 @@ def getuser():
try:
import pwd
return pwd.getpwuid(os.getuid())[0]
return pwd.getpwuid(os.getuid()).pw_name
except (ImportError, KeyError) as e:
raise OSError('No username set in the environment') from e
+1 -1
View File
@@ -1013,7 +1013,7 @@ def nobody_uid():
except ImportError:
return -1
try:
nobody = pwd.getpwnam('nobody')[2]
nobody = pwd.getpwnam('nobody').pw_uid
except KeyError:
nobody = 1 + max(x[2] for x in pwd.getpwall())
return nobody
+1 -1
View File
@@ -15,7 +15,7 @@ def _can_security_check():
def _getpwuid(uid):
try:
import pwd
return pwd.getpwuid(uid)[0]
return pwd.getpwuid(uid).pw_name
except (ImportError, LookupError):
return f'uid {uid}'
+2 -2
View File
@@ -983,7 +983,7 @@ def _get_gid(name):
except KeyError:
result = None
if result is not None:
return result[2]
return result.gr_gid
return None
def _get_uid(name):
@@ -1001,7 +1001,7 @@ def _get_uid(name):
except KeyError:
result = None
if result is not None:
return result[2]
return result.pw_uid
return None
def _make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,
+4 -4
View File
@@ -2282,14 +2282,14 @@ class TarFile(object):
if pwd:
if tarinfo.uid not in self._unames:
try:
self._unames[tarinfo.uid] = pwd.getpwuid(tarinfo.uid)[0]
self._unames[tarinfo.uid] = pwd.getpwuid(tarinfo.uid).pw_name
except KeyError:
self._unames[tarinfo.uid] = ''
tarinfo.uname = self._unames[tarinfo.uid]
if grp:
if tarinfo.gid not in self._gnames:
try:
self._gnames[tarinfo.gid] = grp.getgrgid(tarinfo.gid)[0]
self._gnames[tarinfo.gid] = grp.getgrgid(tarinfo.gid).gr_name
except KeyError:
self._gnames[tarinfo.gid] = ''
tarinfo.gname = self._gnames[tarinfo.gid]
@@ -2837,12 +2837,12 @@ class TarFile(object):
if not numeric_owner:
try:
if grp and tarinfo.gname:
g = grp.getgrnam(tarinfo.gname)[2]
g = grp.getgrnam(tarinfo.gname).gr_gid
except KeyError:
pass
try:
if pwd and tarinfo.uname:
u = pwd.getpwnam(tarinfo.uname)[2]
u = pwd.getpwnam(tarinfo.uname).pw_uid
except KeyError:
pass
if g is None:
+1 -1
View File
@@ -862,7 +862,7 @@ if __name__ == '__main__':
except ImportError:
print('Cannot import module "pwd"; try running with -n option.', file=sys.stderr)
sys.exit(1)
nobody = pwd.getpwnam('nobody')[2]
nobody = pwd.getpwnam('nobody').pw_uid
try:
os.setuid(nobody)
except PermissionError:
+4 -1
View File
@@ -39,10 +39,13 @@ class GetpassGetuserTest(unittest.TestCase):
expected_name = 'some_name'
environ.get.return_value = None
if pwd:
class User:
pass
with mock.patch('os.getuid') as uid, \
mock.patch('pwd.getpwuid') as getpw:
uid.return_value = 42
getpw.return_value = [expected_name]
getpw.return_value = User()
getpw.return_value.pw_name = expected_name
self.assertEqual(expected_name,
getpass.getuser())
getpw.assert_called_once_with(42)
+2 -2
View File
@@ -1322,8 +1322,8 @@ class PosixTester(unittest.TestCase):
@unittest.skipUnless(hasattr(pwd, 'getpwuid'), "test needs pwd.getpwuid()")
@unittest.skipUnless(hasattr(os, 'getuid'), "test needs os.getuid()")
def test_getgrouplist(self):
user = pwd.getpwuid(os.getuid())[0]
group = pwd.getpwuid(os.getuid())[3]
user = pwd.getpwuid(os.getuid()).pw_name
group = pwd.getpwuid(os.getuid()).pw_gid
self.assertIn(group, posix.getgrouplist(user, group))
+1 -1
View File
@@ -50,7 +50,7 @@ class PwdTest(unittest.TestCase):
# check whether the entry returned by getpwuid()
# for each uid is among those from getpwall() for this uid
for e in entries:
if not e[0] or e[0] == '+':
if not e.pw_name or e.pw_name == '+':
continue # skip NIS entries etc.
self.assertIn(pwd.getpwnam(e.pw_name), entriesbyname[e.pw_name])
self.assertIn(pwd.getpwuid(e.pw_uid), entriesbyuid[e.pw_uid])
+6 -6
View File
@@ -1999,8 +1999,8 @@ class TestArchives(BaseTest, unittest.TestCase):
# testing make_archive with owner and group, with various combinations
# this works even if there's not gid/uid support
if UID_GID_SUPPORT:
group = grp.getgrgid(0)[0]
owner = pwd.getpwuid(0)[0]
group = grp.getgrgid(0).gr_name
owner = pwd.getpwuid(0).pw_name
else:
group = owner = 'root'
@@ -2027,8 +2027,8 @@ class TestArchives(BaseTest, unittest.TestCase):
def test_tarfile_root_owner(self):
root_dir, base_dir = self._create_files()
base_name = os.path.join(self.mkdtemp(), 'archive')
group = grp.getgrgid(0)[0]
owner = pwd.getpwuid(0)[0]
group = grp.getgrgid(0).gr_name
owner = pwd.getpwuid(0).pw_name
with os_helper.change_cwd(root_dir), no_chdir:
archive_name = make_archive(base_name, 'gztar', root_dir, 'dist',
owner=owner, group=group)
@@ -2433,8 +2433,8 @@ class TestMisc(BaseTest, unittest.TestCase):
check_chown(dirname, gid=gid)
try:
user = pwd.getpwuid(uid)[0]
group = grp.getgrgid(gid)[0]
user = pwd.getpwuid(uid).pw_name
group = grp.getgrgid(gid).gr_name
except KeyError:
# On some systems uid/gid cannot be resolved.
pass
+2 -2
View File
@@ -3351,12 +3351,12 @@ def root_is_uid_gid_0():
except ImportError:
return False
try:
if pwd.getpwuid(0)[0] != 'root':
if pwd.getpwuid(0).pw_name != 'root':
return False
except KeyError:
# On Cygwin, there is no root user (uid 0)
return False
if grp.getgrgid(0)[0] != 'root':
if grp.getgrgid(0).gr_name != 'root':
return False
return True
+1 -1
View File
@@ -411,7 +411,7 @@ def _get_user_info(user):
if user is None:
uid = os.geteuid()
#username = os.getlogin()
username = pwd.getpwuid(uid)[0]
username = pwd.getpwuid(uid).pw_name
gid = os.getgid()
groups = os.getgroups()
else: