<!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.4 The Module's Method Table and Initialization Function
</TITLE>
<META NAME="description" CONTENT="1.4 The Module's Method Table and Initialization Function
">
<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="compilation.html">
<LINK REL="previous" HREF="backToExample.html">
<LINK REL="up" HREF="intro.html">
<LINK REL="next" HREF="compilation.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="compilation.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="backToExample.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="compilation.html">1.5 Compilation and Linkage</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="backToExample.html">1.3 Back to the</A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION003400000000000000000"></A>
<BR>
1.4 The Module's Method Table and Initialization Function
         
</H1>

<P>
I promised to show how <tt class=cfunction>spam_system()</tt> is called from Python
programs.  First, we need to list its name and address in a ``method
table'':

<P>
<dl><dd><pre class=verbatim>
static PyMethodDef SpamMethods[] = {
    ...
    {"system",  spam_system, METH_VARARGS},
    ...
    {NULL,      NULL}        /* Sentinel */
};
</pre></dl>

<P>
Note the third entry ("<tt class=samp>METH_VARARGS</tt>").  This is a flag telling
the interpreter the calling convention to be used for the C
function.  It should normally always be "<tt class=samp>METH_VARARGS</tt>" or
"<tt class=samp>METH_VARARGS | METH_KEYWORDS</tt>"; a value of <tt>0</tt> means that an
obsolete variant of <tt class=cfunction>PyArg_ParseTuple()</tt> is used.

<P>
When using only "<tt class=samp>METH_VARARGS</tt>", the function should expect
the Python-level parameters to be passed in as a tuple acceptable for
parsing via <tt class=cfunction>PyArg_ParseTuple()</tt>; more information on this
function is provided below.

<P>
The <tt class=constant>METH_KEYWORDS</tt> bit may be set in the third field if keyword
arguments should be passed to the function.  In this case, the C
function should accept a third "<tt class=samp>PyObject *</tt>" parameter which will
be a dictionary of keywords.  Use <tt class=cfunction>PyArg_ParseTupleAndKeywords()</tt>
to parse the arguments to such a function.

<P>
The method table must be passed to the interpreter in the module's
initialization function (which should be the only non-<tt>static</tt>item defined in the module file):

<P>
<dl><dd><pre class=verbatim>
void
initspam()
{
    (void) Py_InitModule("spam", SpamMethods);
}
</pre></dl>

<P>
When the Python program imports module <tt class=module>spam</tt> for the first
time, <tt class=cfunction>initspam()</tt> is called.  It calls
<tt class=cfunction>Py_InitModule()</tt>, which creates a ``module object'' (which
is inserted in the dictionary <tt>sys.modules</tt> under the key
<tt>"spam"</tt>), and inserts built-in function objects into the newly
created module based upon the table (an array of <tt class=ctype>PyMethodDef</tt>
structures) that was passed as its second argument.
<tt class=cfunction>Py_InitModule()</tt> returns a pointer to the module object
that it creates (which is unused here).  It aborts with a fatal error
if the module could not be initialized satisfactorily, so the caller
doesn't need to check for errors.

<P>
<p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="compilation.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="backToExample.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="compilation.html">1.5 Compilation and Linkage</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="backToExample.html">1.3 Back to the</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>
