]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #18787: spwd.getspnam() now raises a PermissionError if the user
authorBerker Peksag <berker.peksag@gmail.com>
Sat, 19 Mar 2016 09:44:17 +0000 (11:44 +0200)
committerBerker Peksag <berker.peksag@gmail.com>
Sat, 19 Mar 2016 09:44:17 +0000 (11:44 +0200)
doesn't have privileges.

Doc/library/spwd.rst
Doc/whatsnew/3.6.rst
Lib/test/test_spwd.py
Misc/NEWS
Modules/spwdmodule.c

index 58be78f0175916c900fb153c79c6e41521ab0340..53f8c09e09d31df6bc41773442342c44de7b397b 100644 (file)
@@ -54,6 +54,9 @@ The following functions are defined:
 
    Return the shadow password database entry for the given user name.
 
+   .. versionchanged:: 3.6
+      Raises a :exc:`PermissionError` instead of :exc:`KeyError` if the user
+      doesn't have privileges.
 
 .. function:: getspall()
 
index 1129cb343491b181e148a78cb275191db00ca466..904605861b7d2e19885df1a70c9c2c6eabbda44a 100644 (file)
@@ -471,6 +471,8 @@ Changes in the Python API
   the exception will stop a single-threaded server. (Contributed by
   Martin Panter in :issue:`23430`.)
 
+* :func:`spwd.getspnam` now raises a :exc:`PermissionError` instead of
+  :exc:`KeyError` if the user doesn't have privileges.
 
 Changes in the C API
 --------------------
index bea7ab1ba5d28ba53e7ee9066fa7940047373d0f..fca809ef3761eadc503afd27cf9c36f94e93877d 100644 (file)
@@ -56,5 +56,15 @@ class TestSpwdRoot(unittest.TestCase):
             self.assertRaises(TypeError, spwd.getspnam, bytes_name)
 
 
+@unittest.skipUnless(hasattr(os, 'geteuid') and os.geteuid() != 0,
+                     'non-root user required')
+class TestSpwdNonRoot(unittest.TestCase):
+
+    def test_getspnam_exception(self):
+        with self.assertRaises(PermissionError) as cm:
+            spwd.getspnam('bin')
+        self.assertEqual(str(cm.exception), '[Errno 13] Permission denied')
+
+
 if __name__ == "__main__":
     unittest.main()
index fe3071ecb5400a630e7fc9bd800726fa6f040209..ae0a1001686fefac1adf510c31db2bc647806ceb 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -226,6 +226,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #18787: spwd.getspnam() now raises a PermissionError if the user
+  doesn't have privileges.
+
 - Issue #26560: Avoid potential ValueError in BaseHandler.start_response.
   Initial patch by Peter Inglesby.
 
index 49324d50f839e58a877ddcf43e5ce270ac01bc5c..e715d01b647718f5936b93aa603b0a1fe1b8e5ff 100644 (file)
@@ -137,7 +137,10 @@ spwd_getspnam_impl(PyModuleDef *module, PyObject *arg)
     if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
         goto out;
     if ((p = getspnam(name)) == NULL) {
-        PyErr_SetString(PyExc_KeyError, "getspnam(): name not found");
+        if (errno != 0)
+            PyErr_SetFromErrno(PyExc_OSError);
+        else
+            PyErr_SetString(PyExc_KeyError, "getspnam(): name not found");
         goto out;
     }
     retval = mkspent(p);