Issue #26129: Deprecated accepting non-integers in grp.getgrgid().

This commit is contained in:
Serhiy Storchaka 2016-01-18 18:49:57 +02:00
parent 9def284387
commit 9cc4ed5c7a
4 changed files with 31 additions and 5 deletions

View File

@ -42,6 +42,9 @@ It defines the following items:
Return the group database entry for the given numeric group ID. :exc:`KeyError` Return the group database entry for the given numeric group ID. :exc:`KeyError`
is raised if the entry asked for cannot be found. is raised if the entry asked for cannot be found.
.. deprecated:: 3.6
Since Python 3.6 the support of non-integer arguments like floats or
strings in :func:`getgrgid` is deprecated.
.. function:: getgrnam(name) .. function:: getgrnam(name)

View File

@ -92,5 +92,15 @@ class GroupDatabaseTestCase(unittest.TestCase):
self.assertRaises(KeyError, grp.getgrgid, fakegid) self.assertRaises(KeyError, grp.getgrgid, fakegid)
def test_noninteger_gid(self):
entries = grp.getgrall()
if not entries:
self.skipTest('no groups')
# Choose an existent gid.
gid = entries[0][2]
self.assertWarns(DeprecationWarning, grp.getgrgid, float(gid))
self.assertWarns(DeprecationWarning, grp.getgrgid, str(gid))
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -131,6 +131,8 @@ Core and Builtins
Library Library
------- -------
- Issue #26129: Deprecated accepting non-integers in grp.getgrgid().
- Issue #25850: Use cross-compilation by default for 64-bit Windows. - Issue #25850: Use cross-compilation by default for 64-bit Windows.
- Issue #25822: Add docstrings to the fields of urllib.parse results. - Issue #25822: Add docstrings to the fields of urllib.parse results.

View File

@ -100,6 +100,16 @@ grp_getgrgid_impl(PyModuleDef *module, PyObject *id)
gid_t gid; gid_t gid;
struct group *p; struct group *p;
if (!_Py_Gid_Converter(id, &gid)) {
if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
return NULL;
}
PyErr_Clear();
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
"group id must be int, not %.200",
id->ob_type->tp_name) < 0) {
return NULL;
}
py_int_id = PyNumber_Long(id); py_int_id = PyNumber_Long(id);
if (!py_int_id) if (!py_int_id)
return NULL; return NULL;
@ -108,6 +118,7 @@ grp_getgrgid_impl(PyModuleDef *module, PyObject *id)
return NULL; return NULL;
} }
Py_DECREF(py_int_id); Py_DECREF(py_int_id);
}
if ((p = getgrgid(gid)) == NULL) { if ((p = getgrgid(gid)) == NULL) {
PyObject *gid_obj = _PyLong_FromGid(gid); PyObject *gid_obj = _PyLong_FromGid(gid);