File tree Expand file tree Collapse file tree 4 files changed +31
-5
lines changed Expand file tree Collapse file tree 4 files changed +31
-5
lines changed Original file line number Diff line number Diff line change @@ -42,6 +42,9 @@ It defines the following items:
4242 Return the group database entry for the given numeric group ID. :exc: `KeyError `
4343 is raised if the entry asked for cannot be found.
4444
45+ .. deprecated :: 3.6
46+ Since Python 3.6 the support of non-integer arguments like floats or
47+ strings in :func: `getgrgid ` is deprecated.
4548
4649.. function :: getgrnam(name)
4750
Original file line number Diff line number Diff line change @@ -92,5 +92,15 @@ def test_errors(self):
9292
9393 self .assertRaises (KeyError , grp .getgrgid , fakegid )
9494
95+ def test_noninteger_gid (self ):
96+ entries = grp .getgrall ()
97+ if not entries :
98+ self .skipTest ('no groups' )
99+ # Choose an existent gid.
100+ gid = entries [0 ][2 ]
101+ self .assertWarns (DeprecationWarning , grp .getgrgid , float (gid ))
102+ self .assertWarns (DeprecationWarning , grp .getgrgid , str (gid ))
103+
104+
95105if __name__ == "__main__" :
96106 unittest .main ()
Original file line number Diff line number Diff line change @@ -131,6 +131,8 @@ Core and Builtins
131131Library
132132-------
133133
134+ - Issue #26129: Deprecated accepting non-integers in grp.getgrgid().
135+
134136- Issue #25850: Use cross-compilation by default for 64-bit Windows.
135137
136138- Issue #25822: Add docstrings to the fields of urllib.parse results.
Original file line number Diff line number Diff line change @@ -100,14 +100,25 @@ grp_getgrgid_impl(PyModuleDef *module, PyObject *id)
100100 gid_t gid ;
101101 struct group * p ;
102102
103- py_int_id = PyNumber_Long ( id );
104- if (!py_int_id )
103+ if (! _Py_Gid_Converter ( id , & gid )) {
104+ if (!PyErr_ExceptionMatches ( PyExc_TypeError )) {
105105 return NULL ;
106- if (!_Py_Gid_Converter (py_int_id , & gid )) {
106+ }
107+ PyErr_Clear ();
108+ if (PyErr_WarnFormat (PyExc_DeprecationWarning , 1 ,
109+ "group id must be int, not %.200" ,
110+ id -> ob_type -> tp_name ) < 0 ) {
111+ return NULL ;
112+ }
113+ py_int_id = PyNumber_Long (id );
114+ if (!py_int_id )
115+ return NULL ;
116+ if (!_Py_Gid_Converter (py_int_id , & gid )) {
117+ Py_DECREF (py_int_id );
118+ return NULL ;
119+ }
107120 Py_DECREF (py_int_id );
108- return NULL ;
109121 }
110- Py_DECREF (py_int_id );
111122
112123 if ((p = getgrgid (gid )) == NULL ) {
113124 PyObject * gid_obj = _PyLong_FromGid (gid );
You can’t perform that action at this time.
0 commit comments