]> granicus.if.org Git - python/commitdiff
Merged revisions 76636 via svnmerge from
authorAntoine Pitrou <solipsis@pitrou.net>
Wed, 2 Dec 2009 20:46:48 +0000 (20:46 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Wed, 2 Dec 2009 20:46:48 +0000 (20:46 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r76636 | antoine.pitrou | 2009-12-02 21:37:54 +0100 (mer., 02 déc. 2009) | 5 lines

  Issue #7333: The `posix` module gains an `initgroups()` function providing
  access to the initgroups(3) C library call on Unix systems which implement
  it.  Patch by Jean-Paul Calderone.
........

Doc/library/os.rst
Lib/test/test_posix.py
Misc/NEWS
Modules/posixmodule.c
configure
configure.in
pyconfig.h.in

index 29e789972c671d30d4ffda96a458212dece05d50..7c96bfe23407b1aa7297efe46f1d0e03c207793b 100644 (file)
@@ -161,6 +161,15 @@ process and user.
    Availability: Unix.
 
 
+.. function:: initgroups(username, gid)
+
+   Call the system initgroups() to initialize the group access list with all of
+   the groups of which the specified username is a member, plus the specified
+   group id. Availability: Unix.
+
+   .. versionadded:: 3.2
+
+
 .. function:: getlogin()
 
    Return the name of the user logged in on the controlling terminal of the
index f3edf33fb612dacf91aeb62c8ff829237cdef31d..3a404833327a7fa127ad1d7df376ae45686b3270 100644 (file)
@@ -5,6 +5,7 @@ from test import support
 # Skip these tests if there is no posix module.
 posix = support.import_module('posix')
 
+import errno
 import time
 import os
 import pwd
@@ -82,6 +83,27 @@ class PosixTester(unittest.TestCase):
                 new_group_ids = (current_group_ids[0]+1, -1, -1)
                 self.assertRaises(OSError, posix.setresgid, *new_group_ids)
 
+    @unittest.skipUnless(hasattr(posix, 'initgroups'),
+                         "test needs os.initgroups()")
+    def test_initgroups(self):
+        # It takes a string and an integer; check that it raises a TypeError
+        # for other argument lists.
+        self.assertRaises(TypeError, posix.initgroups)
+        self.assertRaises(TypeError, posix.initgroups, None)
+        self.assertRaises(TypeError, posix.initgroups, 3, "foo")
+        self.assertRaises(TypeError, posix.initgroups, "foo", 3, object())
+
+        # If a non-privileged user invokes it, it should fail with OSError
+        # EPERM.
+        if os.getuid() != 0:
+            name = pwd.getpwuid(posix.getuid()).pw_name
+            try:
+                posix.initgroups(name, 13)
+            except OSError as e:
+                self.assertEquals(e.errno, errno.EPERM)
+            else:
+                self.fail("Expected OSError to be raised by initgroups")
+
     def test_statvfs(self):
         if hasattr(posix, 'statvfs'):
             self.assertTrue(posix.statvfs(os.curdir))
index 46a591224bfa99fc665b866643a33b7772003299..6a51600ad214f4496d7c231c5b30889b9a95de82 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -146,6 +146,10 @@ C-API
 Library
 -------
 
+- Issue #7333: The `posix` module gains an `initgroups()` function providing
+  access to the initgroups(3) C library call on Unix systems which implement
+  it.  Patch by Jean-Paul Calderone.
+
 - Issue #7408: Fixed distutils.tests.sdist so it doesn't check for group
   ownership when the group is not forced, because the group may be different
   from the user's group and inherit from its container when the test is run.
index eaf5b40306712b5917cb19d4f4b79c05beae5df8..3ed8d5f71f0c4886ab4f736fb81e5339affc230e 100644 (file)
@@ -3979,6 +3979,30 @@ posix_getgroups(PyObject *self, PyObject *noargs)
 }
 #endif
 
+#ifdef HAVE_INITGROUPS
+PyDoc_STRVAR(posix_initgroups__doc__,
+"initgroups(username, gid) -> None\n\n\
+Call the system initgroups() to initialize the group access list with all of\n\
+the groups of which the specified username is a member, plus the specified\n\
+group id.");
+
+static PyObject *
+posix_initgroups(PyObject *self, PyObject *args)
+{
+       char *username;
+       long gid;
+
+       if (!PyArg_ParseTuple(args, "sl:initgroups", &username, &gid))
+               return NULL;
+
+       if (initgroups(username, (gid_t) gid) == -1)
+               return PyErr_SetFromErrno(PyExc_OSError);
+
+       Py_INCREF(Py_None);
+       return Py_None;
+}
+#endif
+
 #ifdef HAVE_GETPGID
 PyDoc_STRVAR(posix_getpgid__doc__,
 "getpgid(pid) -> pgid\n\n\
@@ -7184,6 +7208,9 @@ static PyMethodDef posix_methods[] = {
 #ifdef HAVE_SETGROUPS
        {"setgroups",   posix_setgroups, METH_O, posix_setgroups__doc__},
 #endif /* HAVE_SETGROUPS */
+#ifdef HAVE_INITGROUPS
+       {"initgroups",  posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
+#endif /* HAVE_INITGROUPS */
 #ifdef HAVE_GETPGID
        {"getpgid",     posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
 #endif /* HAVE_GETPGID */
index a7dfabb120639ebaeb955b21038fa77b5480cbb0..50d07698b9f8fb1ba86c99cbd3c32ad59d0c3745 100755 (executable)
--- a/configure
+++ b/configure
@@ -1,5 +1,5 @@
 #! /bin/sh
-# From configure.in Revision: 76552 .
+# From configure.in Revision: 76566 .
 # Guess values for system-dependent variables and create Makefiles.
 # Generated by GNU Autoconf 2.61 for python 3.2.
 #
@@ -17377,13 +17377,14 @@ echo "${ECHO_T}MACHDEP_OBJS" >&6; }
 
 
 
+
 
 
 for ac_func in alarm setitimer getitimer bind_textdomain_codeset chown \
  clock confstr ctermid execv fchmod fchown fork fpathconf ftime ftruncate \
  gai_strerror getgroups getlogin getloadavg getpeername getpgid getpid \
  getpriority getresuid getresgid getpwent getspnam getspent getsid getwd \
- kill killpg lchmod lchown lstat mbrtowc mkfifo mknod mktime \
initgroups kill killpg lchmod lchown lstat mbrtowc mkfifo mknod mktime \
  mremap nice pathconf pause plock poll pthread_init \
  putenv readlink realpath \
  select sem_open sem_timedwait sem_getvalue sem_unlink setegid seteuid \
index f9840d4554621c86d65ad217cdedcf66e0c5b4ac..a7a596e6da8de23ad64c107436ca4542a370efb4 100644 (file)
@@ -2418,7 +2418,7 @@ AC_CHECK_FUNCS(alarm setitimer getitimer bind_textdomain_codeset chown \
  clock confstr ctermid execv fchmod fchown fork fpathconf ftime ftruncate \
  gai_strerror getgroups getlogin getloadavg getpeername getpgid getpid \
  getpriority getresuid getresgid getpwent getspnam getspent getsid getwd \
- kill killpg lchmod lchown lstat mbrtowc mkfifo mknod mktime \
initgroups kill killpg lchmod lchown lstat mbrtowc mkfifo mknod mktime \
  mremap nice pathconf pause plock poll pthread_init \
  putenv readlink realpath \
  select sem_open sem_timedwait sem_getvalue sem_unlink setegid seteuid \
index e9c930cd8aef8ea9676a1dc253c9a16ec7defb84..c9225a2d06102658020012ced50b76959f040d9b 100644 (file)
 /* Define to 1 if you have the `getpeername' function. */
 #undef HAVE_GETPEERNAME
 
+/* Define to 1 if you have the `initgroups' function. */
+#undef HAVE_INITGROUPS
+
 /* Define to 1 if you have the `getpgid' function. */
 #undef HAVE_GETPGID