:envvar:`USER`, :envvar:`!LNAME` and :envvar:`USERNAME`, in order, and
returns the value of the first one which is set to a non-empty string. If
none are set, the login name from the password database is returned on
- systems which support the :mod:`pwd` module, otherwise, an exception is
- raised.
+ systems which support the :mod:`pwd` module, otherwise, an :exc:`OSError`
+ is raised.
In general, this function should be preferred over :func:`os.getlogin()`.
+
+ .. versionchanged:: 3.13
+ Previously, various exceptions beyond just :exc:`OSError` were raised.
recomended in the documentation.
(Contributed by Serhiy Storchaka in :gh:`106672`.)
+* An :exc:`OSError` is now raised by :func:`getpass.getuser` for any failure to
+ retrieve a username, instead of :exc:`ImportError` on non-Unix platforms or
+ :exc:`KeyError` on Unix platforms where the password database is empty.
+
Build Changes
=============
First try various environment variables, then the password
database. This works on Windows as long as USERNAME is set.
+ Any failure to find a username raises OSError.
+ .. versionchanged:: 3.13
+ Previously, various exceptions beyond just :exc:`OSError`
+ were raised.
"""
for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
if user:
return user
- # If this fails, the exception will "explain" why
- import pwd
- return pwd.getpwuid(os.getuid())[0]
+ try:
+ import pwd
+ return pwd.getpwuid(os.getuid())[0]
+ except (ImportError, KeyError) as e:
+ raise OSError('No username set in the environment') from e
+
# Bind the name getpass to the appropriate function
try:
environ.get.return_value = None
try:
getpass.getuser()
- except ImportError: # in case there's no pwd module
+ except OSError: # in case there's no pwd module
pass
except KeyError:
# current user has no pwd entry
getpass.getuser())
getpw.assert_called_once_with(42)
else:
- self.assertRaises(ImportError, getpass.getuser)
+ self.assertRaises(OSError, getpass.getuser)
class GetpassRawinputTest(unittest.TestCase):
--- /dev/null
+:func:`getpass.getuser` now raises :exc:`OSError` for all failures rather
+than :exc:`ImportError` on systems lacking the :mod:`pwd` module or
+:exc:`KeyError` if the password database is empty.