]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-76912: Raise OSError from any failure in getpass.getuser() (#29739)
authorJacob Walls <jacobtylerwalls@gmail.com>
Mon, 27 Nov 2023 18:05:55 +0000 (13:05 -0500)
committerGitHub <noreply@github.com>
Mon, 27 Nov 2023 18:05:55 +0000 (10:05 -0800)
* bpo-32731: Raise OSError from any failure in getpass.getuser()
Previously, if the username was not set in certain environment variables, ImportError escaped on Windows systems, and it was possible for KeyError to escape on other systems if getpwuid() failed.

Doc/library/getpass.rst
Doc/whatsnew/3.13.rst
Lib/getpass.py
Lib/test/test_getpass.py
Misc/NEWS.d/next/Library/2021-11-23-22-22-49.bpo-32731.kNOASr.rst [new file with mode: 0644]

index 5c79daf0f47d8e6cec07161f575ae6e5a1d689b1..54c84d45a59856cfaaa9b07ce9900344e4057379 100644 (file)
@@ -46,7 +46,10 @@ The :mod:`getpass` module provides two functions:
    :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.
index 3fd0f5e165f018d1c21beda58b9d7f8c09877aed..ec09dfea4aad3c4fe49d7d2f55d608617d9cca10 100644 (file)
@@ -1032,6 +1032,10 @@ Changes in the Python API
   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
 =============
index 8b42c0a536b4c4bb458cc9b8c7a3316d081a7a2a..bd0097ced94c5e06f4ba3ba2aad063f512e5fca2 100644 (file)
@@ -156,7 +156,11 @@ def getuser():
 
     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'):
@@ -164,9 +168,12 @@ def getuser():
         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:
index 98ecec94336e326bca72c74ac6feca27b8b3a497..80dda2caaa33310f645cfe1e9099acf63bd1a251 100644 (file)
@@ -26,7 +26,7 @@ class GetpassGetuserTest(unittest.TestCase):
         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
@@ -47,7 +47,7 @@ class GetpassGetuserTest(unittest.TestCase):
                                  getpass.getuser())
                 getpw.assert_called_once_with(42)
         else:
-            self.assertRaises(ImportError, getpass.getuser)
+            self.assertRaises(OSError, getpass.getuser)
 
 
 class GetpassRawinputTest(unittest.TestCase):
diff --git a/Misc/NEWS.d/next/Library/2021-11-23-22-22-49.bpo-32731.kNOASr.rst b/Misc/NEWS.d/next/Library/2021-11-23-22-22-49.bpo-32731.kNOASr.rst
new file mode 100644 (file)
index 0000000..92f3b87
--- /dev/null
@@ -0,0 +1,3 @@
+: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.