Index: Modules/posixmodule.c =================================================================== --- Modules/posixmodule.c (revision 78371) +++ Modules/posixmodule.c (working copy) @@ -3841,17 +3841,41 @@ #define MAX_GROUPS 64 #endif gid_t grouplist[MAX_GROUPS]; + gid_t* alt_grouplist = NULL; int n; n = getgroups(MAX_GROUPS, grouplist); - if (n < 0) - posix_error(); - else { - result = PyList_New(n); - if (result != NULL) { + if (n < 0) { + if (errno == EINVAL) { + n = getgroups(0, grouplist); + if (n == -1) { + return posix_error(); + } + if (n == 0) { + alt_grouplist = grouplist; + } else { + alt_grouplist = PyMem_Malloc(n); + if (alt_grouplist == NULL) { + errno = EINVAL; + return posix_error(); + } + n = getgroups(n, grouplist); + if (n == -1) { + PyMem_Free(alt_grouplist); + return posix_error; + } + } + } else { + return posix_error(); + } + } else { + alt_grouplist = grouplist; + } + result = PyList_New(n); + if (result != NULL) { int i; for (i = 0; i < n; ++i) { - PyObject *o = PyInt_FromLong((long)grouplist[i]); + PyObject *o = PyInt_FromLong((long)alt_grouplist[i]); if (o == NULL) { Py_DECREF(result); result = NULL; @@ -3859,10 +3883,13 @@ } PyList_SET_ITEM(result, i, o); } - } - } + } - return result; + if (alt_grouplist != grouplist) { + PyMem_Free(alt_grouplist); + } + + return result; } #endif