summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-01-18 16:49:57 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2016-01-18 16:49:57 (GMT)
commit9cc4ed5c7a86b2aa58176222107dbb01d40a3680 (patch)
tree7a8e8b0a9ecf98a61a833e8fc6a0b69505d406c3
parent9def2843873edde3feec6eaf2ee60c4e48172164 (diff)
downloadcpython-9cc4ed5c7a86b2aa58176222107dbb01d40a3680.zip
cpython-9cc4ed5c7a86b2aa58176222107dbb01d40a3680.tar.gz
cpython-9cc4ed5c7a86b2aa58176222107dbb01d40a3680.tar.bz2
Issue #26129: Deprecated accepting non-integers in grp.getgrgid().
-rw-r--r--Doc/library/grp.rst3
-rw-r--r--Lib/test/test_grp.py10
-rw-r--r--Misc/NEWS2
-rw-r--r--Modules/grpmodule.c21
4 files changed, 31 insertions, 5 deletions
diff --git a/Doc/library/grp.rst b/Doc/library/grp.rst
index 8882140..c840cfe 100644
--- a/Doc/library/grp.rst
+++ b/Doc/library/grp.rst
@@ -42,6 +42,9 @@ It defines the following items:
Return the group database entry for the given numeric group ID. :exc:`KeyError`
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)
diff --git a/Lib/test/test_grp.py b/Lib/test/test_grp.py
index 272b086..69095a3 100644
--- a/Lib/test/test_grp.py
+++ b/Lib/test/test_grp.py
@@ -92,5 +92,15 @@ class GroupDatabaseTestCase(unittest.TestCase):
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__":
unittest.main()
diff --git a/Misc/NEWS b/Misc/NEWS
index 4564e29..d3edac7 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -131,6 +131,8 @@ Core and Builtins
Library
-------
+- Issue #26129: Deprecated accepting non-integers in grp.getgrgid().
+
- Issue #25850: Use cross-compilation by default for 64-bit Windows.
- Issue #25822: Add docstrings to the fields of urllib.parse results.
diff --git a/Modules/grpmodule.c b/Modules/grpmodule.c
index 403e434..5ad87f1 100644
--- a/Modules/grpmodule.c
+++ b/Modules/grpmodule.c
@@ -100,14 +100,25 @@ grp_getgrgid_impl(PyModuleDef *module, PyObject *id)
gid_t gid;
struct group *p;
- py_int_id = PyNumber_Long(id);
- if (!py_int_id)
+ if (!_Py_Gid_Converter(id, &gid)) {
+ if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
return NULL;
- if (!_Py_Gid_Converter(py_int_id, &gid)) {
+ }
+ 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);
+ if (!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);
- return NULL;
}
- Py_DECREF(py_int_id);
if ((p = getgrgid(gid)) == NULL) {
PyObject *gid_obj = _PyLong_FromGid(gid);