Index: Modules/posixmodule.c =================================================================== --- Modules/posixmodule.c (révision 70584) +++ Modules/posixmodule.c (copie de travail) @@ -1394,8 +1394,10 @@ PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev)); #endif PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink)); - PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid)); - PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid)); + PyStructSequence_SET_ITEM(v, 4, + PyLong_FromUnsignedLong((unsigned long)st->st_uid)); + PyStructSequence_SET_ITEM(v, 5, + PyLong_FromUnsignedLong((unsigned long)st->st_gid)); #ifdef HAVE_LARGEFILE_SUPPORT PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); @@ -1971,6 +1973,71 @@ } #endif /* HAVE_FDATASYNC */ +static int +parse_uid(PyObject *elem, uid_t *uid) +{ + if (PyInt_Check(elem)) { + long x = PyInt_AsLong(elem); + *uid = x; + /* read back the value to see if it fitted in uid_t */ + if (*uid != x) { + PyErr_SetString(PyExc_TypeError, "user id too big"); + return 0; + } + return 1; + } + if (PyLong_Check(elem)) { + unsigned long x = PyLong_AsUnsignedLong(elem); + if (PyErr_Occurred()) { + PyErr_SetString(PyExc_TypeError, + "user id too big"); + return 0; + } + *uid = x; + /* read back the value to see if it fitted in uid_t */ + if (*uid != x) { + PyErr_SetString(PyExc_TypeError, + "user id too big"); + return 0; + } + return 1; + } + PyErr_SetString(PyExc_TypeError, "user id must be integer"); + return 0; +} + +static int +parse_gid(PyObject *elem, gid_t *gid) +{ + if (PyInt_Check(elem)) { + long x = PyInt_AsLong(elem); + *gid = x; + /* read back the value to see if it fitted in gid_t */ + if (*gid != x) { + PyErr_SetString(PyExc_TypeError, "group id too big"); + return 0; + } + return 1; + } + if (PyLong_Check(elem)) { + unsigned long x = PyLong_AsUnsignedLong(elem); + if (PyErr_Occurred()) { + PyErr_SetString(PyExc_TypeError, + "group id too big"); + return 0; + } + *gid = x; + /* read back the value to see if it fitted in gid_t */ + if (*gid != x) { + PyErr_SetString(PyExc_TypeError, + "group id too big"); + return 0; + } + return 1; + } + PyErr_SetString(PyExc_TypeError, "group id must be integer"); + return 0; +} #ifdef HAVE_CHOWN PyDoc_STRVAR(posix_chown__doc__, @@ -1981,14 +2048,20 @@ posix_chown(PyObject *self, PyObject *args) { char *path = NULL; - long uid, gid; + PyObject *uidobj, *gidobj; + uid_t uid; + gid_t gid; int res; - if (!PyArg_ParseTuple(args, "etll:chown", + if (!PyArg_ParseTuple(args, "etOO:chown", Py_FileSystemDefaultEncoding, &path, - &uid, &gid)) + &uidobj, &gidobj)) + return NULL; + if (!parse_uid(uidobj, &uid)) + return NULL; + if (!parse_gid(gidobj, &gid)) return NULL; Py_BEGIN_ALLOW_THREADS - res = chown(path, (uid_t) uid, (gid_t) gid); + res = chown(path, uid, gid); Py_END_ALLOW_THREADS if (res < 0) return posix_error_with_allocated_filename(path); @@ -2007,9 +2080,16 @@ static PyObject * posix_fchown(PyObject *self, PyObject *args) { - int fd, uid, gid; + int fd; + PyObject *uidobj, *gidobj; + uid_t uid; + gid_t gid; int res; - if (!PyArg_ParseTuple(args, "iii:chown", &fd, &uid, &gid)) + if (!PyArg_ParseTuple(args, "iOO:fchown", &fd, &uidobj, &gidobj)) + return NULL; + if (!parse_uid(uidobj, &uid)) + return NULL; + if (!parse_gid(gidobj, &gid)) return NULL; Py_BEGIN_ALLOW_THREADS res = fchown(fd, (uid_t) uid, (gid_t) gid); @@ -5740,38 +5820,9 @@ elem = PySequence_GetItem(groups, i); if (!elem) return NULL; - if (!PyInt_Check(elem)) { - if (!PyLong_Check(elem)) { - PyErr_SetString(PyExc_TypeError, - "groups must be integers"); + if (!parse_gid(elem, &grouplist[i])) { Py_DECREF(elem); return NULL; - } else { - unsigned long x = PyLong_AsUnsignedLong(elem); - if (PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, - "group id too big"); - Py_DECREF(elem); - return NULL; - } - grouplist[i] = x; - /* read back the value to see if it fitted in gid_t */ - if (grouplist[i] != x) { - PyErr_SetString(PyExc_TypeError, - "group id too big"); - Py_DECREF(elem); - return NULL; - } - } - } else { - long x = PyInt_AsLong(elem); - grouplist[i] = x; - if (grouplist[i] != x) { - PyErr_SetString(PyExc_TypeError, - "group id too big"); - Py_DECREF(elem); - return NULL; - } } Py_DECREF(elem); }