<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<link rel=
"STYLESHEET" href=
"ext.css" type='text/css'
/>
<link rel=
"SHORTCUT ICON" href=
"../icons/pyfav.png" type=
"image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index'
/>
<link rel=
"first" href=
"ext.html" title='Extending and Embedding the Python Interpreter'
/>
<link rel='contents' href='contents.html'
title=
"Contents" />
<link rel='last' href='about.html' title='About this document...'
/>
<link rel='help' href='about.html' title='About this document...'
/>
<link rel=
"next" href=
"parseTupleAndKeywords.html" />
<link rel=
"prev" href=
"callingPython.html" />
<link rel=
"parent" href=
"intro.html" />
<link rel=
"next" href=
"parseTupleAndKeywords.html" />
<meta name='aesop' content='information'
/>
<title>1.7 Extracting Parameters in Extension Functions
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"1.6 Calling Python Functions"
href=
"callingPython.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"1. Extending Python with"
href=
"intro.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"1.8 Keyword Parameters for"
href=
"parseTupleAndKeywords.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Extending and Embedding the Python Interpreter
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><img src='../icons/blank.png'
border='
0' height='
32' alt='' width='
32'
/></td>
<td class='online-navigation'
><img src='../icons/blank.png'
border='
0' height='
32' alt='' width='
32'
/></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"callingPython.html">1.6 Calling Python Functions
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"intro.html">1. Extending Python with
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"parseTupleAndKeywords.html">1.8 Keyword Parameters for
</A>
<!--End of Navigation Panel-->
<H1><A NAME=
"SECTION003700000000000000000"></A><A NAME=
"parseTuple"></A>
1.7 Extracting Parameters in Extension Functions
<a id='l2h-
2' xml:id='l2h-
2'
></a>
The
<tt class=
"cfunction">PyArg_ParseTuple()
</tt> function is declared as follows:
<div class=
"verbatim"><pre>
int PyArg_ParseTuple(PyObject *arg, char *format, ...);
The
<var>arg
</var> argument must be a tuple object containing an argument
list passed from Python to a C function. The
<var>format
</var> argument
must be a format string, whose syntax is explained in
``
<a class=
"ulink" href=
"../api/arg-parsing.html"
>Parsing arguments and building
title=
"Python/C API Reference Manual"
>Python/C API Reference Manual
</a></em>. The
remaining arguments must be addresses of variables whose type is
determined by the format string.
Note that while
<tt class=
"cfunction">PyArg_ParseTuple()
</tt> checks that the Python
arguments have the required types, it cannot check the validity of the
addresses of C variables passed to the call: if you make mistakes
there, your code will probably crash or at least overwrite random bits
in memory. So be careful!
Note that any Python object references which are provided to the
caller are
<em>borrowed
</em> references; do not decrement their
<div class=
"verbatim"><pre>
ok = PyArg_ParseTuple(args,
""); /* No arguments */
<div class=
"verbatim"><pre>
ok = PyArg_ParseTuple(args,
"s",
&s); /* A string */
/* Possible Python call: f('whoops!') */
<div class=
"verbatim"><pre>
ok = PyArg_ParseTuple(args,
"lls",
&k,
&l,
&s); /* Two longs and a string */
/* Possible Python call: f(
1,
2, 'three') */
<div class=
"verbatim"><pre>
ok = PyArg_ParseTuple(args,
"(ii)s#",
&i,
&j,
&s,
&size);
/* A pair of ints and a string, whose size is also returned */
/* Possible Python call: f((
1,
2), 'three') */
<div class=
"verbatim"><pre>
ok = PyArg_ParseTuple(args,
"s|si",
&file,
&mode,
&bufsize);
/* A string, and optionally another string and an integer */
/* Possible Python calls:
f('spam', 'wb',
100000) */
<div class=
"verbatim"><pre>
int left, top, right, bottom, h, v;
ok = PyArg_ParseTuple(args,
"((ii)(ii))(ii)",
&left,
&top,
&right,
&bottom,
&h,
&v);
/* A rectangle and a point */
f(((
0,
0), (
400,
300)), (
10,
10)) */
<div class=
"verbatim"><pre>
ok = PyArg_ParseTuple(args,
"D:myfunction",
&c);
/* a complex, also providing a function name for errors */
/* Possible Python call: myfunction(
1+
2j) */
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"1.6 Calling Python Functions"
href=
"callingPython.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"1. Extending Python with"
href=
"intro.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"1.8 Keyword Parameters for"
href=
"parseTupleAndKeywords.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Extending and Embedding the Python Interpreter
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><img src='../icons/blank.png'
border='
0' height='
32' alt='' width='
32'
/></td>
<td class='online-navigation'
><img src='../icons/blank.png'
border='
0' height='
32' alt='' width='
32'
/></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"callingPython.html">1.6 Calling Python Functions
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"intro.html">1. Extending Python with
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"parseTupleAndKeywords.html">1.8 Keyword Parameters for
</A>
<span class=
"release-info">Release
2.4.2, documentation updated on
28 September
2005.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.