Skip to content

Commit db3ffd0

Browse files
gh-156261: Use the uid_t and gid_t converters in the pwd and grp modules (GH-156294)
They are moved to Argument Clinic, so that pwd.getpwuid() and grp.getgrgid() can declare the type of their parameter instead of converting it in the "impl" function. pwd.getpwuid() now raises OverflowError instead of KeyError for a user id out of the range of uid_t, as grp.getgrgid() already did.
1 parent 4abf0d4 commit db3ffd0

10 files changed

Lines changed: 53 additions & 37 deletions

File tree

‎Lib/test/test_clinic.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4014,6 +4014,7 @@ def test_cli_converters(self):
40144014
"DWORD",
40154015
"fildes",
40164016
"float",
4017+
"gid_t",
40174018
"HANDLE",
40184019
"int",
40194020
"long",
@@ -4032,6 +4033,7 @@ def test_cli_converters(self):
40324033
"size_t",
40334034
"slice_index",
40344035
"str",
4036+
"uid_t",
40354037
"uint16",
40364038
"uint32",
40374039
"uint64",

‎Lib/test/test_pwd.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ def test_errors(self):
6161
self.assertRaises(TypeError, pwd.getpwuid, 0.0)
6262
self.assertRaises(TypeError, pwd.getpwuid, 0, 0)
6363
# should be out of uid_t range
64-
self.assertRaises(KeyError, pwd.getpwuid, 2**128)
65-
self.assertRaises(KeyError, pwd.getpwuid, -2**128)
64+
self.assertRaises(OverflowError, pwd.getpwuid, 2**128)
65+
self.assertRaises(OverflowError, pwd.getpwuid, -2**128)
6666
self.assertRaises(TypeError, pwd.getpwnam)
6767
self.assertRaises(TypeError, pwd.getpwnam, 42)
6868
self.assertRaises(TypeError, pwd.getpwnam, b'root')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:func:`pwd.getpwuid` now raises :exc:`OverflowError` instead of
2+
:exc:`KeyError` if the user id is out of the range of the C ``uid_t`` type,
3+
as :func:`grp.getgrgid` does for the group id.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Argument Clinic: the ``pid_t``, ``Py_off_t``, ``HANDLE``, ``DWORD`` and
2-
``BOOL`` converters, previously defined in 9 different files, are now
3-
provided by Argument Clinic itself.
1+
Argument Clinic: the ``pid_t``, ``uid_t``, ``gid_t``, ``Py_off_t``,
2+
``HANDLE``, ``DWORD`` and ``BOOL`` converters, previously defined in
3+
individual files, are now provided by Argument Clinic itself.

‎Modules/clinic/grpmodule.c.h‎

Lines changed: 7 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Modules/clinic/pwdmodule.c.h‎

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Modules/grpmodule.c‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,26 +117,22 @@ mkgrent(PyObject *module, struct group *p)
117117
/*[clinic input]
118118
grp.getgrgid
119119
120-
id: object
120+
id as gid: gid_t
121121
122122
Return the group database entry for the given numeric group ID.
123123
124124
If id is not valid, raise KeyError.
125125
[clinic start generated code]*/
126126

127127
static PyObject *
128-
grp_getgrgid_impl(PyObject *module, PyObject *id)
129-
/*[clinic end generated code: output=30797c289504a1ba input=15fa0e2ccf5cda25]*/
128+
grp_getgrgid_impl(PyObject *module, gid_t gid)
129+
/*[clinic end generated code: output=a9e7385cd6df08da input=fca15128dd772588]*/
130130
{
131131
PyObject *retval = NULL;
132132
int nomem = 0;
133133
char *buf = NULL, *buf2 = NULL;
134-
gid_t gid;
135134
struct group *p;
136135

137-
if (!_Py_Gid_Converter(id, &gid)) {
138-
return NULL;
139-
}
140136
#ifdef HAVE_GETGRGID_R
141137
int status;
142138
Py_ssize_t bufsize;

‎Modules/posixmodule.c‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3204,14 +3204,6 @@ class dir_fd_converter(CConverter):
32043204
def c_default_init(self):
32053205
self.c_default = 'DEFAULT_DIR_FD'
32063206

3207-
class uid_t_converter(CConverter):
3208-
type = "uid_t"
3209-
converter = '_Py_Uid_Converter'
3210-
3211-
class gid_t_converter(CConverter):
3212-
type = "gid_t"
3213-
converter = '_Py_Gid_Converter'
3214-
32153207
class dev_t_converter(CConverter):
32163208
type = 'dev_t'
32173209
converter = '_Py_Dev_Converter'
@@ -3268,7 +3260,7 @@ class confname_converter(CConverter):
32683260
""", argname=argname, converter=self.converter, table=self.table)
32693261

32703262
[python start generated code]*/
3271-
/*[python end generated code: output=da39a3ee5e6b4b0d input=e459765bdf453ebf]*/
3263+
/*[python end generated code: output=da39a3ee5e6b4b0d input=7ceccf55bb600f61]*/
32723264

32733265
/*[clinic input]
32743266

‎Modules/pwdmodule.c‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ mkpwent(PyObject *module, struct passwd *p)
121121
/*[clinic input]
122122
pwd.getpwuid
123123
124-
uidobj: object
124+
uidobj as uid: uid_t
125125
/
126126
127127
Return the password database entry for the given numeric user ID.
@@ -130,21 +130,14 @@ See `help(pwd)` for more on password database entries.
130130
[clinic start generated code]*/
131131

132132
static PyObject *
133-
pwd_getpwuid(PyObject *module, PyObject *uidobj)
134-
/*[clinic end generated code: output=c4ee1d4d429b86c4 input=ae64d507a1c6d3e8]*/
133+
pwd_getpwuid_impl(PyObject *module, uid_t uid)
134+
/*[clinic end generated code: output=631bad376fa670c3 input=506d3a592ef19799]*/
135135
{
136136
PyObject *retval = NULL;
137-
uid_t uid;
138137
int nomem = 0;
139138
struct passwd *p;
140139
char *buf = NULL, *buf2 = NULL;
141140

142-
if (!_Py_Uid_Converter(uidobj, &uid)) {
143-
if (PyErr_ExceptionMatches(PyExc_OverflowError))
144-
PyErr_Format(PyExc_KeyError,
145-
"getpwuid(): uid not found");
146-
return NULL;
147-
}
148141
#ifdef HAVE_GETPWUID_R
149142
int status;
150143
Py_ssize_t bufsize;

‎Tools/clinic/libclinic/converters.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,16 @@ def parse_arg(self, argname: str, displayname: str, *, limited_capi: bool) -> st
601601
argname=argname)
602602

603603

604+
class gid_t_converter(CConverter):
605+
type = 'gid_t'
606+
converter = '_Py_Gid_Converter'
607+
608+
609+
class uid_t_converter(CConverter):
610+
type = 'uid_t'
611+
converter = '_Py_Uid_Converter'
612+
613+
604614
class pid_t_converter(CConverter):
605615
type = 'pid_t'
606616
format_unit = '" _Py_PARSE_PID "'

0 commit comments

Comments
 (0)