Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / html / python / ext / extending-with-embedding.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="embeddingInCplusplus.html" />
12<link rel="prev" href="pure-embedding.html" />
13<link rel="parent" href="embedding.html" />
14<link rel="next" href="embeddingInCplusplus.html" />
15<meta name='aesop' content='information' />
16<title>5.4 Extending Embedded Python </title>
17</head>
18<body>
19<DIV CLASS="navigation">
20<div id='top-navigation-panel' xml:id='top-navigation-panel'>
21<table align="center" width="100%" cellpadding="0" cellspacing="2">
22<tr>
23<td class='online-navigation'><a rel="prev" title="5.3 Pure Embedding"
24 href="pure-embedding.html"><img src='../icons/previous.png'
25 border='0' height='32' alt='Previous Page' width='32' /></A></td>
26<td class='online-navigation'><a rel="parent" title="5. Embedding Python in"
27 href="embedding.html"><img src='../icons/up.png'
28 border='0' height='32' alt='Up One Level' width='32' /></A></td>
29<td class='online-navigation'><a rel="next" title="5.5 Embedding Python in"
30 href="embeddingInCplusplus.html"><img src='../icons/next.png'
31 border='0' height='32' alt='Next Page' width='32' /></A></td>
32<td align="center" width="100%">Extending and Embedding the Python Interpreter</td>
33<td class='online-navigation'><a rel="contents" title="Table of Contents"
34 href="contents.html"><img src='../icons/contents.png'
35 border='0' height='32' alt='Contents' width='32' /></A></td>
36<td class='online-navigation'><img src='../icons/blank.png'
37 border='0' height='32' alt='' width='32' /></td>
38<td class='online-navigation'><img src='../icons/blank.png'
39 border='0' height='32' alt='' width='32' /></td>
40</tr></table>
41<div class='online-navigation'>
42<b class="navlabel">Previous:</b>
43<a class="sectref" rel="prev" href="pure-embedding.html">5.3 Pure Embedding</A>
44<b class="navlabel">Up:</b>
45<a class="sectref" rel="parent" href="embedding.html">5. Embedding Python in</A>
46<b class="navlabel">Next:</b>
47<a class="sectref" rel="next" href="embeddingInCplusplus.html">5.5 Embedding Python in</A>
48</div>
49<hr /></div>
50</DIV>
51<!--End of Navigation Panel-->
52
53<H1><A NAME="SECTION007400000000000000000"></A><A NAME="extending-with-embedding"></A>
54<BR>
555.4 Extending Embedded Python
56
57</H1>
58
59<P>
60Until now, the embedded Python interpreter had no access to
61functionality from the application itself. The Python API allows this
62by extending the embedded interpreter. That is, the embedded
63interpreter gets extended with routines provided by the application.
64While it sounds complex, it is not so bad. Simply forget for a while
65that the application starts the Python interpreter. Instead, consider
66the application to be a set of subroutines, and write some glue code
67that gives Python access to those routines, just like you would write
68a normal Python extension. For example:
69
70<P>
71<div class="verbatim"><pre>
72static int numargs=0;
73
74/* Return the number of arguments of the application command line */
75static PyObject*
76emb_numargs(PyObject *self, PyObject *args)
77{
78 if(!PyArg_ParseTuple(args, ":numargs"))
79 return NULL;
80 return Py_BuildValue("i", numargs);
81}
82
83static PyMethodDef EmbMethods[] = {
84 {"numargs", emb_numargs, METH_VARARGS,
85 "Return the number of arguments received by the process."},
86 {NULL, NULL, 0, NULL}
87};
88</pre></div>
89
90<P>
91Insert the above code just above the <tt class="cfunction">main()</tt> function.
92Also, insert the following two statements directly after
93<tt class="cfunction">Py_Initialize()</tt>:
94
95<P>
96<div class="verbatim"><pre>
97 numargs = argc;
98 Py_InitModule("emb", EmbMethods);
99</pre></div>
100
101<P>
102These two lines initialize the <code>numargs</code> variable, and make the
103<tt class="function">emb.numargs()</tt> function accessible to the embedded Python
104interpreter. With these extensions, the Python script can do things
105like
106
107<P>
108<div class="verbatim"><pre>
109import emb
110print "Number of arguments", emb.numargs()
111</pre></div>
112
113<P>
114In a real application, the methods will expose an API of the
115application to Python.
116
117<P>
118
119<DIV CLASS="navigation">
120<div class='online-navigation'>
121<p></p><hr />
122<table align="center" width="100%" cellpadding="0" cellspacing="2">
123<tr>
124<td class='online-navigation'><a rel="prev" title="5.3 Pure Embedding"
125 href="pure-embedding.html"><img src='../icons/previous.png'
126 border='0' height='32' alt='Previous Page' width='32' /></A></td>
127<td class='online-navigation'><a rel="parent" title="5. Embedding Python in"
128 href="embedding.html"><img src='../icons/up.png'
129 border='0' height='32' alt='Up One Level' width='32' /></A></td>
130<td class='online-navigation'><a rel="next" title="5.5 Embedding Python in"
131 href="embeddingInCplusplus.html"><img src='../icons/next.png'
132 border='0' height='32' alt='Next Page' width='32' /></A></td>
133<td align="center" width="100%">Extending and Embedding the Python Interpreter</td>
134<td class='online-navigation'><a rel="contents" title="Table of Contents"
135 href="contents.html"><img src='../icons/contents.png'
136 border='0' height='32' alt='Contents' width='32' /></A></td>
137<td class='online-navigation'><img src='../icons/blank.png'
138 border='0' height='32' alt='' width='32' /></td>
139<td class='online-navigation'><img src='../icons/blank.png'
140 border='0' height='32' alt='' width='32' /></td>
141</tr></table>
142<div class='online-navigation'>
143<b class="navlabel">Previous:</b>
144<a class="sectref" rel="prev" href="pure-embedding.html">5.3 Pure Embedding</A>
145<b class="navlabel">Up:</b>
146<a class="sectref" rel="parent" href="embedding.html">5. Embedding Python in</A>
147<b class="navlabel">Next:</b>
148<a class="sectref" rel="next" href="embeddingInCplusplus.html">5.5 Embedding Python in</A>
149</div>
150</div>
151<hr />
152<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
153</DIV>
154<!--End of Navigation Panel-->
155<ADDRESS>
156See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
157</ADDRESS>
158</BODY>
159</HTML>