a96f753
diff -up Python-2.7.3/Include/modsupport.h.uid-gid-overflows Python-2.7.3/Include/modsupport.h
a96f753
--- Python-2.7.3/Include/modsupport.h.uid-gid-overflows	2012-04-09 19:07:29.000000000 -0400
7e98946
+++ Python-2.7.3/Include/modsupport.h	2012-06-26 14:52:03.739471150 -0400
7e98946
@@ -8,6 +8,7 @@ extern "C" {
7e98946
 /* Module support interface */
7e98946
 
7e98946
 #include <stdarg.h>
7e98946
+#include <sys/types.h>
7e98946
 
7e98946
 /* If PY_SSIZE_T_CLEAN is defined, each functions treats #-specifier
7e98946
    to mean Py_ssize_t */
7e98946
@@ -128,6 +129,17 @@ PyAPI_FUNC(PyObject *) Py_InitModule4(co
a96f753
 
a96f753
 PyAPI_DATA(char *) _Py_PackageContext;
a96f753
 
a96f753
+/*
a96f753
+  Non-standard extension: support for dealing with uid_t and gid_t without
a96f753
+  integer overflow
a96f753
+ */
a96f753
+
a96f753
+PyAPI_FUNC(PyObject *) _PyObject_FromUid(uid_t uid);
a96f753
+PyAPI_FUNC(PyObject *) _PyObject_FromGid(gid_t gid);
a96f753
+
a96f753
+PyAPI_FUNC(int) _PyArg_ParseUid(PyObject *in_obj, uid_t *out_uid);
a96f753
+PyAPI_FUNC(int) _PyArg_ParseGid(PyObject *in_obj, gid_t *out_gid);
a96f753
+
a96f753
 #ifdef __cplusplus
a96f753
 }
a96f753
 #endif
a96f753
diff -up Python-2.7.3/Lib/test/test_grp.py.uid-gid-overflows Python-2.7.3/Lib/test/test_grp.py
a96f753
--- Python-2.7.3/Lib/test/test_grp.py.uid-gid-overflows	2012-04-09 19:07:31.000000000 -0400
7e98946
+++ Python-2.7.3/Lib/test/test_grp.py	2012-06-26 14:51:36.000817929 -0400
a96f753
@@ -16,7 +16,7 @@ class GroupDatabaseTestCase(unittest.Tes
a96f753
         self.assertEqual(value[1], value.gr_passwd)
a96f753
         self.assertIsInstance(value.gr_passwd, basestring)
a96f753
         self.assertEqual(value[2], value.gr_gid)
a96f753
-        self.assertIsInstance(value.gr_gid, int)
a96f753
+        self.assertIsInstance(value.gr_gid, (int, long))
a96f753
         self.assertEqual(value[3], value.gr_mem)
a96f753
         self.assertIsInstance(value.gr_mem, list)
a96f753
 
a96f753
diff -up Python-2.7.3/Lib/test/test_os.py.uid-gid-overflows Python-2.7.3/Lib/test/test_os.py
a96f753
--- Python-2.7.3/Lib/test/test_os.py.uid-gid-overflows	2012-04-09 19:07:32.000000000 -0400
7e98946
+++ Python-2.7.3/Lib/test/test_os.py	2012-06-26 14:51:36.000817929 -0400
a96f753
@@ -677,30 +677,36 @@ if sys.platform != 'win32':
a96f753
             def test_setuid(self):
a96f753
                 if os.getuid() != 0:
a96f753
                     self.assertRaises(os.error, os.setuid, 0)
a96f753
+                self.assertRaises(TypeError, os.setuid, 'not an int')
a96f753
                 self.assertRaises(OverflowError, os.setuid, 1<<32)
a96f753
 
a96f753
         if hasattr(os, 'setgid'):
a96f753
             def test_setgid(self):
a96f753
                 if os.getuid() != 0:
a96f753
                     self.assertRaises(os.error, os.setgid, 0)
a96f753
+                self.assertRaises(TypeError, os.setgid, 'not an int')
a96f753
                 self.assertRaises(OverflowError, os.setgid, 1<<32)
a96f753
 
a96f753
         if hasattr(os, 'seteuid'):
a96f753
             def test_seteuid(self):
a96f753
                 if os.getuid() != 0:
a96f753
                     self.assertRaises(os.error, os.seteuid, 0)
a96f753
+                self.assertRaises(TypeError, os.seteuid, 'not an int')
a96f753
                 self.assertRaises(OverflowError, os.seteuid, 1<<32)
a96f753
 
a96f753
         if hasattr(os, 'setegid'):
a96f753
             def test_setegid(self):
a96f753
                 if os.getuid() != 0:
a96f753
                     self.assertRaises(os.error, os.setegid, 0)
a96f753
+                self.assertRaises(TypeError, os.setegid, 'not an int')
a96f753
                 self.assertRaises(OverflowError, os.setegid, 1<<32)
a96f753
 
a96f753
         if hasattr(os, 'setreuid'):
a96f753
             def test_setreuid(self):
a96f753
                 if os.getuid() != 0:
a96f753
                     self.assertRaises(os.error, os.setreuid, 0, 0)
a96f753
+                self.assertRaises(TypeError, os.setreuid, 'not an int', 0)
a96f753
+                self.assertRaises(TypeError, os.setreuid, 0, 'not an int')
a96f753
                 self.assertRaises(OverflowError, os.setreuid, 1<<32, 0)
a96f753
                 self.assertRaises(OverflowError, os.setreuid, 0, 1<<32)
a96f753
 
a96f753
@@ -715,6 +721,8 @@ if sys.platform != 'win32':
a96f753
             def test_setregid(self):
a96f753
                 if os.getuid() != 0:
a96f753
                     self.assertRaises(os.error, os.setregid, 0, 0)
a96f753
+                self.assertRaises(TypeError, os.setregid, 'not an int', 0)
a96f753
+                self.assertRaises(TypeError, os.setregid, 0, 'not an int')
a96f753
                 self.assertRaises(OverflowError, os.setregid, 1<<32, 0)
a96f753
                 self.assertRaises(OverflowError, os.setregid, 0, 1<<32)
a96f753
 
a96f753
diff -up Python-2.7.3/Lib/test/test_posix.py.uid-gid-overflows Python-2.7.3/Lib/test/test_posix.py
a96f753
--- Python-2.7.3/Lib/test/test_posix.py.uid-gid-overflows	2012-04-09 19:07:32.000000000 -0400
7e98946
+++ Python-2.7.3/Lib/test/test_posix.py	2012-06-26 14:51:36.001817916 -0400
a96f753
@@ -217,7 +217,7 @@ class PosixTester(unittest.TestCase):
a96f753
         if hasattr(posix, 'stat'):
a96f753
             self.assertTrue(posix.stat(test_support.TESTFN))
a96f753
 
a96f753
-    def _test_all_chown_common(self, chown_func, first_param):
a96f753
+    def _test_all_chown_common(self, chown_func, stat_func, first_param):
a96f753
         """Common code for chown, fchown and lchown tests."""
a96f753
         if os.getuid() == 0:
a96f753
             try:
a96f753
@@ -237,6 +237,13 @@ class PosixTester(unittest.TestCase):
a96f753
 
a96f753
         # test a successful chown call
a96f753
         chown_func(first_param, os.getuid(), os.getgid())
a96f753
+        self.assertEqual(stat_func(first_param).st_uid, os.getuid())
a96f753
+        self.assertEqual(stat_func(first_param).st_gid, os.getgid())
a96f753
+
a96f753
+        # verify that -1 works as a "do-nothing" option:
a96f753
+        chown_func(first_param, -1, -1)
a96f753
+        self.assertEqual(stat_func(first_param).st_uid, os.getuid())
a96f753
+        self.assertEqual(stat_func(first_param).st_gid, os.getgid())
a96f753
 
a96f753
     @unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()")
a96f753
     def test_chown(self):
a96f753
@@ -246,7 +253,7 @@ class PosixTester(unittest.TestCase):
a96f753
 
a96f753
         # re-create the file
a96f753
         open(test_support.TESTFN, 'w').close()
a96f753
-        self._test_all_chown_common(posix.chown, test_support.TESTFN)
a96f753
+        self._test_all_chown_common(posix.chown, posix.stat, test_support.TESTFN)
a96f753
 
a96f753
     @unittest.skipUnless(hasattr(posix, 'fchown'), "test needs os.fchown()")
a96f753
     def test_fchown(self):
a96f753
@@ -256,7 +263,7 @@ class PosixTester(unittest.TestCase):
a96f753
         test_file = open(test_support.TESTFN, 'w')
a96f753
         try:
a96f753
             fd = test_file.fileno()
a96f753
-            self._test_all_chown_common(posix.fchown, fd)
a96f753
+            self._test_all_chown_common(posix.fchown, posix.fstat, fd)
a96f753
         finally:
a96f753
             test_file.close()
a96f753
 
a96f753
@@ -265,7 +272,7 @@ class PosixTester(unittest.TestCase):
a96f753
         os.unlink(test_support.TESTFN)
a96f753
         # create a symlink
a96f753
         os.symlink(_DUMMY_SYMLINK, test_support.TESTFN)
a96f753
-        self._test_all_chown_common(posix.lchown, test_support.TESTFN)
a96f753
+        self._test_all_chown_common(posix.lchown, posix.lstat, test_support.TESTFN)
a96f753
 
a96f753
     def test_chdir(self):
a96f753
         if hasattr(posix, 'chdir'):
a96f753
diff -up Python-2.7.3/Lib/test/test_pwd.py.uid-gid-overflows Python-2.7.3/Lib/test/test_pwd.py
a96f753
--- Python-2.7.3/Lib/test/test_pwd.py.uid-gid-overflows	2012-04-09 19:07:32.000000000 -0400
7e98946
+++ Python-2.7.3/Lib/test/test_pwd.py	2012-06-26 14:51:36.001817916 -0400
a96f753
@@ -18,9 +18,9 @@ class PwdTest(unittest.TestCase):
a96f753
             self.assertEqual(e[1], e.pw_passwd)
a96f753
             self.assertIsInstance(e.pw_passwd, basestring)
a96f753
             self.assertEqual(e[2], e.pw_uid)
a96f753
-            self.assertIsInstance(e.pw_uid, int)
a96f753
+            self.assertIsInstance(e.pw_uid, (int, long))
a96f753
             self.assertEqual(e[3], e.pw_gid)
a96f753
-            self.assertIsInstance(e.pw_gid, int)
a96f753
+            self.assertIsInstance(e.pw_gid, (int, long))
a96f753
             self.assertEqual(e[4], e.pw_gecos)
a96f753
             self.assertIsInstance(e.pw_gecos, basestring)
a96f753
             self.assertEqual(e[5], e.pw_dir)
a96f753
@@ -87,9 +87,9 @@ class PwdTest(unittest.TestCase):
a96f753
         # In some cases, byuids isn't a complete list of all users in the
a96f753
         # system, so if we try to pick a value not in byuids (via a perturbing
a96f753
         # loop, say), pwd.getpwuid() might still be able to find data for that
a96f753
-        # uid. Using sys.maxint may provoke the same problems, but hopefully
a96f753
+        # uid. Using 2**32 - 2 may provoke the same problems, but hopefully
a96f753
         # it will be a more repeatable failure.
a96f753
-        fakeuid = sys.maxint
a96f753
+        fakeuid = 2**32 - 2
a96f753
         self.assertNotIn(fakeuid, byuids)
a96f753
         self.assertRaises(KeyError, pwd.getpwuid, fakeuid)
a96f753
 
a96f753
diff -up Python-2.7.3/Modules/grpmodule.c.uid-gid-overflows Python-2.7.3/Modules/grpmodule.c
a96f753
--- Python-2.7.3/Modules/grpmodule.c.uid-gid-overflows	2012-04-09 19:07:34.000000000 -0400
7e98946
+++ Python-2.7.3/Modules/grpmodule.c	2012-06-26 14:51:36.002817904 -0400
a96f753
@@ -70,7 +70,7 @@ mkgrent(struct group *p)
a96f753
 	    Py_INCREF(Py_None);
a96f753
     }
a96f753
 #endif
a96f753
-    SET(setIndex++, PyInt_FromLong((long) p->gr_gid));
a96f753
+    SET(setIndex++, _PyObject_FromGid(p->gr_gid));
a96f753
     SET(setIndex++, w);
a96f753
 #undef SET
a96f753
 
a96f753
@@ -85,18 +85,15 @@ mkgrent(struct group *p)
a96f753
 static PyObject *
a96f753
 grp_getgrgid(PyObject *self, PyObject *pyo_id)
a96f753
 {
a96f753
-    PyObject *py_int_id;
a96f753
-    unsigned int gid;
a96f753
+    gid_t gid;
a96f753
     struct group *p;
a96f753
 
a96f753
-    py_int_id = PyNumber_Int(pyo_id);
a96f753
-    if (!py_int_id)
a96f753
-	    return NULL;
a96f753
-    gid = PyInt_AS_LONG(py_int_id);
a96f753
-    Py_DECREF(py_int_id);
a96f753
+    if (!_PyArg_ParseGid(pyo_id, &gid)) {
a96f753
+        return NULL;
a96f753
+    }
a96f753
 
a96f753
     if ((p = getgrgid(gid)) == NULL) {
a96f753
-	PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %d", gid);
a96f753
+        PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %lu", (unsigned long)gid);
a96f753
         return NULL;
a96f753
     }
a96f753
     return mkgrent(p);
a96f753
diff -up Python-2.7.3/Modules/posixmodule.c.uid-gid-overflows Python-2.7.3/Modules/posixmodule.c
7e98946
--- Python-2.7.3/Modules/posixmodule.c.uid-gid-overflows	2012-06-26 14:51:35.864819629 -0400
7e98946
+++ Python-2.7.3/Modules/posixmodule.c	2012-06-26 14:51:36.005817868 -0400
a96f753
@@ -1305,8 +1305,8 @@ _pystat_fromstructstat(STRUCT_STAT *st)
a96f753
     PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
a96f753
 #endif
a96f753
     PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
a96f753
-    PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
a96f753
-    PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
a96f753
+    PyStructSequence_SET_ITEM(v, 4, _PyObject_FromUid(st->st_uid));
a96f753
+    PyStructSequence_SET_ITEM(v, 5, _PyObject_FromGid(st->st_gid));
a96f753
 #ifdef HAVE_LARGEFILE_SUPPORT
a96f753
     PyStructSequence_SET_ITEM(v, 6,
a96f753
                               PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
a96f753
@@ -1883,14 +1883,16 @@ static PyObject *
a96f753
 posix_chown(PyObject *self, PyObject *args)
a96f753
 {
a96f753
     char *path = NULL;
a96f753
-    long uid, gid;
a96f753
+    uid_t uid;
a96f753
+    gid_t gid;
a96f753
     int res;
a96f753
-    if (!PyArg_ParseTuple(args, "etll:chown",
a96f753
+    if (!PyArg_ParseTuple(args, "etO&O&:chown",
a96f753
                           Py_FileSystemDefaultEncoding, &path,
a96f753
-                          &uid, &gid))
a96f753
+			  _PyArg_ParseUid, &uid,
a96f753
+			  _PyArg_ParseGid, &gid))
a96f753
         return NULL;
a96f753
     Py_BEGIN_ALLOW_THREADS
a96f753
-    res = chown(path, (uid_t) uid, (gid_t) gid);
a96f753
+    res = chown(path, uid, gid);
a96f753
     Py_END_ALLOW_THREADS
a96f753
     if (res < 0)
a96f753
         return posix_error_with_allocated_filename(path);
a96f753
@@ -1910,12 +1912,15 @@ static PyObject *
a96f753
 posix_fchown(PyObject *self, PyObject *args)
a96f753
 {
a96f753
     int fd;
a96f753
-    long uid, gid;
a96f753
+    uid_t uid;
a96f753
+    gid_t gid;
a96f753
     int res;
a96f753
-    if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
a96f753
+    if (!PyArg_ParseTuple(args, "iO&O&:chown", &fd,
a96f753
+			  _PyArg_ParseUid, &uid,
a96f753
+			  _PyArg_ParseGid, &gid))
a96f753
         return NULL;
a96f753
     Py_BEGIN_ALLOW_THREADS
a96f753
-    res = fchown(fd, (uid_t) uid, (gid_t) gid);
a96f753
+    res = fchown(fd, uid, gid);
a96f753
     Py_END_ALLOW_THREADS
a96f753
     if (res < 0)
a96f753
         return posix_error();
a96f753
@@ -1933,14 +1938,16 @@ static PyObject *
a96f753
 posix_lchown(PyObject *self, PyObject *args)
a96f753
 {
a96f753
     char *path = NULL;
a96f753
-    long uid, gid;
a96f753
+    uid_t uid;
a96f753
+    gid_t gid;
a96f753
     int res;
a96f753
-    if (!PyArg_ParseTuple(args, "etll:lchown",
a96f753
+    if (!PyArg_ParseTuple(args, "etO&O&:lchown",
a96f753
                           Py_FileSystemDefaultEncoding, &path,
a96f753
-                          &uid, &gid))
a96f753
+			  _PyArg_ParseUid, &uid,
a96f753
+			  _PyArg_ParseGid, &gid))
a96f753
         return NULL;
a96f753
     Py_BEGIN_ALLOW_THREADS
a96f753
-    res = lchown(path, (uid_t) uid, (gid_t) gid);
a96f753
+    res = lchown(path, uid, gid);
a96f753
     Py_END_ALLOW_THREADS
a96f753
     if (res < 0)
a96f753
         return posix_error_with_allocated_filename(path);
a96f753
@@ -3841,7 +3848,7 @@ Return the current process's effective g
a96f753
 static PyObject *
a96f753
 posix_getegid(PyObject *self, PyObject *noargs)
a96f753
 {
a96f753
-    return PyInt_FromLong((long)getegid());
a96f753
+    return _PyObject_FromGid(getegid());
a96f753
 }
a96f753
 #endif
a96f753
 
a96f753
@@ -3854,7 +3861,7 @@ Return the current process's effective u
a96f753
 static PyObject *
a96f753
 posix_geteuid(PyObject *self, PyObject *noargs)
a96f753
 {
a96f753
-    return PyInt_FromLong((long)geteuid());
a96f753
+    return _PyObject_FromUid(geteuid());
a96f753
 }
a96f753
 #endif
a96f753
 
a96f753
@@ -3867,7 +3874,7 @@ Return the current process's group id.")
a96f753
 static PyObject *
a96f753
 posix_getgid(PyObject *self, PyObject *noargs)
a96f753
 {
a96f753
-    return PyInt_FromLong((long)getgid());
a96f753
+    return _PyObject_FromGid(getgid());
a96f753
 }
a96f753
 #endif
a96f753
 
a96f753
@@ -3942,7 +3949,7 @@ posix_getgroups(PyObject *self, PyObject
a96f753
     if (result != NULL) {
a96f753
         int i;
a96f753
         for (i = 0; i < n; ++i) {
a96f753
-            PyObject *o = PyInt_FromLong((long)alt_grouplist[i]);
a96f753
+            PyObject *o = _PyObject_FromGid(alt_grouplist[i]);
a96f753
             if (o == NULL) {
a96f753
                 Py_DECREF(result);
a96f753
                 result = NULL;
a96f753
@@ -3971,12 +3978,13 @@ static PyObject *
a96f753
 posix_initgroups(PyObject *self, PyObject *args)
a96f753
 {
a96f753
     char *username;
a96f753
-    long gid;
a96f753
+    gid_t gid;
a96f753
 
a96f753
-    if (!PyArg_ParseTuple(args, "sl:initgroups", &username, &gid))
a96f753
+    if (!PyArg_ParseTuple(args, "sO&:initgroups", &username,
a96f753
+                          _PyArg_ParseGid, &gid))
a96f753
         return NULL;
a96f753
 
a96f753
-    if (initgroups(username, (gid_t) gid) == -1)
a96f753
+    if (initgroups(username, gid) == -1)
a96f753
         return PyErr_SetFromErrno(PyExc_OSError);
a96f753
 
a96f753
     Py_INCREF(Py_None);
a96f753
@@ -4090,7 +4098,7 @@ Return the current process's user id.");
a96f753
 static PyObject *
a96f753
 posix_getuid(PyObject *self, PyObject *noargs)
a96f753
 {
a96f753
-    return PyInt_FromLong((long)getuid());
a96f753
+    return _PyObject_FromUid(getuid());
a96f753
 }
a96f753
 #endif
a96f753
 
a96f753
@@ -5736,15 +5744,9 @@ Set the current process's user id.");
a96f753
 static PyObject *
a96f753
 posix_setuid(PyObject *self, PyObject *args)
a96f753
 {
a96f753
-    long uid_arg;
a96f753
     uid_t uid;
a96f753
-    if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
a96f753
+    if (!PyArg_ParseTuple(args, "O&:setuid", _PyArg_ParseUid, &uid))
a96f753
         return NULL;
a96f753
-    uid = uid_arg;
a96f753
-    if (uid != uid_arg) {
a96f753
-        PyErr_SetString(PyExc_OverflowError, "user id too big");
a96f753
-        return NULL;
a96f753
-    }
a96f753
     if (setuid(uid) < 0)
a96f753
         return posix_error();
a96f753
     Py_INCREF(Py_None);
a96f753
@@ -5761,15 +5763,9 @@ Set the current process's effective user
a96f753
 static PyObject *
a96f753
 posix_seteuid (PyObject *self, PyObject *args)
a96f753
 {
a96f753
-    long euid_arg;
a96f753
     uid_t euid;
a96f753
-    if (!PyArg_ParseTuple(args, "l", &euid_arg))
a96f753
+    if (!PyArg_ParseTuple(args, "O&:seteuid", _PyArg_ParseUid, &euid))
a96f753
         return NULL;
a96f753
-    euid = euid_arg;
a96f753
-    if (euid != euid_arg) {
a96f753
-        PyErr_SetString(PyExc_OverflowError, "user id too big");
a96f753
-        return NULL;
a96f753
-    }
a96f753
     if (seteuid(euid) < 0) {
a96f753
         return posix_error();
a96f753
     } else {
a96f753
@@ -5787,15 +5783,9 @@ Set the current process's effective grou
a96f753
 static PyObject *
a96f753
 posix_setegid (PyObject *self, PyObject *args)
a96f753
 {
a96f753
-    long egid_arg;
a96f753
     gid_t egid;
a96f753
-    if (!PyArg_ParseTuple(args, "l", &egid_arg))
a96f753
+    if (!PyArg_ParseTuple(args, "O&:setegid", _PyArg_ParseGid, &egid))
a96f753
         return NULL;
a96f753
-    egid = egid_arg;
a96f753
-    if (egid != egid_arg) {
a96f753
-        PyErr_SetString(PyExc_OverflowError, "group id too big");
a96f753
-        return NULL;
a96f753
-    }
a96f753
     if (setegid(egid) < 0) {
a96f753
         return posix_error();
a96f753
     } else {
a96f753
@@ -5813,23 +5803,11 @@ Set the current process's real and effec
a96f753
 static PyObject *
a96f753
 posix_setreuid (PyObject *self, PyObject *args)
a96f753
 {
a96f753
-    long ruid_arg, euid_arg;
a96f753
     uid_t ruid, euid;
a96f753
-    if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
a96f753
+    if (!PyArg_ParseTuple(args, "O&O&",
a96f753
+			  _PyArg_ParseUid, &ruid,
a96f753
+			  _PyArg_ParseUid, &euid))
a96f753
         return NULL;
a96f753
-    if (ruid_arg == -1)
a96f753
-        ruid = (uid_t)-1;  /* let the compiler choose how -1 fits */
a96f753
-    else
a96f753
-        ruid = ruid_arg;  /* otherwise, assign from our long */
a96f753
-    if (euid_arg == -1)
a96f753
-        euid = (uid_t)-1;
a96f753
-    else
a96f753
-        euid = euid_arg;
a96f753
-    if ((euid_arg != -1 && euid != euid_arg) ||
a96f753
-        (ruid_arg != -1 && ruid != ruid_arg)) {
a96f753
-        PyErr_SetString(PyExc_OverflowError, "user id too big");
a96f753
-        return NULL;
a96f753
-    }
a96f753
     if (setreuid(ruid, euid) < 0) {
a96f753
         return posix_error();
a96f753
     } else {
a96f753
@@ -5847,23 +5825,11 @@ Set the current process's real and effec
a96f753
 static PyObject *
a96f753
 posix_setregid (PyObject *self, PyObject *args)
a96f753
 {
a96f753
-    long rgid_arg, egid_arg;
a96f753
     gid_t rgid, egid;
a96f753
-    if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
a96f753
+    if (!PyArg_ParseTuple(args, "O&O&",
a96f753
+			  _PyArg_ParseGid, &rgid,
a96f753
+			  _PyArg_ParseGid, &egid))
a96f753
         return NULL;
a96f753
-    if (rgid_arg == -1)
a96f753
-        rgid = (gid_t)-1;  /* let the compiler choose how -1 fits */
a96f753
-    else
a96f753
-        rgid = rgid_arg;  /* otherwise, assign from our long */
a96f753
-    if (egid_arg == -1)
a96f753
-        egid = (gid_t)-1;
a96f753
-    else
a96f753
-        egid = egid_arg;
a96f753
-    if ((egid_arg != -1 && egid != egid_arg) ||
a96f753
-        (rgid_arg != -1 && rgid != rgid_arg)) {
a96f753
-        PyErr_SetString(PyExc_OverflowError, "group id too big");
a96f753
-        return NULL;
a96f753
-    }
a96f753
     if (setregid(rgid, egid) < 0) {
a96f753
         return posix_error();
a96f753
     } else {
a96f753
@@ -5881,15 +5847,9 @@ Set the current process's group id.");
a96f753
 static PyObject *
a96f753
 posix_setgid(PyObject *self, PyObject *args)
a96f753
 {
a96f753
-    long gid_arg;
a96f753
     gid_t gid;
a96f753
-    if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
a96f753
-        return NULL;
a96f753
-    gid = gid_arg;
a96f753
-    if (gid != gid_arg) {
a96f753
-        PyErr_SetString(PyExc_OverflowError, "group id too big");
a96f753
+    if (!PyArg_ParseTuple(args, "O&:setgid", _PyArg_ParseGid, &gid))
a96f753
         return NULL;
a96f753
-    }
a96f753
     if (setgid(gid) < 0)
a96f753
         return posix_error();
a96f753
     Py_INCREF(Py_None);
a96f753
@@ -5922,39 +5882,10 @@ posix_setgroups(PyObject *self, PyObject
a96f753
         elem = PySequence_GetItem(groups, i);
a96f753
         if (!elem)
a96f753
             return NULL;
a96f753
-        if (!PyInt_Check(elem)) {
a96f753
-            if (!PyLong_Check(elem)) {
a96f753
-                PyErr_SetString(PyExc_TypeError,
a96f753
-                                "groups must be integers");
a96f753
-                Py_DECREF(elem);
a96f753
-                return NULL;
a96f753
-            } else {
a96f753
-                unsigned long x = PyLong_AsUnsignedLong(elem);
a96f753
-                if (PyErr_Occurred()) {
a96f753
-                    PyErr_SetString(PyExc_TypeError,
a96f753
-                                    "group id too big");
a96f753
-                    Py_DECREF(elem);
a96f753
-                    return NULL;
a96f753
-                }
a96f753
-                grouplist[i] = x;
a96f753
-                /* read back to see if it fits in gid_t */
a96f753
-                if (grouplist[i] != x) {
a96f753
-                    PyErr_SetString(PyExc_TypeError,
a96f753
-                                    "group id too big");
a96f753
-                    Py_DECREF(elem);
a96f753
-                    return NULL;
a96f753
-                }
a96f753
-            }
a96f753
-        } else {
a96f753
-            long x  = PyInt_AsLong(elem);
a96f753
-            grouplist[i] = x;
a96f753
-            if (grouplist[i] != x) {
a96f753
-                PyErr_SetString(PyExc_TypeError,
a96f753
-                                "group id too big");
a96f753
-                Py_DECREF(elem);
a96f753
-                return NULL;
a96f753
-            }
a96f753
-        }
a96f753
+	if (!_PyArg_ParseGid(elem, &grouplist[i])) {
a96f753
+	    Py_DECREF(elem);
a96f753
+	    return NULL;
a96f753
+	}
a96f753
         Py_DECREF(elem);
a96f753
     }
a96f753
 
a96f753
@@ -8576,9 +8507,11 @@ Set the current process's real, effectiv
a96f753
 static PyObject*
a96f753
 posix_setresuid (PyObject *self, PyObject *args)
a96f753
 {
a96f753
-    /* We assume uid_t is no larger than a long. */
a96f753
-    long ruid, euid, suid;
a96f753
-    if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
a96f753
+    uid_t ruid, euid, suid;
a96f753
+    if (!PyArg_ParseTuple(args, "O&O&O&",
a96f753
+                          _PyArg_ParseUid, &ruid,
a96f753
+                          _PyArg_ParseUid, &euid,
a96f753
+                          _PyArg_ParseUid, &suid))
a96f753
         return NULL;
a96f753
     if (setresuid(ruid, euid, suid) < 0)
a96f753
         return posix_error();
a96f753
@@ -8594,9 +8527,12 @@ Set the current process's real, effectiv
a96f753
 static PyObject*
a96f753
 posix_setresgid (PyObject *self, PyObject *args)
a96f753
 {
a96f753
-    /* We assume uid_t is no larger than a long. */
a96f753
-    long rgid, egid, sgid;
a96f753
-    if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
a96f753
+    gid_t rgid, egid, sgid;
a96f753
+    if (!PyArg_ParseTuple(args, "O&O&O&",
a96f753
+                          _PyArg_ParseGid, &rgid,
a96f753
+                          _PyArg_ParseGid, &egid,
a96f753
+                          _PyArg_ParseGid, &sgid))
a96f753
+
a96f753
         return NULL;
a96f753
     if (setresgid(rgid, egid, sgid) < 0)
a96f753
         return posix_error();
a96f753
@@ -8613,14 +8549,13 @@ static PyObject*
a96f753
 posix_getresuid (PyObject *self, PyObject *noargs)
a96f753
 {
a96f753
     uid_t ruid, euid, suid;
a96f753
-    long l_ruid, l_euid, l_suid;
a96f753
+    PyObject *obj_ruid, *obj_euid, *obj_suid;
a96f753
     if (getresuid(&ruid, &euid, &suid) < 0)
a96f753
         return posix_error();
a96f753
-    /* Force the values into long's as we don't know the size of uid_t. */
a96f753
-    l_ruid = ruid;
a96f753
-    l_euid = euid;
a96f753
-    l_suid = suid;
a96f753
-    return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
a96f753
+    obj_ruid = _PyObject_FromUid(ruid);
a96f753
+    obj_euid = _PyObject_FromUid(euid);
a96f753
+    obj_suid = _PyObject_FromUid(suid);
a96f753
+    return Py_BuildValue("(NNN)", obj_ruid, obj_euid, obj_suid);
a96f753
 }
a96f753
 #endif
a96f753
 
a96f753
@@ -8632,15 +8567,14 @@ Get tuple of the current process's real,
a96f753
 static PyObject*
a96f753
 posix_getresgid (PyObject *self, PyObject *noargs)
a96f753
 {
a96f753
-    uid_t rgid, egid, sgid;
a96f753
-    long l_rgid, l_egid, l_sgid;
a96f753
+    gid_t rgid, egid, sgid;
a96f753
+    PyObject *obj_rgid, *obj_egid, *obj_sgid;
a96f753
     if (getresgid(&rgid, &egid, &sgid) < 0)
a96f753
         return posix_error();
a96f753
-    /* Force the values into long's as we don't know the size of uid_t. */
a96f753
-    l_rgid = rgid;
a96f753
-    l_egid = egid;
a96f753
-    l_sgid = sgid;
a96f753
-    return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
a96f753
+    obj_rgid = _PyObject_FromGid(rgid);
a96f753
+    obj_egid = _PyObject_FromGid(egid);
a96f753
+    obj_sgid = _PyObject_FromGid(sgid);
a96f753
+    return Py_BuildValue("(NNN)", obj_rgid, obj_egid, obj_sgid);
a96f753
 }
a96f753
 #endif
a96f753
 
a96f753
diff -up Python-2.7.3/Modules/pwdmodule.c.uid-gid-overflows Python-2.7.3/Modules/pwdmodule.c
a96f753
--- Python-2.7.3/Modules/pwdmodule.c.uid-gid-overflows	2012-04-09 19:07:34.000000000 -0400
7e98946
+++ Python-2.7.3/Modules/pwdmodule.c	2012-06-26 14:51:36.006817855 -0400
a96f753
@@ -73,8 +73,8 @@ mkpwent(struct passwd *p)
a96f753
 #else
a96f753
     SETS(setIndex++, p->pw_passwd);
a96f753
 #endif
a96f753
-    SETI(setIndex++, p->pw_uid);
a96f753
-    SETI(setIndex++, p->pw_gid);
a96f753
+    PyStructSequence_SET_ITEM(v, setIndex++, _PyObject_FromUid(p->pw_uid));
a96f753
+    PyStructSequence_SET_ITEM(v, setIndex++, _PyObject_FromGid(p->pw_gid));
a96f753
 #ifdef __VMS
a96f753
     SETS(setIndex++, "");
a96f753
 #else
a96f753
@@ -103,13 +103,14 @@ See help(pwd) for more on password datab
a96f753
 static PyObject *
a96f753
 pwd_getpwuid(PyObject *self, PyObject *args)
a96f753
 {
a96f753
-    unsigned int uid;
a96f753
+    uid_t uid;
a96f753
     struct passwd *p;
a96f753
-    if (!PyArg_ParseTuple(args, "I:getpwuid", &uid))
a96f753
+    if (!PyArg_ParseTuple(args, "O&:getpwuid",
a96f753
+                          _PyArg_ParseUid, &uid))
a96f753
         return NULL;
a96f753
     if ((p = getpwuid(uid)) == NULL) {
a96f753
         PyErr_Format(PyExc_KeyError,
a96f753
-                     "getpwuid(): uid not found: %d", uid);
a96f753
+                     "getpwuid(): uid not found: %lu", (unsigned long)uid);
a96f753
         return NULL;
a96f753
     }
a96f753
     return mkpwent(p);
a96f753
diff -up Python-2.7.3/Python/getargs.c.uid-gid-overflows Python-2.7.3/Python/getargs.c
a96f753
--- Python-2.7.3/Python/getargs.c.uid-gid-overflows	2012-04-09 19:07:35.000000000 -0400
7e98946
+++ Python-2.7.3/Python/getargs.c	2012-06-26 14:51:36.007817842 -0400
a96f753
@@ -4,6 +4,7 @@
a96f753
 #include "Python.h"
a96f753
 
a96f753
 #include <ctype.h>
a96f753
+#include <limits.h>
a96f753
 
a96f753
 
a96f753
 #ifdef __cplusplus
7e98946
@@ -1902,6 +1903,110 @@ _PyArg_NoKeywords(const char *funcname, 
a96f753
                     funcname);
a96f753
     return 0;
a96f753
 }
a96f753
+
a96f753
+PyObject *
a96f753
+_PyObject_FromUid(uid_t uid)
a96f753
+{
a96f753
+    if (uid <= (uid_t)LONG_MAX) {
a96f753
+        return PyInt_FromLong((uid_t)uid);
a96f753
+    } else {
a96f753
+        return PyLong_FromUnsignedLong((uid_t)uid);
a96f753
+    }
a96f753
+}
a96f753
+
a96f753
+PyObject *
a96f753
+_PyObject_FromGid(gid_t gid)
a96f753
+{
a96f753
+    if (gid <= (gid_t)LONG_MAX) {
a96f753
+        return PyInt_FromLong((gid_t)gid);
a96f753
+    } else {
a96f753
+        return PyLong_FromUnsignedLong((gid_t)gid);
a96f753
+    }
a96f753
+}
a96f753
+
a96f753
+int
a96f753
+_PyArg_ParseUid(PyObject *in_obj, uid_t *out_uid)
a96f753
+{
a96f753
+    PyObject *index, *number = NULL;
a96f753
+    long sl;
a96f753
+    unsigned long ul;
a96f753
+
a96f753
+    assert(out_uid);
a96f753
+
a96f753
+    index = PyNumber_Index(in_obj);
a96f753
+    if (index != NULL) {
a96f753
+        number = PyNumber_Long(index);
a96f753
+        Py_DECREF(index);
a96f753
+    }
a96f753
+    if (number == NULL) {
a96f753
+        PyErr_SetString(PyExc_TypeError, "user id must be integer");
a96f753
+        return 0;
a96f753
+    }
a96f753
+
a96f753
+    /* Special case: support -1 (e.g. for use by chown) */
a96f753
+    sl = PyLong_AsLong(number);
a96f753
+    if (PyErr_Occurred()) {
a96f753
+        PyErr_Clear();
a96f753
+    } else if (sl == -1) {
a96f753
+        Py_DECREF(number);
a96f753
+        *out_uid = (uid_t)-1;
a96f753
+        return 1;
a96f753
+    }
a96f753
+
a96f753
+    /* Otherwise, it must be >= 0 */
a96f753
+    ul = PyLong_AsUnsignedLong(number);
a96f753
+    Py_DECREF(number);
a96f753
+    *out_uid = ul;
a96f753
+    /* read back the value to see if it fitted in uid_t */
a96f753
+    if (PyErr_Occurred() || *out_uid != ul) {
a96f753
+        PyErr_SetString(PyExc_OverflowError,
a96f753
+			"user id is not in range(-1, 2^32-1)");
a96f753
+	return 0;
a96f753
+    }
a96f753
+    return 1;
a96f753
+}
a96f753
+
a96f753
+int
a96f753
+_PyArg_ParseGid(PyObject *in_obj, gid_t *out_gid)
a96f753
+{
a96f753
+    PyObject *index, *number = NULL;
a96f753
+    long sl;
a96f753
+    unsigned long ul;
a96f753
+
a96f753
+    assert(out_gid);
a96f753
+
a96f753
+    index = PyNumber_Index(in_obj);
a96f753
+    if (index != NULL) {
a96f753
+        number = PyNumber_Long(index);
a96f753
+	Py_DECREF(index);
a96f753
+    }
a96f753
+    if (number == NULL) {
a96f753
+        PyErr_SetString(PyExc_TypeError, "group id must be integer");
a96f753
+	return 0;
a96f753
+    }
a96f753
+
a96f753
+    /* Special case: support -1 (e.g. for use by chown) */
a96f753
+    sl = PyLong_AsLong(number);
a96f753
+    if (PyErr_Occurred()) {
a96f753
+        PyErr_Clear();
a96f753
+    } else if (sl == -1) {
a96f753
+        Py_DECREF(number);
a96f753
+	*out_gid = (gid_t)-1;
a96f753
+	return 1;
a96f753
+    }
a96f753
+
a96f753
+    ul = PyLong_AsUnsignedLong(number);
a96f753
+    Py_DECREF(number);
a96f753
+    *out_gid = ul;
a96f753
+    /* read back the value to see if it fitted in gid_t */
a96f753
+    if (PyErr_Occurred() || *out_gid != ul) {
a96f753
+        PyErr_SetString(PyExc_OverflowError,
a96f753
+			"group id is not in range(-1, 2^32-1)");
a96f753
+	return 0;
a96f753
+    }
a96f753
+    return 1;
a96f753
+}
a96f753
+
a96f753
 #ifdef __cplusplus
a96f753
 };
a96f753
 #endif