Issue #20152: Convert the grp module to Argument Clinic.

This commit is contained in:
Brett Cannon 2014-08-22 11:52:46 -04:00
parent f2de1fc21a
commit 8fb7bb2f29
2 changed files with 143 additions and 29 deletions

View File

@ -0,0 +1,87 @@
/*[clinic input]
preserve
[clinic start generated code]*/
PyDoc_STRVAR(grp_getgrgid__doc__,
"getgrgid($module, /, id)\n"
"--\n"
"\n"
"Return the group database entry for the given numeric group ID.\n"
"\n"
"If id is not valid, raise KeyError.");
#define GRP_GETGRGID_METHODDEF \
{"getgrgid", (PyCFunction)grp_getgrgid, METH_VARARGS|METH_KEYWORDS, grp_getgrgid__doc__},
static PyObject *
grp_getgrgid_impl(PyModuleDef *module, PyObject *id);
static PyObject *
grp_getgrgid(PyModuleDef *module, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
static char *_keywords[] = {"id", NULL};
PyObject *id;
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"O:getgrgid", _keywords,
&id))
goto exit;
return_value = grp_getgrgid_impl(module, id);
exit:
return return_value;
}
PyDoc_STRVAR(grp_getgrnam__doc__,
"getgrnam($module, /, name)\n"
"--\n"
"\n"
"Return the group database entry for the given group name.\n"
"\n"
"If name is not valid, raise KeyError.");
#define GRP_GETGRNAM_METHODDEF \
{"getgrnam", (PyCFunction)grp_getgrnam, METH_VARARGS|METH_KEYWORDS, grp_getgrnam__doc__},
static PyObject *
grp_getgrnam_impl(PyModuleDef *module, PyObject *name);
static PyObject *
grp_getgrnam(PyModuleDef *module, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
static char *_keywords[] = {"name", NULL};
PyObject *name;
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"U:getgrnam", _keywords,
&name))
goto exit;
return_value = grp_getgrnam_impl(module, name);
exit:
return return_value;
}
PyDoc_STRVAR(grp_getgrall__doc__,
"getgrall($module, /)\n"
"--\n"
"\n"
"Return a list of all available group entries, in arbitrary order.\n"
"\n"
"An entry whose name starts with \'+\' or \'-\' represents an instruction\n"
"to use YP/NIS and may not be accessible via getgrnam or getgrgid.");
#define GRP_GETGRALL_METHODDEF \
{"getgrall", (PyCFunction)grp_getgrall, METH_NOARGS, grp_getgrall__doc__},
static PyObject *
grp_getgrall_impl(PyModuleDef *module);
static PyObject *
grp_getgrall(PyModuleDef *module, PyObject *Py_UNUSED(ignored))
{
return grp_getgrall_impl(module);
}
/*[clinic end generated code: output=4709a6ba40bb8df9 input=a9049054013a1b77]*/

View File

@ -6,6 +6,13 @@
#include <grp.h>
#include "clinic/grpmodule.c.h"
/*[clinic input]
output preset file
module grp
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=68180a9a9efb8506]*/
static PyStructSequence_Field struct_group_type_fields[] = {
{"gr_name", "group name"},
{"gr_passwd", "password"},
@ -76,14 +83,25 @@ mkgrent(struct group *p)
return v;
}
/*[clinic input]
grp.getgrgid
id: object
Return the group database entry for the given numeric group ID.
If id is not valid, raise KeyError.
[clinic start generated code]*/
static PyObject *
grp_getgrgid(PyObject *self, PyObject *pyo_id)
grp_getgrgid_impl(PyModuleDef *module, PyObject *id)
/*[clinic end generated code: output=8a11f5fdeb8c78a0 input=15fa0e2ccf5cda25]*/
{
PyObject *py_int_id;
gid_t gid;
struct group *p;
py_int_id = PyNumber_Long(pyo_id);
py_int_id = PyNumber_Long(id);
if (!py_int_id)
return NULL;
if (!_Py_Gid_Converter(py_int_id, &gid)) {
@ -103,22 +121,31 @@ grp_getgrgid(PyObject *self, PyObject *pyo_id)
return mkgrent(p);
}
static PyObject *
grp_getgrnam(PyObject *self, PyObject *args)
{
char *name;
struct group *p;
PyObject *arg, *bytes, *retval = NULL;
/*[clinic input]
grp.getgrnam
if (!PyArg_ParseTuple(args, "U:getgrnam", &arg))
name: unicode
Return the group database entry for the given group name.
If name is not valid, raise KeyError.
[clinic start generated code]*/
static PyObject *
grp_getgrnam_impl(PyModuleDef *module, PyObject *name)
/*[clinic end generated code: output=cd47511f4854da8e input=08ded29affa3c863]*/
{
char *name_chars;
struct group *p;
PyObject *bytes, *retval = NULL;
if ((bytes = PyUnicode_EncodeFSDefault(name)) == NULL)
return NULL;
if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL)
return NULL;
if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
if (PyBytes_AsStringAndSize(bytes, &name_chars, NULL) == -1)
goto out;
if ((p = getgrnam(name)) == NULL) {
PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %s", name);
if ((p = getgrnam(name_chars)) == NULL) {
PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %s", name_chars);
goto out;
}
retval = mkgrent(p);
@ -127,8 +154,18 @@ out:
return retval;
}
/*[clinic input]
grp.getgrall
Return a list of all available group entries, in arbitrary order.
An entry whose name starts with '+' or '-' represents an instruction
to use YP/NIS and may not be accessible via getgrnam or getgrgid.
[clinic start generated code]*/
static PyObject *
grp_getgrall(PyObject *self, PyObject *ignore)
grp_getgrall_impl(PyModuleDef *module)
/*[clinic end generated code: output=add9037a20c202de input=d7df76c825c367df]*/
{
PyObject *d;
struct group *p;
@ -151,20 +188,10 @@ grp_getgrall(PyObject *self, PyObject *ignore)
}
static PyMethodDef grp_methods[] = {
{"getgrgid", grp_getgrgid, METH_O,
"getgrgid(id) -> tuple\n\
Return the group database entry for the given numeric group ID. If\n\
id is not valid, raise KeyError."},
{"getgrnam", grp_getgrnam, METH_VARARGS,
"getgrnam(name) -> tuple\n\
Return the group database entry for the given group name. If\n\
name is not valid, raise KeyError."},
{"getgrall", grp_getgrall, METH_NOARGS,
"getgrall() -> list of tuples\n\
Return a list of all available group entries, in arbitrary order.\n\
An entry whose name starts with '+' or '-' represents an instruction\n\
to use YP/NIS and may not be accessible via getgrnam or getgrgid."},
{NULL, NULL} /* sentinel */
GRP_GETGRGID_METHODDEF
GRP_GETGRNAM_METHODDEF
GRP_GETGRALL_METHODDEF
{NULL, NULL}
};
PyDoc_STRVAR(grp__doc__,