ยปCore Development>Code coverage>Modules/grpmodule.c

Python code coverage for Modules/grpmodule.c

#countcontent
1n/a
2n/a/* UNIX group file access module */
3n/a
4n/a#include "Python.h"
5n/a#include "posixmodule.h"
6n/a
7n/a#include <grp.h>
8n/a
9n/a#include "clinic/grpmodule.c.h"
10n/a/*[clinic input]
11n/amodule grp
12n/a[clinic start generated code]*/
13n/a/*[clinic end generated code: output=da39a3ee5e6b4b0d input=cade63f2ed1bd9f8]*/
14n/a
15n/astatic PyStructSequence_Field struct_group_type_fields[] = {
16n/a {"gr_name", "group name"},
17n/a {"gr_passwd", "password"},
18n/a {"gr_gid", "group id"},
19n/a {"gr_mem", "group members"},
20n/a {0}
21n/a};
22n/a
23n/aPyDoc_STRVAR(struct_group__doc__,
24n/a"grp.struct_group: Results from getgr*() routines.\n\n\
25n/aThis object may be accessed either as a tuple of\n\
26n/a (gr_name,gr_passwd,gr_gid,gr_mem)\n\
27n/aor via the object attributes as named in the above tuple.\n");
28n/a
29n/astatic PyStructSequence_Desc struct_group_type_desc = {
30n/a "grp.struct_group",
31n/a struct_group__doc__,
32n/a struct_group_type_fields,
33n/a 4,
34n/a};
35n/a
36n/a
37n/astatic int initialized;
38n/astatic PyTypeObject StructGrpType;
39n/a
40n/astatic PyObject *
41n/amkgrent(struct group *p)
42n/a{
43n/a int setIndex = 0;
44n/a PyObject *v = PyStructSequence_New(&StructGrpType), *w;
45n/a char **member;
46n/a
47n/a if (v == NULL)
48n/a return NULL;
49n/a
50n/a if ((w = PyList_New(0)) == NULL) {
51n/a Py_DECREF(v);
52n/a return NULL;
53n/a }
54n/a for (member = p->gr_mem; *member != NULL; member++) {
55n/a PyObject *x = PyUnicode_DecodeFSDefault(*member);
56n/a if (x == NULL || PyList_Append(w, x) != 0) {
57n/a Py_XDECREF(x);
58n/a Py_DECREF(w);
59n/a Py_DECREF(v);
60n/a return NULL;
61n/a }
62n/a Py_DECREF(x);
63n/a }
64n/a
65n/a#define SET(i,val) PyStructSequence_SET_ITEM(v, i, val)
66n/a SET(setIndex++, PyUnicode_DecodeFSDefault(p->gr_name));
67n/a if (p->gr_passwd)
68n/a SET(setIndex++, PyUnicode_DecodeFSDefault(p->gr_passwd));
69n/a else {
70n/a SET(setIndex++, Py_None);
71n/a Py_INCREF(Py_None);
72n/a }
73n/a SET(setIndex++, _PyLong_FromGid(p->gr_gid));
74n/a SET(setIndex++, w);
75n/a#undef SET
76n/a
77n/a if (PyErr_Occurred()) {
78n/a Py_DECREF(v);
79n/a return NULL;
80n/a }
81n/a
82n/a return v;
83n/a}
84n/a
85n/a/*[clinic input]
86n/agrp.getgrgid
87n/a
88n/a id: object
89n/a
90n/aReturn the group database entry for the given numeric group ID.
91n/a
92n/aIf id is not valid, raise KeyError.
93n/a[clinic start generated code]*/
94n/a
95n/astatic PyObject *
96n/agrp_getgrgid_impl(PyObject *module, PyObject *id)
97n/a/*[clinic end generated code: output=30797c289504a1ba input=15fa0e2ccf5cda25]*/
98n/a{
99n/a PyObject *py_int_id;
100n/a gid_t gid;
101n/a struct group *p;
102n/a
103n/a if (!_Py_Gid_Converter(id, &gid)) {
104n/a if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
105n/a return NULL;
106n/a }
107n/a PyErr_Clear();
108n/a if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
109n/a "group id must be int, not %.200",
110n/a id->ob_type->tp_name) < 0) {
111n/a return NULL;
112n/a }
113n/a py_int_id = PyNumber_Long(id);
114n/a if (!py_int_id)
115n/a return NULL;
116n/a if (!_Py_Gid_Converter(py_int_id, &gid)) {
117n/a Py_DECREF(py_int_id);
118n/a return NULL;
119n/a }
120n/a Py_DECREF(py_int_id);
121n/a }
122n/a
123n/a if ((p = getgrgid(gid)) == NULL) {
124n/a PyObject *gid_obj = _PyLong_FromGid(gid);
125n/a if (gid_obj == NULL)
126n/a return NULL;
127n/a PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %S", gid_obj);
128n/a Py_DECREF(gid_obj);
129n/a return NULL;
130n/a }
131n/a return mkgrent(p);
132n/a}
133n/a
134n/a/*[clinic input]
135n/agrp.getgrnam
136n/a
137n/a name: unicode
138n/a
139n/aReturn the group database entry for the given group name.
140n/a
141n/aIf name is not valid, raise KeyError.
142n/a[clinic start generated code]*/
143n/a
144n/astatic PyObject *
145n/agrp_getgrnam_impl(PyObject *module, PyObject *name)
146n/a/*[clinic end generated code: output=67905086f403c21c input=08ded29affa3c863]*/
147n/a{
148n/a char *name_chars;
149n/a struct group *p;
150n/a PyObject *bytes, *retval = NULL;
151n/a
152n/a if ((bytes = PyUnicode_EncodeFSDefault(name)) == NULL)
153n/a return NULL;
154n/a if (PyBytes_AsStringAndSize(bytes, &name_chars, NULL) == -1)
155n/a goto out;
156n/a
157n/a if ((p = getgrnam(name_chars)) == NULL) {
158n/a PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %s", name_chars);
159n/a goto out;
160n/a }
161n/a retval = mkgrent(p);
162n/aout:
163n/a Py_DECREF(bytes);
164n/a return retval;
165n/a}
166n/a
167n/a/*[clinic input]
168n/agrp.getgrall
169n/a
170n/aReturn a list of all available group entries, in arbitrary order.
171n/a
172n/aAn entry whose name starts with '+' or '-' represents an instruction
173n/ato use YP/NIS and may not be accessible via getgrnam or getgrgid.
174n/a[clinic start generated code]*/
175n/a
176n/astatic PyObject *
177n/agrp_getgrall_impl(PyObject *module)
178n/a/*[clinic end generated code: output=585dad35e2e763d7 input=d7df76c825c367df]*/
179n/a{
180n/a PyObject *d;
181n/a struct group *p;
182n/a
183n/a if ((d = PyList_New(0)) == NULL)
184n/a return NULL;
185n/a setgrent();
186n/a while ((p = getgrent()) != NULL) {
187n/a PyObject *v = mkgrent(p);
188n/a if (v == NULL || PyList_Append(d, v) != 0) {
189n/a Py_XDECREF(v);
190n/a Py_DECREF(d);
191n/a endgrent();
192n/a return NULL;
193n/a }
194n/a Py_DECREF(v);
195n/a }
196n/a endgrent();
197n/a return d;
198n/a}
199n/a
200n/astatic PyMethodDef grp_methods[] = {
201n/a GRP_GETGRGID_METHODDEF
202n/a GRP_GETGRNAM_METHODDEF
203n/a GRP_GETGRALL_METHODDEF
204n/a {NULL, NULL}
205n/a};
206n/a
207n/aPyDoc_STRVAR(grp__doc__,
208n/a"Access to the Unix group database.\n\
209n/a\n\
210n/aGroup entries are reported as 4-tuples containing the following fields\n\
211n/afrom the group database, in order:\n\
212n/a\n\
213n/a gr_name - name of the group\n\
214n/a gr_passwd - group password (encrypted); often empty\n\
215n/a gr_gid - numeric ID of the group\n\
216n/a gr_mem - list of members\n\
217n/a\n\
218n/aThe gid is an integer, name and password are strings. (Note that most\n\
219n/ausers are not explicitly listed as members of the groups they are in\n\
220n/aaccording to the password database. Check both databases to get\n\
221n/acomplete membership information.)");
222n/a
223n/a
224n/a
225n/astatic struct PyModuleDef grpmodule = {
226n/a PyModuleDef_HEAD_INIT,
227n/a "grp",
228n/a grp__doc__,
229n/a -1,
230n/a grp_methods,
231n/a NULL,
232n/a NULL,
233n/a NULL,
234n/a NULL
235n/a};
236n/a
237n/aPyMODINIT_FUNC
238n/aPyInit_grp(void)
239n/a{
240n/a PyObject *m, *d;
241n/a m = PyModule_Create(&grpmodule);
242n/a if (m == NULL)
243n/a return NULL;
244n/a d = PyModule_GetDict(m);
245n/a if (!initialized) {
246n/a if (PyStructSequence_InitType2(&StructGrpType,
247n/a &struct_group_type_desc) < 0)
248n/a return NULL;
249n/a }
250n/a if (PyDict_SetItemString(d, "struct_group",
251n/a (PyObject *)&StructGrpType) < 0)
252n/a return NULL;
253n/a initialized = 1;
254n/a return m;
255n/a}