[Python-checkins] cpython (3.3): Raise KeyError instead of OverflowError when getpwuid's argument is out of

serhiy.storchaka python-checkins at python.org
Mon Feb 11 19:34:36 CET 2013


http://hg.python.org/cpython/rev/a0983e46feb1
changeset:   82166:a0983e46feb1
branch:      3.3
parent:      82163:cabcddbed377
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Mon Feb 11 20:32:47 2013 +0200
summary:
  Raise KeyError instead of OverflowError when getpwuid's argument is out of
uid_t range.

files:
  Lib/test/test_pwd.py |  9 +++++++++
  Modules/pwdmodule.c  |  6 +++++-
  2 files changed, 14 insertions(+), 1 deletions(-)


diff --git a/Lib/test/test_pwd.py b/Lib/test/test_pwd.py
--- a/Lib/test/test_pwd.py
+++ b/Lib/test/test_pwd.py
@@ -49,7 +49,9 @@
 
     def test_errors(self):
         self.assertRaises(TypeError, pwd.getpwuid)
+        self.assertRaises(TypeError, pwd.getpwuid, 3.14)
         self.assertRaises(TypeError, pwd.getpwnam)
+        self.assertRaises(TypeError, pwd.getpwnam, 42)
         self.assertRaises(TypeError, pwd.getpwall, 42)
 
         # try to get some errors
@@ -93,6 +95,13 @@
         self.assertNotIn(fakeuid, byuids)
         self.assertRaises(KeyError, pwd.getpwuid, fakeuid)
 
+        # -1 shouldn't be a valid uid because it has a special meaning in many
+        # uid-related functions
+        self.assertRaises(KeyError, pwd.getpwuid, -1)
+        # should be out of uid_t range
+        self.assertRaises(KeyError, pwd.getpwuid, 2**128)
+        self.assertRaises(KeyError, pwd.getpwuid, -2**128)
+
 def test_main():
     support.run_unittest(PwdTest)
 
diff --git a/Modules/pwdmodule.c b/Modules/pwdmodule.c
--- a/Modules/pwdmodule.c
+++ b/Modules/pwdmodule.c
@@ -106,8 +106,12 @@
 {
     uid_t uid;
     struct passwd *p;
-    if (!PyArg_ParseTuple(args, "O&:getpwuid", _Py_Uid_Converter, &uid))
+    if (!PyArg_ParseTuple(args, "O&:getpwuid", _Py_Uid_Converter, &uid)) {
+        if (PyErr_ExceptionMatches(PyExc_OverflowError))
+            PyErr_Format(PyExc_KeyError,
+                         "getpwuid(): uid not found");
         return NULL;
+    }
     if ((p = getpwuid(uid)) == NULL) {
         PyObject *uid_obj = _PyLong_FromUid(uid);
         if (uid_obj == NULL)

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list