Skip to content

Commit d499031

Browse files
authored
bpo-32651 Recommend getpass.getuser() (#5301)
* bpo-32651 - In the documentation for os.getlogin(), recommend getpass.getuser()
1 parent 22feeb8 commit d499031

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

Doc/library/getpass.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ The :mod:`getpass` module provides two functions:
4242
Return the "login name" of the user.
4343

4444
This function checks the environment variables :envvar:`LOGNAME`,
45-
:envvar:`USER`, :envvar:`LNAME` and :envvar:`USERNAME`, in order, and returns
46-
the value of the first one which is set to a non-empty string. If none are set,
47-
the login name from the password database is returned on systems which support
48-
the :mod:`pwd` module, otherwise, an exception is raised.
45+
:envvar:`USER`, :envvar:`LNAME` and :envvar:`USERNAME`, in order, and
46+
returns the value of the first one which is set to a non-empty string. If
47+
none are set, the login name from the password database is returned on
48+
systems which support the :mod:`pwd` module, otherwise, an exception is
49+
raised.
4950

51+
In general, this function should be preferred over :func:`os.getlogin()`.

Doc/library/os.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,11 @@ process and user.
325325
.. function:: getlogin()
326326

327327
Return the name of the user logged in on the controlling terminal of the
328-
process. For most purposes, it is more useful to use the environment
329-
variables :envvar:`LOGNAME` or :envvar:`USERNAME` to find out who the user
330-
is, or ``pwd.getpwuid(os.getuid())[0]`` to get the login name of the current
331-
real user id.
328+
process. For most purposes, it is more useful to use
329+
:func:`getpass.getuser` since the latter checks the environment variables
330+
:envvar:`LOGNAME` or :envvar:`USERNAME` to find out who the user is, and
331+
falls back to ``pwd.getpwuid(os.getuid())[0]`` to get the login name of the
332+
current real user id.
332333

333334
Availability: Unix, Windows.
334335

0 commit comments

Comments
 (0)