Skip to content

Commit cb2da43

Browse files
committed
Extended tuple's C API to include a new function, PyTuple_Pack() that is
useful for rapidly building argument tuples without having to invoke the more sophisticated machinery of Py_BuildValue().
1 parent d662548 commit cb2da43

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

Doc/api/concrete.tex

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ \subsubsection{Built-in Codecs \label{builtinCodecs}}
973973
\cdata{Py_FileSystemDefaultEncoding} as the encoding for file
974974
names. This variable should be treated as read-only: On some systems,
975975
it will be a pointer to a static string, on others, it will change at
976-
run-time, e.g. when the application invokes setlocale.
976+
run-time (such as when the application invokes setlocale).
977977

978978
Error handling is set by errors which may also be set to \NULL{}
979979
meaning to use the default handling defined for the codec. Default
@@ -1584,6 +1584,14 @@ \subsection{Tuple Objects \label{tupleObjects}}
15841584
Return a new tuple object of size \var{len}, or \NULL{} on failure.
15851585
\end{cfuncdesc}
15861586

1587+
\begin{cfuncdesc}{PyObject*}{PyTuple_Pack}{int n, \moreargs}
1588+
Return a new tuple object of size \var{n}, or \NULL{} on failure.
1589+
The tuple values are initialized to the subsequent \var{n} C arguments
1590+
pointing to Python objects. \samp{PyTuple_Pack(2, \var{a}, \var{b})}
1591+
is equivalent to \samp{Py_BuildValue("(OO)", \var{a}, \var{b})}.
1592+
\versionadded{2.4}
1593+
\end{cfuncdesc}
1594+
15871595
\begin{cfuncdesc}{int}{PyTuple_Size}{PyObject *p}
15881596
Takes a pointer to a tuple object, and returns the size of that
15891597
tuple.

Include/tupleobject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ PyAPI_FUNC(PyObject *) PyTuple_GetItem(PyObject *, int);
3535
PyAPI_FUNC(int) PyTuple_SetItem(PyObject *, int, PyObject *);
3636
PyAPI_FUNC(PyObject *) PyTuple_GetSlice(PyObject *, int, int);
3737
PyAPI_FUNC(int) _PyTuple_Resize(PyObject **, int);
38+
PyAPI_FUNC(PyObject *) PyTuple_Pack(int, ...);
3839

3940
/* Macro, trading safety for speed */
4041
#define PyTuple_GET_ITEM(op, i) (((PyTupleObject *)(op))->ob_item[i])

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ Build
140140
C API
141141
-----
142142

143+
- Added a new function, PyTuple_Pack(n, ...) for constructing tuples from a
144+
variable length argument list of Python objects without having to invoke
145+
the more complex machinery of Py_BuildValue(). PyTuple_Pack(3, a, b, c)
146+
is equivalent to Py_BuildValue("(OOO)", a, b, c).
147+
143148
New platforms
144149
-------------
145150

Objects/tupleobject.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,28 @@ PyTuple_SetItem(register PyObject *op, register int i, PyObject *newitem)
130130
return 0;
131131
}
132132

133+
PyObject *
134+
PyTuple_Pack(int n, ...)
135+
{
136+
int i;
137+
PyObject *o;
138+
PyObject *result;
139+
va_list vargs;
140+
141+
va_start(vargs, n);
142+
result = PyTuple_New(n);
143+
if (result == NULL)
144+
return NULL;
145+
for (i = 0; i < n; i++) {
146+
o = va_arg(vargs, PyObject *);
147+
Py_INCREF(o);
148+
PyTuple_SET_ITEM(result, i, o);
149+
}
150+
va_end(vargs);
151+
return result;
152+
}
153+
154+
133155
/* Methods */
134156

135157
static void

0 commit comments

Comments
 (0)