<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>1.3 Back to the Example
</TITLE>
<META NAME="description" CONTENT="1.3 Back to the Example
">
<META NAME="keywords" CONTENT="ext">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="ext.css">
<LINK REL="next" HREF="methodTable.html">
<LINK REL="previous" HREF="errors.html">
<LINK REL="up" HREF="intro.html">
<LINK REL="next" HREF="methodTable.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="methodTable.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="intro.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="errors.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Extending and Embedding the Python Interpreter</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="methodTable.html">1.4 The Module's Method</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="intro.html">1. Extending Python with</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="errors.html">1.2 Intermezzo: Errors and</A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION003300000000000000000"></A>
<BR>
1.3 Back to the Example
         
</H1>

<P>
Going back to our example function, you should now be able to
understand this statement:

<P>
<dl><dd><pre class=verbatim>
    if (!PyArg_ParseTuple(args, "s", &amp;command))
        return NULL;
</pre></dl>

<P>
It returns <tt>NULL</tt> (the error indicator for functions returning
object pointers) if an error is detected in the argument list, relying
on the exception set by <tt class=cfunction>PyArg_ParseTuple()</tt>.  Otherwise the
string value of the argument has been copied to the local variable
<tt class=cdata>command</tt>.  This is a pointer assignment and you are not supposed
to modify the string to which it points (so in Standard C, the variable
<tt class=cdata>command</tt> should properly be declared as "<tt class=samp>const char
*command</tt>").

<P>
The next statement is a call to the Unix function
<tt class=cfunction>system()</tt>, passing it the string we just got from
<tt class=cfunction>PyArg_ParseTuple()</tt>:

<P>
<dl><dd><pre class=verbatim>
    sts = system(command);
</pre></dl>

<P>
Our <tt class=function>spam.system()</tt> function must return the value of
<tt class=cdata>sts</tt> as a Python object.  This is done using the function
<tt class=cfunction>Py_BuildValue()</tt>, which is something like the inverse of
<tt class=cfunction>PyArg_ParseTuple()</tt>: it takes a format string and an
arbitrary number of C values, and returns a new Python object.
More info on <tt class=cfunction>Py_BuildValue()</tt> is given later.

<P>
<dl><dd><pre class=verbatim>
    return Py_BuildValue("i", sts);
</pre></dl>

<P>
In this case, it will return an integer object.  (Yes, even integers
are objects on the heap in Python!)

<P>
If you have a C function that returns no useful argument (a function
returning <tt class=ctype>void</tt>), the corresponding Python function must return
<tt>None</tt>.   You need this idiom to do so:

<P>
<dl><dd><pre class=verbatim>
    Py_INCREF(Py_None);
    return Py_None;
</pre></dl>

<P>
<tt class=cdata>Py_None</tt> is the C name for the special Python object
<tt>None</tt>.  It is a genuine Python object rather than a <tt>NULL</tt>
pointer, which means ``error'' in most contexts, as we have seen.

<P>
<p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="methodTable.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="intro.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="errors.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Extending and Embedding the Python Interpreter</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="methodTable.html">1.4 The Module's Method</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="intro.html">1. Extending Python with</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="errors.html">1.2 Intermezzo: Errors and</A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
