]> granicus.if.org Git - python/commitdiff
Issue #26129: Deprecated accepting non-integers in grp.getgrgid().
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 18 Jan 2016 16:49:57 +0000 (18:49 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Mon, 18 Jan 2016 16:49:57 +0000 (18:49 +0200)
Doc/library/grp.rst
Lib/test/test_grp.py
Misc/NEWS
Modules/grpmodule.c

index 88821406a33d690ae774321fcc920e9754208f99..c840cfe67a2b16a8caff71febdd513927e03b8d9 100644 (file)
@@ -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)
 
index 272b08615dcc22403e211c7b62989547163efac0..69095a3fb98d743347e67182366ecd904cabb726 100644 (file)
@@ -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()
index 4564e29095bf58f57e12cd671d16441e265b21f4..d3edac7552cebb3ea3f64723915aeb7edce37208 100644 (file)
--- 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.
index 403e434697b0e414f3199784b3cf45b713e03ed9..5ad87f1ca8a50c9d2ee8ab03f51f81ef0e7bc69b 100644 (file)
@@ -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);