Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dbd55b3

Browse files
committed
Patch #523268, #522027: return enhanced tuples.
1 parent 9986633 commit dbd55b3

3 files changed

Lines changed: 114 additions & 23 deletions

File tree

Doc/lib/libgrp.tex

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ \section{\module{grp} ---
99
This module provides access to the \UNIX{} group database.
1010
It is available on all \UNIX{} versions.
1111

12-
Group database entries are reported as 4-tuples containing the
13-
following items from the group database (see \code{<grp.h>}), in order:
12+
Group database entries are reported as a tuple-like object, whose
13+
attributes correspond to the members of the \code{group} structure
14+
(Attribute field below, see \code{<pwd.h>}):
1415

15-
\begin{tableiii}{r|l|l}{textrm}{Index}{Field}{Meaning}
16+
\begin{tableiii}{r|l|l}{textrm}{Index}{Attribute}{Meaning}
1617
\lineiii{0}{gr_name}{the name of the group}
1718
\lineiii{1}{gr_passwd}{the (encrypted) group password; often empty}
1819
\lineiii{2}{gr_gid}{the numerical group ID}

Modules/grpmodule.c

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,73 @@
22
/* UNIX group file access module */
33

44
#include "Python.h"
5+
#include "structseq.h"
56

67
#include <sys/types.h>
78
#include <grp.h>
89

10+
static PyStructSequence_Field struct_group_type_fields[] = {
11+
{"gr_name", "group name"},
12+
{"gr_passwd", "password"},
13+
{"gr_gid", "group id"},
14+
{"gr_mem", "group memebers"},
15+
{0}
16+
};
17+
18+
static char struct_group__doc__[] =
19+
"grp.struct_group: Results from getgr*() routines.\n\n\
20+
This object may be accessed either as a tuple of\n\
21+
(gr_name,gr_passwd,gr_gid,gr_mem)\n\
22+
or via the object attributes as named in the above tuple.\n";
23+
24+
static PyStructSequence_Desc struct_group_type_desc = {
25+
"grp.struct_group",
26+
struct_group__doc__,
27+
struct_group_type_fields,
28+
4,
29+
};
30+
31+
32+
static PyTypeObject StructGrpType;
933

1034
static PyObject *
1135
mkgrent(struct group *p)
1236
{
13-
PyObject *v, *w;
37+
int setIndex = 0;
38+
PyObject *v = PyStructSequence_New(&StructGrpType), *w;
1439
char **member;
40+
41+
if (v == NULL)
42+
return NULL;
43+
1544
if ((w = PyList_New(0)) == NULL) {
45+
Py_DECREF(v);
1646
return NULL;
1747
}
1848
for (member = p->gr_mem; *member != NULL; member++) {
1949
PyObject *x = PyString_FromString(*member);
2050
if (x == NULL || PyList_Append(w, x) != 0) {
2151
Py_XDECREF(x);
2252
Py_DECREF(w);
53+
Py_DECREF(v);
2354
return NULL;
2455
}
2556
Py_DECREF(x);
2657
}
27-
v = Py_BuildValue("(sslO)",
28-
p->gr_name,
29-
p->gr_passwd,
30-
(long)p->gr_gid,
31-
w);
32-
Py_DECREF(w);
58+
59+
#define SET(i,val) PyStructSequence_SET_ITEM(v, i, val)
60+
SET(setIndex++, PyString_FromString(p->gr_name));
61+
SET(setIndex++, PyString_FromString(p->gr_passwd));
62+
SET(setIndex++, PyInt_FromLong((long) p->gr_gid));
63+
SET(setIndex++, w);
64+
#undef SET
65+
66+
if (PyErr_Occurred()) {
67+
Py_DECREF(v);
68+
Py_DECREF(w);
69+
return NULL;
70+
}
71+
3372
return v;
3473
}
3574

@@ -120,5 +159,9 @@ complete membership information.)";
120159
DL_EXPORT(void)
121160
initgrp(void)
122161
{
123-
Py_InitModule3("grp", grp_methods, grp__doc__);
162+
PyObject *m, *d;
163+
m = Py_InitModule3("grp", grp_methods, grp__doc__);
164+
d = PyModule_GetDict(m);
165+
PyStructSequence_InitType(&StructGrpType, &struct_group_type_desc);
166+
PyDict_SetItemString(d, "struct_group", (PyObject *) &StructGrpType);
124167
}

Modules/pwdmodule.c

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,35 @@
22
/* UNIX password file access module */
33

44
#include "Python.h"
5+
#include "structseq.h"
56

67
#include <sys/types.h>
78
#include <pwd.h>
89

10+
static PyStructSequence_Field struct_pwd_type_fields[] = {
11+
{"pw_name", "user name"},
12+
{"pw_passwd", "password"},
13+
{"pw_uid", "user id"},
14+
{"pw_gid", "group id"},
15+
{"pw_gecos", "real name"},
16+
{"pw_dir", "home directory"},
17+
{"pw_shell", "shell program"},
18+
{0}
19+
};
20+
21+
static char struct_passwd__doc__[] =
22+
"pwd.struct_passwd: Results from getpw*() routines.\n\n\
23+
This object may be accessed either as a tuple of\n\
24+
(pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)\n\
25+
or via the object attributes as named in the above tuple.\n";
26+
27+
static PyStructSequence_Desc struct_pwd_type_desc = {
28+
"pwd.struct_passwd",
29+
struct_passwd__doc__,
30+
struct_pwd_type_fields,
31+
7,
32+
};
33+
934
static char pwd__doc__ [] = "\
1035
This module provides access to the Unix password database.\n\
1136
It is available on all Unix versions.\n\
@@ -17,22 +42,40 @@ The uid and gid items are integers, all others are strings. An\n\
1742
exception is raised if the entry asked for cannot be found.";
1843

1944

45+
static PyTypeObject StructPwdType;
46+
2047
static PyObject *
2148
mkpwent(struct passwd *p)
2249
{
23-
return Py_BuildValue(
24-
"(ssllsss)",
25-
p->pw_name,
26-
p->pw_passwd,
27-
(long)p->pw_uid,
28-
(long)p->pw_gid,
29-
p->pw_gecos,
30-
p->pw_dir,
31-
p->pw_shell);
50+
int setIndex = 0;
51+
PyObject *v = PyStructSequence_New(&StructPwdType);
52+
if (v == NULL)
53+
return NULL;
54+
55+
#define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
56+
#define SETS(i,val) PyStructSequence_SET_ITEM(v, i, PyString_FromString(val))
57+
58+
SETS(setIndex++, p->pw_name);
59+
SETS(setIndex++, p->pw_passwd);
60+
SETI(setIndex++, p->pw_uid);
61+
SETI(setIndex++, p->pw_gid);
62+
SETS(setIndex++, p->pw_gecos);
63+
SETS(setIndex++, p->pw_dir);
64+
SETS(setIndex++, p->pw_shell);
65+
66+
#undef SETS
67+
#undef SETI
68+
69+
if (PyErr_Occurred()) {
70+
Py_XDECREF(v);
71+
return NULL;
72+
}
73+
74+
return v;
3275
}
3376

3477
static char pwd_getpwuid__doc__[] = "\
35-
getpwuid(uid) -> entry\n\
78+
getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)\n\
3679
Return the password database entry for the given numeric user ID.\n\
3780
See pwd.__doc__ for more on password database entries.";
3881

@@ -51,7 +94,7 @@ pwd_getpwuid(PyObject *self, PyObject *args)
5194
}
5295

5396
static char pwd_getpwnam__doc__[] = "\
54-
getpwnam(name) -> entry\n\
97+
getpwnam(name) -> (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)\n\
5598
Return the password database entry for the given user name.\n\
5699
See pwd.__doc__ for more on password database entries.";
57100

@@ -112,6 +155,10 @@ static PyMethodDef pwd_methods[] = {
112155
DL_EXPORT(void)
113156
initpwd(void)
114157
{
115-
Py_InitModule4("pwd", pwd_methods, pwd__doc__,
158+
PyObject *m, *d;
159+
m = Py_InitModule4("pwd", pwd_methods, pwd__doc__,
116160
(PyObject *)NULL, PYTHON_API_VERSION);
161+
d = PyModule_GetDict(m);
162+
PyStructSequence_InitType(&StructPwdType, &struct_pwd_type_desc);
163+
PyDict_SetItemString(d, "struct_pwent", (PyObject *) &StructPwdType);
117164
}

0 commit comments

Comments
 (0)