changeset: 100607:a1d738158390 user: Berker Peksag date: Sat Mar 19 11:44:17 2016 +0200 files: Doc/library/spwd.rst Doc/whatsnew/3.6.rst Lib/test/test_spwd.py Misc/NEWS Modules/spwdmodule.c description: Issue #18787: spwd.getspnam() now raises a PermissionError if the user doesn't have privileges. diff -r 71c55c89d4f1 -r a1d738158390 Doc/library/spwd.rst --- a/Doc/library/spwd.rst Sat Mar 19 10:33:50 2016 +0100 +++ b/Doc/library/spwd.rst Sat Mar 19 11:44:17 2016 +0200 @@ -54,6 +54,9 @@ 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() diff -r 71c55c89d4f1 -r a1d738158390 Doc/whatsnew/3.6.rst --- a/Doc/whatsnew/3.6.rst Sat Mar 19 10:33:50 2016 +0100 +++ b/Doc/whatsnew/3.6.rst Sat Mar 19 11:44:17 2016 +0200 @@ -471,6 +471,8 @@ 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 -------------------- diff -r 71c55c89d4f1 -r a1d738158390 Lib/test/test_spwd.py --- a/Lib/test/test_spwd.py Sat Mar 19 10:33:50 2016 +0100 +++ b/Lib/test/test_spwd.py Sat Mar 19 11:44:17 2016 +0200 @@ -56,5 +56,15 @@ 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() diff -r 71c55c89d4f1 -r a1d738158390 Misc/NEWS --- a/Misc/NEWS Sat Mar 19 10:33:50 2016 +0100 +++ b/Misc/NEWS Sat Mar 19 11:44:17 2016 +0200 @@ -226,6 +226,9 @@ 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. diff -r 71c55c89d4f1 -r a1d738158390 Modules/spwdmodule.c --- a/Modules/spwdmodule.c Sat Mar 19 10:33:50 2016 +0100 +++ b/Modules/spwdmodule.c Sat Mar 19 11:44:17 2016 +0200 @@ -137,7 +137,10 @@ 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);