]> granicus.if.org Git - python/commitdiff
Extended tuple's C API to include a new function, PyTuple_Pack() that is
authorRaymond Hettinger <python@rcn.com>
Sun, 12 Oct 2003 18:24:34 +0000 (18:24 +0000)
committerRaymond Hettinger <python@rcn.com>
Sun, 12 Oct 2003 18:24:34 +0000 (18:24 +0000)
useful for rapidly building argument tuples without having to invoke the
more sophisticated machinery of Py_BuildValue().

Doc/api/concrete.tex
Include/tupleobject.h
Misc/NEWS
Objects/tupleobject.c

index 5b0d9e38637665f77634b64a0a428cf90dea4f36..7b8409213e5ca5d7463317baf1102308ab3dbfa2 100644 (file)
@@ -973,7 +973,7 @@ which is \ASCII.  The file system calls should use
 \cdata{Py_FileSystemDefaultEncoding} as the encoding for file
 names. This variable should be treated as read-only: On some systems,
 it will be a pointer to a static string, on others, it will change at
-run-time, e.g. when the application invokes setlocale.
+run-time (such as when the application invokes setlocale).
 
 Error handling is set by errors which may also be set to \NULL{}
 meaning to use the default handling defined for the codec.  Default
@@ -1584,6 +1584,14 @@ format.
   Return a new tuple object of size \var{len}, or \NULL{} on failure.
 \end{cfuncdesc}
 
+\begin{cfuncdesc}{PyObject*}{PyTuple_Pack}{int n, \moreargs}
+  Return a new tuple object of size \var{n}, or \NULL{} on failure.
+  The tuple values are initialized to the subsequent \var{n} C arguments
+  pointing to Python objects.  \samp{PyTuple_Pack(2, \var{a}, \var{b})}
+  is equivalent to \samp{Py_BuildValue("(OO)", \var{a}, \var{b})}.
+  \versionadded{2.4}                    
+\end{cfuncdesc}
+
 \begin{cfuncdesc}{int}{PyTuple_Size}{PyObject *p}
   Takes a pointer to a tuple object, and returns the size of that
   tuple.
index 58bf8968e07e43a4a42b0f9fe5b6bf0454bb57c4..f1839fe98cda2d2098e06bd65caa5ee807415f7f 100644 (file)
@@ -35,6 +35,7 @@ PyAPI_FUNC(PyObject *) PyTuple_GetItem(PyObject *, int);
 PyAPI_FUNC(int) PyTuple_SetItem(PyObject *, int, PyObject *);
 PyAPI_FUNC(PyObject *) PyTuple_GetSlice(PyObject *, int, int);
 PyAPI_FUNC(int) _PyTuple_Resize(PyObject **, int);
+PyAPI_FUNC(PyObject *) PyTuple_Pack(int, ...);
 
 /* Macro, trading safety for speed */
 #define PyTuple_GET_ITEM(op, i) (((PyTupleObject *)(op))->ob_item[i])
index 0ee896d5533ced16604c19cf78c6d1958b4bed1b..eb57e68d23e74290585beacf2e621230b831f8ea 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -140,6 +140,11 @@ Build
 C API
 -----
 
+- Added a new function, PyTuple_Pack(n, ...) for constructing tuples from a
+  variable length argument list of Python objects without having to invoke
+  the more complex machinery of Py_BuildValue().  PyTuple_Pack(3, a, b, c)
+  is equivalent to Py_BuildValue("(OOO)", a, b, c).
+
 New platforms
 -------------
 
index 282da3e8b97b6a776fa746ec1c44ebf6f8c1a611..ef5cb8518b3ab105ffd56956dea913565ae6ae32 100644 (file)
@@ -130,6 +130,28 @@ PyTuple_SetItem(register PyObject *op, register int i, PyObject *newitem)
        return 0;
 }
 
+PyObject *
+PyTuple_Pack(int n, ...)
+{
+       int i;
+       PyObject *o;
+       PyObject *result;
+       va_list vargs;
+
+       va_start(vargs, n);
+       result = PyTuple_New(n);
+       if (result == NULL)
+               return NULL;
+       for (i = 0; i < n; i++) {
+               o = va_arg(vargs, PyObject *);
+               Py_INCREF(o);
+               PyTuple_SET_ITEM(result, i, o);
+       }
+       va_end(vargs);
+       return result;
+}
+
+
 /* Methods */
 
 static void