Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / ext / parseTuple.html
CommitLineData
920dae64
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="ext.css" type='text/css' />
5<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
6<link rel='start' href='../index.html' title='Python Documentation Index' />
7<link rel="first" href="ext.html" title='Extending and Embedding the Python Interpreter' />
8<link rel='contents' href='contents.html' title="Contents" />
9<link rel='last' href='about.html' title='About this document...' />
10<link rel='help' href='about.html' title='About this document...' />
11<link rel="next" href="parseTupleAndKeywords.html" />
12<link rel="prev" href="callingPython.html" />
13<link rel="parent" href="intro.html" />
14<link rel="next" href="parseTupleAndKeywords.html" />
15<meta name='aesop' content='information' />
16<title>1.7 Extracting Parameters in Extension Functions
17</title>
18</head>
19<body>
20<DIV CLASS="navigation">
21<div id='top-navigation-panel' xml:id='top-navigation-panel'>
22<table align="center" width="100%" cellpadding="0" cellspacing="2">
23<tr>
24<td class='online-navigation'><a rel="prev" title="1.6 Calling Python Functions"
25 href="callingPython.html"><img src='../icons/previous.png'
26 border='0' height='32' alt='Previous Page' width='32' /></A></td>
27<td class='online-navigation'><a rel="parent" title="1. Extending Python with"
28 href="intro.html"><img src='../icons/up.png'
29 border='0' height='32' alt='Up One Level' width='32' /></A></td>
30<td class='online-navigation'><a rel="next" title="1.8 Keyword Parameters for"
31 href="parseTupleAndKeywords.html"><img src='../icons/next.png'
32 border='0' height='32' alt='Next Page' width='32' /></A></td>
33<td align="center" width="100%">Extending and Embedding the Python Interpreter</td>
34<td class='online-navigation'><a rel="contents" title="Table of Contents"
35 href="contents.html"><img src='../icons/contents.png'
36 border='0' height='32' alt='Contents' width='32' /></A></td>
37<td class='online-navigation'><img src='../icons/blank.png'
38 border='0' height='32' alt='' width='32' /></td>
39<td class='online-navigation'><img src='../icons/blank.png'
40 border='0' height='32' alt='' width='32' /></td>
41</tr></table>
42<div class='online-navigation'>
43<b class="navlabel">Previous:</b>
44<a class="sectref" rel="prev" href="callingPython.html">1.6 Calling Python Functions</A>
45<b class="navlabel">Up:</b>
46<a class="sectref" rel="parent" href="intro.html">1. Extending Python with</A>
47<b class="navlabel">Next:</b>
48<a class="sectref" rel="next" href="parseTupleAndKeywords.html">1.8 Keyword Parameters for</A>
49</div>
50<hr /></div>
51</DIV>
52<!--End of Navigation Panel-->
53
54<H1><A NAME="SECTION003700000000000000000"></A><A NAME="parseTuple"></A>
55<BR>
561.7 Extracting Parameters in Extension Functions
57
58</H1>
59
60<P>
61<a id='l2h-2' xml:id='l2h-2'></a>
62
63<P>
64The <tt class="cfunction">PyArg_ParseTuple()</tt> function is declared as follows:
65
66<P>
67<div class="verbatim"><pre>
68int PyArg_ParseTuple(PyObject *arg, char *format, ...);
69</pre></div>
70
71<P>
72The <var>arg</var> argument must be a tuple object containing an argument
73list passed from Python to a C function. The <var>format</var> argument
74must be a format string, whose syntax is explained in
75``<a class="ulink" href="../api/arg-parsing.html"
76 >Parsing arguments and building
77values</a>'' in the
78<em class="citetitle"><a
79 href="../api/api.html"
80 title="Python/C API Reference Manual"
81 >Python/C API Reference Manual</a></em>. The
82remaining arguments must be addresses of variables whose type is
83determined by the format string.
84
85<P>
86Note that while <tt class="cfunction">PyArg_ParseTuple()</tt> checks that the Python
87arguments have the required types, it cannot check the validity of the
88addresses of C variables passed to the call: if you make mistakes
89there, your code will probably crash or at least overwrite random bits
90in memory. So be careful!
91
92<P>
93Note that any Python object references which are provided to the
94caller are <em>borrowed</em> references; do not decrement their
95reference count!
96
97<P>
98Some example calls:
99
100<P>
101<div class="verbatim"><pre>
102 int ok;
103 int i, j;
104 long k, l;
105 const char *s;
106 int size;
107
108 ok = PyArg_ParseTuple(args, ""); /* No arguments */
109 /* Python call: f() */
110</pre></div>
111
112<P>
113<div class="verbatim"><pre>
114 ok = PyArg_ParseTuple(args, "s", &amp;s); /* A string */
115 /* Possible Python call: f('whoops!') */
116</pre></div>
117
118<P>
119<div class="verbatim"><pre>
120 ok = PyArg_ParseTuple(args, "lls", &amp;k, &amp;l, &amp;s); /* Two longs and a string */
121 /* Possible Python call: f(1, 2, 'three') */
122</pre></div>
123
124<P>
125<div class="verbatim"><pre>
126 ok = PyArg_ParseTuple(args, "(ii)s#", &amp;i, &amp;j, &amp;s, &amp;size);
127 /* A pair of ints and a string, whose size is also returned */
128 /* Possible Python call: f((1, 2), 'three') */
129</pre></div>
130
131<P>
132<div class="verbatim"><pre>
133 {
134 const char *file;
135 const char *mode = "r";
136 int bufsize = 0;
137 ok = PyArg_ParseTuple(args, "s|si", &amp;file, &amp;mode, &amp;bufsize);
138 /* A string, and optionally another string and an integer */
139 /* Possible Python calls:
140 f('spam')
141 f('spam', 'w')
142 f('spam', 'wb', 100000) */
143 }
144</pre></div>
145
146<P>
147<div class="verbatim"><pre>
148 {
149 int left, top, right, bottom, h, v;
150 ok = PyArg_ParseTuple(args, "((ii)(ii))(ii)",
151 &amp;left, &amp;top, &amp;right, &amp;bottom, &amp;h, &amp;v);
152 /* A rectangle and a point */
153 /* Possible Python call:
154 f(((0, 0), (400, 300)), (10, 10)) */
155 }
156</pre></div>
157
158<P>
159<div class="verbatim"><pre>
160 {
161 Py_complex c;
162 ok = PyArg_ParseTuple(args, "D:myfunction", &amp;c);
163 /* a complex, also providing a function name for errors */
164 /* Possible Python call: myfunction(1+2j) */
165 }
166</pre></div>
167
168<P>
169
170<DIV CLASS="navigation">
171<div class='online-navigation'>
172<p></p><hr />
173<table align="center" width="100%" cellpadding="0" cellspacing="2">
174<tr>
175<td class='online-navigation'><a rel="prev" title="1.6 Calling Python Functions"
176 href="callingPython.html"><img src='../icons/previous.png'
177 border='0' height='32' alt='Previous Page' width='32' /></A></td>
178<td class='online-navigation'><a rel="parent" title="1. Extending Python with"
179 href="intro.html"><img src='../icons/up.png'
180 border='0' height='32' alt='Up One Level' width='32' /></A></td>
181<td class='online-navigation'><a rel="next" title="1.8 Keyword Parameters for"
182 href="parseTupleAndKeywords.html"><img src='../icons/next.png'
183 border='0' height='32' alt='Next Page' width='32' /></A></td>
184<td align="center" width="100%">Extending and Embedding the Python Interpreter</td>
185<td class='online-navigation'><a rel="contents" title="Table of Contents"
186 href="contents.html"><img src='../icons/contents.png'
187 border='0' height='32' alt='Contents' width='32' /></A></td>
188<td class='online-navigation'><img src='../icons/blank.png'
189 border='0' height='32' alt='' width='32' /></td>
190<td class='online-navigation'><img src='../icons/blank.png'
191 border='0' height='32' alt='' width='32' /></td>
192</tr></table>
193<div class='online-navigation'>
194<b class="navlabel">Previous:</b>
195<a class="sectref" rel="prev" href="callingPython.html">1.6 Calling Python Functions</A>
196<b class="navlabel">Up:</b>
197<a class="sectref" rel="parent" href="intro.html">1. Extending Python with</A>
198<b class="navlabel">Next:</b>
199<a class="sectref" rel="next" href="parseTupleAndKeywords.html">1.8 Keyword Parameters for</A>
200</div>
201</div>
202<hr />
203<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
204</DIV>
205<!--End of Navigation Panel-->
206<ADDRESS>
207See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
208</ADDRESS>
209</BODY>
210</HTML>