Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(4)

Side by Side Diff: Modules/grpmodule.c

Issue 20152: Derby: Convert the _imp module to use Argument Clinic
Patch Set: Created 6 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* UNIX group file access module */ 2 /* UNIX group file access module */
3 3
4 #include "Python.h" 4 #include "Python.h"
5 #include "posixmodule.h" 5 #include "posixmodule.h"
6 6
7 #include <grp.h> 7 #include <grp.h>
8
9 /*[clinic input]
10 module grp
11 [clinic start generated code]*/
12 /*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709] */
8 13
9 static PyStructSequence_Field struct_group_type_fields[] = { 14 static PyStructSequence_Field struct_group_type_fields[] = {
10 {"gr_name", "group name"}, 15 {"gr_name", "group name"},
11 {"gr_passwd", "password"}, 16 {"gr_passwd", "password"},
12 {"gr_gid", "group id"}, 17 {"gr_gid", "group id"},
13 {"gr_mem", "group members"}, 18 {"gr_mem", "group members"},
14 {0} 19 {0}
15 }; 20 };
16 21
17 PyDoc_STRVAR(struct_group__doc__, 22 PyDoc_STRVAR(struct_group__doc__,
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 #undef SET 74 #undef SET
70 75
71 if (PyErr_Occurred()) { 76 if (PyErr_Occurred()) {
72 Py_DECREF(v); 77 Py_DECREF(v);
73 return NULL; 78 return NULL;
74 } 79 }
75 80
76 return v; 81 return v;
77 } 82 }
78 83
84 /*[clinic input]
85 grp.getgrgid
86
87 id: object
loewis 2014/07/27 17:47:13 You should probably use the positional-only syntax
88
89 Return the group database entry for the given numeric group ID.
90
91 If id is not valid, raise KeyError.
92 [clinic start generated code]*/
93
94 PyDoc_STRVAR(grp_getgrgid__doc__,
95 "getgrgid(id)\n"
96 "Return the group database entry for the given numeric group ID.\n"
97 "\n"
98 "If id is not valid, raise KeyError.");
99
100 #define GRP_GETGRGID_METHODDEF \
101 {"getgrgid", (PyCFunction)grp_getgrgid, METH_VARARGS|METH_KEYWORDS, grp_getg rgid__doc__},
102
79 static PyObject * 103 static PyObject *
80 grp_getgrgid(PyObject *self, PyObject *pyo_id) 104 grp_getgrgid_impl(PyModuleDef *module, PyObject *id);
105
106 static PyObject *
107 grp_getgrgid(PyModuleDef *module, PyObject *args, PyObject *kwargs)
108 {
109 PyObject *return_value = NULL;
110 static char *_keywords[] = {"id", NULL};
111 PyObject *id;
112
113 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
114 "O:getgrgid", _keywords,
115 &id))
116 goto exit;
117 return_value = grp_getgrgid_impl(module, id);
118
119 exit:
120 return return_value;
121 }
122
123 static PyObject *
124 grp_getgrgid_impl(PyModuleDef *module, PyObject *id)
125 /*[clinic end generated code: checksum=6e90fbc6922ac8a6b1a1ddc39287a78c47325fd4] */
81 { 126 {
82 PyObject *py_int_id; 127 PyObject *py_int_id;
83 gid_t gid; 128 gid_t gid;
84 struct group *p; 129 struct group *p;
85 130
86 py_int_id = PyNumber_Long(pyo_id); 131 py_int_id = PyNumber_Long(id);
87 if (!py_int_id) 132 if (!py_int_id)
88 return NULL; 133 return NULL;
89 if (!_Py_Gid_Converter(py_int_id, &gid)) { 134 if (!_Py_Gid_Converter(py_int_id, &gid)) {
90 Py_DECREF(py_int_id); 135 Py_DECREF(py_int_id);
91 return NULL; 136 return NULL;
92 } 137 }
93 Py_DECREF(py_int_id); 138 Py_DECREF(py_int_id);
94 139
95 if ((p = getgrgid(gid)) == NULL) { 140 if ((p = getgrgid(gid)) == NULL) {
96 PyObject *gid_obj = _PyLong_FromGid(gid); 141 PyObject *gid_obj = _PyLong_FromGid(gid);
97 if (gid_obj == NULL) 142 if (gid_obj == NULL)
98 return NULL; 143 return NULL;
99 PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %S", gid_obj); 144 PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %S", gid_obj);
100 Py_DECREF(gid_obj); 145 Py_DECREF(gid_obj);
101 return NULL; 146 return NULL;
102 } 147 }
103 return mkgrent(p); 148 return mkgrent(p);
104 } 149 }
105 150
151 /*[clinic input]
152 grp.getgrnam
153
154 name: unicode
loewis 2014/07/27 17:47:13 Likewise.
155
156 Return the group database entry for the given group name.
157
158 If name is not valid, raise KeyError.
159 [clinic start generated code]*/
160
161 PyDoc_STRVAR(grp_getgrnam__doc__,
162 "getgrnam(name)\n"
163 "Return the group database entry for the given group name.\n"
164 "\n"
165 "If name is not valid, raise KeyError.");
166
167 #define GRP_GETGRNAM_METHODDEF \
168 {"getgrnam", (PyCFunction)grp_getgrnam, METH_VARARGS|METH_KEYWORDS, grp_getg rnam__doc__},
169
106 static PyObject * 170 static PyObject *
107 grp_getgrnam(PyObject *self, PyObject *args) 171 grp_getgrnam_impl(PyModuleDef *module, PyObject *name);
172
173 static PyObject *
174 grp_getgrnam(PyModuleDef *module, PyObject *args, PyObject *kwargs)
108 { 175 {
109 char *name; 176 PyObject *return_value = NULL;
177 static char *_keywords[] = {"name", NULL};
178 PyObject *name;
179
180 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
181 "U:getgrnam", _keywords,
182 &name))
183 goto exit;
184 return_value = grp_getgrnam_impl(module, name);
185
186 exit:
187 return return_value;
188 }
189
190 static PyObject *
191 grp_getgrnam_impl(PyModuleDef *module, PyObject *name)
192 /*[clinic end generated code: checksum=371e8d158be46aa5d095e6eb19bcc975124b622f] */
193 {
194 char *name_chars;
110 struct group *p; 195 struct group *p;
111 PyObject *arg, *bytes, *retval = NULL; 196 PyObject *bytes, *retval = NULL;
112 197
113 if (!PyArg_ParseTuple(args, "U:getgrnam", &arg)) 198 if ((bytes = PyUnicode_EncodeFSDefault(name)) == NULL)
114 return NULL; 199 return NULL;
115 if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL) 200 if (PyBytes_AsStringAndSize(bytes, &name_chars, NULL) == -1)
116 return NULL;
117 if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
118 goto out; 201 goto out;
119 202
120 if ((p = getgrnam(name)) == NULL) { 203 if ((p = getgrnam(name_chars)) == NULL) {
121 PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %s", name); 204 PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %s", name_char s);
122 goto out; 205 goto out;
123 } 206 }
124 retval = mkgrent(p); 207 retval = mkgrent(p);
125 out: 208 out:
126 Py_DECREF(bytes); 209 Py_DECREF(bytes);
127 return retval; 210 return retval;
128 } 211 }
129 212
213 /*[clinic input]
214 grp.getgrall
215
216 Return a list of all available group entries, in arbitrary order.
217
218 An entry whose name starts with '+' or '-' represents an instruction
219 to use YP/NIS and may not be accessible via getgrnam or getgrgid.
220 [clinic start generated code]*/
221
222 PyDoc_STRVAR(grp_getgrall__doc__,
223 "getgrall()\n"
224 "Return a list of all available group entries, in arbitrary order.\n"
225 "\n"
226 "An entry whose name starts with \'+\' or \'-\' represents an instruction\n"
227 "to use YP/NIS and may not be accessible via getgrnam or getgrgid.");
228
229 #define GRP_GETGRALL_METHODDEF \
230 {"getgrall", (PyCFunction)grp_getgrall, METH_NOARGS, grp_getgrall__doc__},
231
130 static PyObject * 232 static PyObject *
131 grp_getgrall(PyObject *self, PyObject *ignore) 233 grp_getgrall_impl(PyModuleDef *module);
234
235 static PyObject *
236 grp_getgrall(PyModuleDef *module, PyObject *Py_UNUSED(ignored))
237 {
238 PyObject *return_value = NULL;
239
240 return_value = grp_getgrall_impl(module);
241
242 return return_value;
243 }
244
245 static PyObject *
246 grp_getgrall_impl(PyModuleDef *module)
247 /*[clinic end generated code: checksum=d525b6b8bd930c1aa07d21f7632b0f644cf19d2f] */
132 { 248 {
133 PyObject *d; 249 PyObject *d;
134 struct group *p; 250 struct group *p;
135 251
136 if ((d = PyList_New(0)) == NULL) 252 if ((d = PyList_New(0)) == NULL)
137 return NULL; 253 return NULL;
138 setgrent(); 254 setgrent();
139 while ((p = getgrent()) != NULL) { 255 while ((p = getgrent()) != NULL) {
140 PyObject *v = mkgrent(p); 256 PyObject *v = mkgrent(p);
141 if (v == NULL || PyList_Append(d, v) != 0) { 257 if (v == NULL || PyList_Append(d, v) != 0) {
142 Py_XDECREF(v); 258 Py_XDECREF(v);
143 Py_DECREF(d); 259 Py_DECREF(d);
144 endgrent(); 260 endgrent();
145 return NULL; 261 return NULL;
146 } 262 }
147 Py_DECREF(v); 263 Py_DECREF(v);
148 } 264 }
149 endgrent(); 265 endgrent();
150 return d; 266 return d;
151 } 267 }
152 268
153 static PyMethodDef grp_methods[] = { 269 static PyMethodDef grp_methods[] = {
154 {"getgrgid", grp_getgrgid, METH_O, 270 GRP_GETGRGID_METHODDEF
155 "getgrgid(id) -> tuple\n\ 271 GRP_GETGRNAM_METHODDEF
156 Return the group database entry for the given numeric group ID. If\n\ 272 GRP_GETGRALL_METHODDEF
157 id is not valid, raise KeyError."}, 273 {NULL, NULL}
158 {"getgrnam", grp_getgrnam, METH_VARARGS,
159 "getgrnam(name) -> tuple\n\
160 Return the group database entry for the given group name. If\n\
161 name is not valid, raise KeyError."},
162 {"getgrall", grp_getgrall, METH_NOARGS,
163 "getgrall() -> list of tuples\n\
164 Return a list of all available group entries, in arbitrary order.\n\
165 An entry whose name starts with '+' or '-' represents an instruction\n\
166 to use YP/NIS and may not be accessible via getgrnam or getgrgid."},
167 {NULL, NULL} /* sentinel */
168 }; 274 };
169 275
170 PyDoc_STRVAR(grp__doc__, 276 PyDoc_STRVAR(grp__doc__,
171 "Access to the Unix group database.\n\ 277 "Access to the Unix group database.\n\
172 \n\ 278 \n\
173 Group entries are reported as 4-tuples containing the following fields\n\ 279 Group entries are reported as 4-tuples containing the following fields\n\
174 from the group database, in order:\n\ 280 from the group database, in order:\n\
175 \n\ 281 \n\
176 name - name of the group\n\ 282 name - name of the group\n\
177 passwd - group password (encrypted); often empty\n\ 283 passwd - group password (encrypted); often empty\n\
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 if (PyStructSequence_InitType2(&StructGrpType, 315 if (PyStructSequence_InitType2(&StructGrpType,
210 &struct_group_type_desc) < 0) 316 &struct_group_type_desc) < 0)
211 return NULL; 317 return NULL;
212 } 318 }
213 if (PyDict_SetItemString(d, "struct_group", 319 if (PyDict_SetItemString(d, "struct_group",
214 (PyObject *)&StructGrpType) < 0) 320 (PyObject *)&StructGrpType) < 0)
215 return NULL; 321 return NULL;
216 initialized = 1; 322 initialized = 1;
217 return m; 323 return m;
218 } 324 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+