Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / ext / backToExample.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="methodTable.html" />
12<link rel="prev" href="errors.html" />
13<link rel="parent" href="intro.html" />
14<link rel="next" href="methodTable.html" />
15<meta name='aesop' content='information' />
16<title>1.3 Back to the Example
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.2 Intermezzo: Errors and"
25 href="errors.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.4 The Module's Method"
31 href="methodTable.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="errors.html">1.2 Intermezzo: Errors and</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="methodTable.html">1.4 The Module's Method</A>
49</div>
50<hr /></div>
51</DIV>
52<!--End of Navigation Panel-->
53
54<H1><A NAME="SECTION003300000000000000000"></A><A NAME="backToExample"></A>
55<BR>
561.3 Back to the Example
57
58</H1>
59
60<P>
61Going back to our example function, you should now be able to
62understand this statement:
63
64<P>
65<div class="verbatim"><pre>
66 if (!PyArg_ParseTuple(args, "s", &amp;command))
67 return NULL;
68</pre></div>
69
70<P>
71It returns <tt class="constant">NULL</tt> (the error indicator for functions returning
72object pointers) if an error is detected in the argument list, relying
73on the exception set by <tt class="cfunction">PyArg_ParseTuple()</tt>. Otherwise the
74string value of the argument has been copied to the local variable
75<tt class="cdata">command</tt>. This is a pointer assignment and you are not supposed
76to modify the string to which it points (so in Standard C, the variable
77<tt class="cdata">command</tt> should properly be declared as "<tt class="samp">const char
78*command</tt>").
79
80<P>
81The next statement is a call to the <span class="Unix">Unix</span> function
82<tt class="cfunction">system()</tt>, passing it the string we just got from
83<tt class="cfunction">PyArg_ParseTuple()</tt>:
84
85<P>
86<div class="verbatim"><pre>
87 sts = system(command);
88</pre></div>
89
90<P>
91Our <tt class="function">spam.system()</tt> function must return the value of
92<tt class="cdata">sts</tt> as a Python object. This is done using the function
93<tt class="cfunction">Py_BuildValue()</tt>, which is something like the inverse of
94<tt class="cfunction">PyArg_ParseTuple()</tt>: it takes a format string and an
95arbitrary number of C values, and returns a new Python object.
96More info on <tt class="cfunction">Py_BuildValue()</tt> is given later.
97
98<P>
99<div class="verbatim"><pre>
100 return Py_BuildValue("i", sts);
101</pre></div>
102
103<P>
104In this case, it will return an integer object. (Yes, even integers
105are objects on the heap in Python!)
106
107<P>
108If you have a C function that returns no useful argument (a function
109returning <tt class="ctype">void</tt>), the corresponding Python function must return
110<code>None</code>. You need this idiom to do so (which is implemented by the
111Py_RETURN_NONE macro):
112
113<P>
114<div class="verbatim"><pre>
115 Py_INCREF(Py_None);
116 return Py_None;
117</pre></div>
118
119<P>
120<tt class="cdata">Py_None</tt> is the C name for the special Python object
121<code>None</code>. It is a genuine Python object rather than a <tt class="constant">NULL</tt>
122pointer, which means ``error'' in most contexts, as we have seen.
123
124<P>
125
126<DIV CLASS="navigation">
127<div class='online-navigation'>
128<p></p><hr />
129<table align="center" width="100%" cellpadding="0" cellspacing="2">
130<tr>
131<td class='online-navigation'><a rel="prev" title="1.2 Intermezzo: Errors and"
132 href="errors.html"><img src='../icons/previous.png'
133 border='0' height='32' alt='Previous Page' width='32' /></A></td>
134<td class='online-navigation'><a rel="parent" title="1. Extending Python with"
135 href="intro.html"><img src='../icons/up.png'
136 border='0' height='32' alt='Up One Level' width='32' /></A></td>
137<td class='online-navigation'><a rel="next" title="1.4 The Module's Method"
138 href="methodTable.html"><img src='../icons/next.png'
139 border='0' height='32' alt='Next Page' width='32' /></A></td>
140<td align="center" width="100%">Extending and Embedding the Python Interpreter</td>
141<td class='online-navigation'><a rel="contents" title="Table of Contents"
142 href="contents.html"><img src='../icons/contents.png'
143 border='0' height='32' alt='Contents' width='32' /></A></td>
144<td class='online-navigation'><img src='../icons/blank.png'
145 border='0' height='32' alt='' width='32' /></td>
146<td class='online-navigation'><img src='../icons/blank.png'
147 border='0' height='32' alt='' width='32' /></td>
148</tr></table>
149<div class='online-navigation'>
150<b class="navlabel">Previous:</b>
151<a class="sectref" rel="prev" href="errors.html">1.2 Intermezzo: Errors and</A>
152<b class="navlabel">Up:</b>
153<a class="sectref" rel="parent" href="intro.html">1. Extending Python with</A>
154<b class="navlabel">Next:</b>
155<a class="sectref" rel="next" href="methodTable.html">1.4 The Module's Method</A>
156</div>
157</div>
158<hr />
159<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
160</DIV>
161<!--End of Navigation Panel-->
162<ADDRESS>
163See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
164</ADDRESS>
165</BODY>
166</HTML>