Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Doc/library/os.rst
Original file line numberDiff line numberDiff line change
Expand Up@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Lib/getpass.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -428,7 +428,7 @@ def getuser():

try:
importpwd
returnpwd.getpwuid(os.getuid())[0]
returnpwd.getpwuid(os.getuid()).pw_name
except (ImportError, KeyError) ase:
raiseOSError('No username set in the environment') frome

Expand Down
2 changes: 1 addition & 1 deletion Lib/http/server.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand Down
2 changes: 1 addition & 1 deletion Lib/netrc.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -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}'

Expand Down
4 changes: 2 additions & 2 deletions Lib/shutil.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -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):
Expand All@@ -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,
Expand Down
8 changes: 4 additions & 4 deletions Lib/tarfile.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -2282,14 +2282,14 @@ def gettarinfo(self, name=None, arcname=None, fileobj=None):
ifpwd:
iftarinfo.uidnotinself._unames:
try:
self._unames[tarinfo.uid] =pwd.getpwuid(tarinfo.uid)[0]
self._unames[tarinfo.uid] =pwd.getpwuid(tarinfo.uid).pw_name
exceptKeyError:
self._unames[tarinfo.uid] =''
tarinfo.uname=self._unames[tarinfo.uid]
ifgrp:
iftarinfo.gidnotinself._gnames:
try:
self._gnames[tarinfo.gid] =grp.getgrgid(tarinfo.gid)[0]
self._gnames[tarinfo.gid] =grp.getgrgid(tarinfo.gid).gr_name
exceptKeyError:
self._gnames[tarinfo.gid] =''
tarinfo.gname=self._gnames[tarinfo.gid]
Expand DownExpand Up@@ -2837,12 +2837,12 @@ def chown(self, tarinfo, targetpath, numeric_owner):
ifnotnumeric_owner:
try:
ifgrpandtarinfo.gname:
g=grp.getgrnam(tarinfo.gname)[2]
g=grp.getgrnam(tarinfo.gname).gr_gid
exceptKeyError:
pass
try:
ifpwdandtarinfo.uname:
u=pwd.getpwnam(tarinfo.uname)[2]
u=pwd.getpwnam(tarinfo.uname).pw_uid
exceptKeyError:
pass
ifgisNone:
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/support/smtpd.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -862,7 +862,7 @@ def parseargs():
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:
Expand Down
5 changes: 4 additions & 1 deletion Lib/test/test_getpass.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -39,10 +39,13 @@ def test_username_falls_back_to_pwd(self, environ):
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)
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_os/test_posix.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -1315,8 +1315,8 @@ def _create_and_do_getcwd(dirname, current_path_length = 0):
@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))


Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_pwd.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -50,7 +50,7 @@ def test_values_extended(self):
# 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])
Expand Down
12 changes: 6 additions & 6 deletions Lib/test/test_shutil.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -1999,8 +1999,8 @@ def test_make_archive_owner_group(self):
# 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'

Expand All@@ -2027,8 +2027,8 @@ def test_make_archive_owner_group(self):
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)
Expand DownExpand Up@@ -2433,8 +2433,8 @@ def check_chown(path, uid=None, gid=None):
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
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_tarfile.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -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

Expand Down
2 changes: 1 addition & 1 deletion Tools/c-analyzer/c_common/fsutil.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -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:
Expand Down
Loading