[pypy-commit] pypy default: move grp.py's logic inside _pwdgrp_build.py too

arigo noreply at buildbot.pypy.org
Tue May 19 22:54:36 CEST 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r77415:b96f6cabe5bd
Date: 2015-05-19 22:54 +0200
http://bitbucket.org/pypy/pypy/changeset/b96f6cabe5bd/

Log:	move grp.py's logic inside _pwdgrp_build.py too

diff --git a/lib_pypy/_pwd_build.py b/lib_pypy/_pwdgrp_build.py
rename from lib_pypy/_pwd_build.py
rename to lib_pypy/_pwdgrp_build.py
--- a/lib_pypy/_pwd_build.py
+++ b/lib_pypy/_pwdgrp_build.py
@@ -2,9 +2,10 @@
 
 ffi = FFI()
 
-ffi.set_source("_pwd_cffi", """
+ffi.set_source("_pwdgrp_cffi", """
 #include <sys/types.h>
 #include <pwd.h>
+#include <grp.h>
 """)
 
 
@@ -24,6 +25,13 @@
     ...;
 };
 
+struct group {
+    char *gr_name;       /* group name */
+    char *gr_passwd;     /* group password */
+    gid_t gr_gid;        /* group ID */
+    char **gr_mem;        /* group members */
+};
+
 struct passwd *getpwuid(uid_t uid);
 struct passwd *getpwnam(const char *name);
 
@@ -31,6 +39,13 @@
 void setpwent(void);
 void endpwent(void);
 
+struct group *getgrgid(gid_t gid);
+struct group *getgrnam(const char *name);
+
+struct group *getgrent(void);
+void setgrent(void);
+void endgrent(void);
+
 """)
 
 
diff --git a/lib_pypy/grp.py b/lib_pypy/grp.py
--- a/lib_pypy/grp.py
+++ b/lib_pypy/grp.py
@@ -2,63 +2,38 @@
 """ This module provides ctypes version of cpython's grp module
 """
 
-import sys
-if sys.platform == 'win32':
-    raise ImportError("No grp module on Windows")
-
-from ctypes import Structure, c_char_p, c_int, POINTER
-from ctypes_support import standard_c_lib as libc
+from _pwdgrp_cffi import ffi, lib
 import _structseq
 
 try: from __pypy__ import builtinify
 except ImportError: builtinify = lambda f: f
 
 
-gid_t = c_int
-
-class GroupStruct(Structure):
-    _fields_ = (
-        ('gr_name', c_char_p),
-        ('gr_passwd', c_char_p),
-        ('gr_gid', gid_t),
-        ('gr_mem', POINTER(c_char_p)),
-        )
-
 class struct_group:
     __metaclass__ = _structseq.structseqtype
+    name = "grp.struct_group"
 
     gr_name   = _structseq.structseqfield(0)
     gr_passwd = _structseq.structseqfield(1)
     gr_gid    = _structseq.structseqfield(2)
     gr_mem    = _structseq.structseqfield(3)
 
-libc.getgrgid.argtypes = [gid_t]
-libc.getgrgid.restype = POINTER(GroupStruct)
-
-libc.getgrnam.argtypes = [c_char_p]
-libc.getgrnam.restype = POINTER(GroupStruct)
-
-libc.getgrent.argtypes = []
-libc.getgrent.restype = POINTER(GroupStruct)
-
-libc.setgrent.argtypes = []
-libc.setgrent.restype = None
-
-libc.endgrent.argtypes = []
-libc.endgrent.restype = None
 
 def _group_from_gstruct(res):
     i = 0
-    mem = []
-    while res.contents.gr_mem[i]:
-        mem.append(res.contents.gr_mem[i])
+    members = []
+    while res.gr_mem[i]:
+        members.append(ffi.string(res.gr_mem[i]))
         i += 1
-    return struct_group((res.contents.gr_name, res.contents.gr_passwd,
-                         res.contents.gr_gid, mem))
+    return struct_group([
+        ffi.string(res.gr_name),
+        ffi.string(res.gr_passwd),
+        res.gr_gid,
+        members])
 
 @builtinify
 def getgrgid(gid):
-    res = libc.getgrgid(gid)
+    res = lib.getgrgid(gid)
     if not res:
         # XXX maybe check error eventually
         raise KeyError(gid)
@@ -69,18 +44,32 @@
     if not isinstance(name, basestring):
         raise TypeError("expected string")
     name = str(name)
-    res = libc.getgrnam(name)
+    res = lib.getgrnam(name)
     if not res:
         raise KeyError("'getgrnam(): name not found: %s'" % name)
     return _group_from_gstruct(res)
 
 @builtinify
 def getgrall():
-    libc.setgrent()
+    lib.setgrent()
     lst = []
     while 1:
-        p = libc.getgrent()
+        p = lib.getgrent()
         if not p:
-            libc.endgrent()
-            return lst
+            break
         lst.append(_group_from_gstruct(p))
+    lib.endgrent()
+    return lst
+
+__all__ = ('struct_group', 'getgrgid', 'getgrnam', 'getgrall')
+
+if __name__ == "__main__":
+    from os import getgid
+    gid = getgid()
+    pw = getgrgid(gid)
+    print("gid %s: %s" % (pw.gr_gid, pw))
+    name = pw.gr_name
+    print("name %r: %s" % (name, getgrnam(name)))
+    print("All:")
+    for pw in getgrall():
+        print(pw)
diff --git a/lib_pypy/pwd.py b/lib_pypy/pwd.py
--- a/lib_pypy/pwd.py
+++ b/lib_pypy/pwd.py
@@ -10,8 +10,8 @@
 exception is raised if the entry asked for cannot be found.
 """
 
-from _pwd_cffi import ffi, lib
-from _structseq import structseqtype, structseqfield
+from _pwdgrp_cffi import ffi, lib
+import _structseq
 
 try: from __pypy__ import builtinify
 except ImportError: builtinify = lambda f: f
@@ -25,15 +25,16 @@
       (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)
     or via the object attributes as named in the above tuple.
     """
-    __metaclass__ = structseqtype
+    __metaclass__ = _structseq.structseqtype
     name = "pwd.struct_passwd"
-    pw_name = structseqfield(0)
-    pw_passwd = structseqfield(1)
-    pw_uid = structseqfield(2)
-    pw_gid = structseqfield(3)
-    pw_gecos = structseqfield(4)
-    pw_dir = structseqfield(5)
-    pw_shell = structseqfield(6)
+
+    pw_name = _structseq.structseqfield(0)
+    pw_passwd = _structseq.structseqfield(1)
+    pw_uid = _structseq.structseqfield(2)
+    pw_gid = _structseq.structseqfield(3)
+    pw_gecos = _structseq.structseqfield(4)
+    pw_dir = _structseq.structseqfield(5)
+    pw_shell = _structseq.structseqfield(6)
 
 
 def _mkpwent(pw):
@@ -67,8 +68,9 @@
     Return the password database entry for the given user name.
     See pwd.__doc__ for more on password database entries.
     """
-    if not isinstance(name, str):
+    if not isinstance(name, basestring):
         raise TypeError("expected string")
+    name = str(name)
     pw = lib.getpwnam(name)
     if not pw:
         raise KeyError("getpwname(): name not found: %s" % name)
diff --git a/pypy/tool/release/package.py b/pypy/tool/release/package.py
--- a/pypy/tool/release/package.py
+++ b/pypy/tool/release/package.py
@@ -56,7 +56,7 @@
     modules = ['_sqlite3_build.py', '_audioop_build.py']
     if not sys.platform == 'win32':
         modules += ['_curses_build.py', '_syslog_build.py', '_gdbm_build.py',
-                    '_pwd_build.py']
+                    '_pwdgrp_build.py']
     if not options.no_tk:
         modules.append('_tkinter/tklib_build.py')
     for module in modules:


More information about the pypy-commit mailing list