Issue #4591: Uid and gid values larger than 2**31 are supported now.
diff --git a/Modules/grpmodule.c b/Modules/grpmodule.c
index ebc38cc..55da9b8 100644
--- a/Modules/grpmodule.c
+++ b/Modules/grpmodule.c
@@ -3,8 +3,8 @@
 
 #include "Python.h"
 #include "structseq.h"
+#include "posixmodule.h"
 
-#include <sys/types.h>
 #include <grp.h>
 
 static PyStructSequence_Field struct_group_type_fields[] = {
@@ -70,7 +70,7 @@
 	    Py_INCREF(Py_None);
     }
 #endif
-    SET(setIndex++, PyInt_FromLong((long) p->gr_gid));
+    SET(setIndex++, _PyInt_FromGid(p->gr_gid));
     SET(setIndex++, w);
 #undef SET
 
@@ -86,17 +86,25 @@
 grp_getgrgid(PyObject *self, PyObject *pyo_id)
 {
     PyObject *py_int_id;
-    unsigned int gid;
+    gid_t gid;
     struct group *p;
 
     py_int_id = PyNumber_Int(pyo_id);
     if (!py_int_id)
-	    return NULL;
-    gid = PyInt_AS_LONG(py_int_id);
+            return NULL;
+    if (!_Py_Gid_Converter(py_int_id, &gid)) {
+        Py_DECREF(py_int_id);
+        return NULL;
+    }
     Py_DECREF(py_int_id);
 
     if ((p = getgrgid(gid)) == NULL) {
-	PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %d", gid);
+        if (gid < 0)
+            PyErr_Format(PyExc_KeyError,
+                         "getgrgid(): gid not found: %ld", (long)gid);
+        else
+            PyErr_Format(PyExc_KeyError,
+                         "getgrgid(): gid not found: %lu", (unsigned long)gid);
         return NULL;
     }
     return mkgrent(p);