|
1 | 1 | <html> |
2 | 2 | <head> |
3 | | -<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| 3 | +<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> |
4 | 4 | <title>Embedding</title> |
5 | | -<link rel="stylesheet" href="../../../../../../../doc/html/boostbook.css" type="text/css"> |
6 | | -<meta name="generator" content="DocBook XSL Stylesheets V1.66.1"> |
7 | | -<link rel="start" href="../index.html" title="Chapter 1. python 1.0"> |
8 | | -<link rel="up" href="../index.html" title="Chapter 1. python 1.0"> |
9 | | -<link rel="prev" href="object.html" title=" Object Interface"> |
| 5 | +<link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css"> |
| 6 | +<meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> |
| 7 | +<link rel="home" href="../index.html" title="Chapter 1. python 2.0"> |
| 8 | +<link rel="up" href="../index.html" title="Chapter 1. python 2.0"> |
| 9 | +<link rel="prev" href="object.html" title="Object Interface"> |
10 | 10 | <link rel="next" href="iterators.html" title="Iterators"> |
11 | 11 | </head> |
12 | 12 | <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> |
|
20 | 20 | </tr></table> |
21 | 21 | <hr> |
22 | 22 | <div class="spirit-nav"> |
23 | | -<a accesskey="p" href="object.html"><img src="../../../../../../../doc/html/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../../../doc/html/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../../../doc/html/images/home.png" alt="Home"></a><a accesskey="n" href="iterators.html"><img src="../../../../../../../doc/html/images/next.png" alt="Next"></a> |
| 23 | +<a accesskey="p" href="object.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="iterators.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> |
24 | 24 | </div> |
25 | | -<div class="section" lang="en"> |
| 25 | +<div class="section"> |
26 | 26 | <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
27 | 27 | <a name="python.embedding"></a>Embedding</h2></div></div></div> |
28 | 28 | <div class="toc"><dl><dt><span class="section"><a href="embedding.html#python.using_the_interpreter">Using the interpreter</a></span></dt></dl></div> |
|
39 | 39 | a lot easier and, in a future version, it may become unnecessary to touch the |
40 | 40 | Python/C API at all. So stay tuned... <span class="inlinemediaobject"><img src="../images/smiley.png" alt="smiley"></span> |
41 | 41 | </p> |
42 | | -<a name="embedding.building_embedded_programs"></a><h2> |
43 | | -<a name="id472330"></a> |
| 42 | +<a name="embedding.building_embedded_programs"></a><h3> |
| 43 | +<a name="id773307"></a> |
44 | 44 | Building embedded programs |
45 | | - </h2> |
| 45 | + </h3> |
46 | 46 | <p> |
47 | 47 | To be able to embed python into your programs, you have to link to both Boost.Python's |
48 | 48 | as well as Python's own runtime library. |
49 | 49 | </p> |
50 | 50 | <p> |
51 | 51 | Boost.Python's library comes in two variants. Both are located in Boost's |
52 | | - <tt class="literal">/libs/python/build/bin-stage</tt> subdirectory. On Windows, the |
53 | | - variants are called <tt class="literal">boost_python.lib</tt> (for release builds) |
54 | | - and <tt class="literal">boost_python_debug.lib</tt> (for debugging). If you can't |
| 52 | + <code class="literal">/libs/python/build/bin-stage</code> subdirectory. On Windows, the |
| 53 | + variants are called <code class="literal">boost_python.lib</code> (for release builds) |
| 54 | + and <code class="literal">boost_python_debug.lib</code> (for debugging). If you can't |
55 | 55 | find the libraries, you probably haven't built Boost.Python yet. See <a href="../../../../building.html" target="_top">Building and Testing</a> on how to do this. |
56 | 56 | </p> |
57 | 57 | <p> |
58 | | - Python's library can be found in the <tt class="literal">/libs</tt> subdirectory |
| 58 | + Python's library can be found in the <code class="literal">/libs</code> subdirectory |
59 | 59 | of your Python directory. On Windows it is called pythonXY.lib where X.Y is |
60 | 60 | your major Python version number. |
61 | 61 | </p> |
62 | 62 | <p> |
63 | | - Additionally, Python's <tt class="literal">/include</tt> subdirectory has to be added |
| 63 | + Additionally, Python's <code class="literal">/include</code> subdirectory has to be added |
64 | 64 | to your include path. |
65 | 65 | </p> |
66 | 66 | <p> |
|
81 | 81 | <library-path>$(PYTHON_LIB_PATH) |
82 | 82 | <find-library>$(PYTHON_EMBEDDED_LIBRARY) ; |
83 | 83 | </pre> |
84 | | -<a name="embedding.getting_started"></a><h2> |
85 | | -<a name="id472435"></a> |
| 84 | +<a name="embedding.getting_started"></a><h3> |
| 85 | +<a name="id773391"></a> |
86 | 86 | Getting started |
87 | | - </h2> |
| 87 | + </h3> |
88 | 88 | <p> |
89 | 89 | Being able to build is nice, but there is nothing to build yet. Embedding the |
90 | 90 | Python interpreter into one of your C++ programs requires these 4 steps: |
91 | 91 | </p> |
92 | | -<div class="orderedlist"><ol type="1"> |
93 | | -<li> |
94 | | - #include <tt class="literal"><boost/python.hpp></tt> |
95 | | -</li> |
96 | | -<li> |
97 | | - Call <a href="http://www.python.org/doc/current/api/initialization.html#l2h-652" target="_top">Py_Initialize</a>() |
98 | | - to start the interpreter and create the <tt class="literal"><span class="underline">_main</span>_</tt> |
99 | | - module. |
100 | | - </li> |
101 | | -<li> |
102 | | - Call other Python C API routines to use the interpreter. |
103 | | - </li> |
| 92 | +<div class="orderedlist"><ol class="orderedlist" type="1"> |
| 93 | +<li class="listitem"> |
| 94 | + #include <code class="literal"><boost/python.hpp></code> |
| 95 | + </li> |
| 96 | +<li class="listitem"> |
| 97 | + Call <a href="http://www.python.org/doc/current/api/initialization.html#l2h-652" target="_top">Py_Initialize</a>() |
| 98 | + to start the interpreter and create the <code class="literal"><span class="underline">_main</span>_</code> |
| 99 | + module. |
| 100 | + </li> |
| 101 | +<li class="listitem"> |
| 102 | + Call other Python C API routines to use the interpreter. |
| 103 | + </li> |
104 | 104 | </ol></div> |
105 | 105 | <div class="note"><table border="0" summary="Note"> |
106 | 106 | <tr> |
107 | | -<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/html/images/note.png"></td> |
| 107 | +<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td> |
108 | 108 | <th align="left">Note</th> |
109 | 109 | </tr> |
110 | | -<tr><td colspan="2" align="left" valign="top"><p> |
111 | | - <span class="bold"><b>Note that at this time you must not call <a href="http://www.python.org/doc/current/api/initialization.html#l2h-656" target="_top">Py_Finalize</a>() |
112 | | - to stop the interpreter. This may be fixed in a future version of boost.python.</b></span> |
| 110 | +<tr><td align="left" valign="top"><p> |
| 111 | + <span class="bold"><strong>Note that at this time you must not call <a href="http://www.python.org/doc/current/api/initialization.html#l2h-656" target="_top">Py_Finalize</a>() |
| 112 | + to stop the interpreter. This may be fixed in a future version of boost.python.</strong></span> |
113 | 113 | </p></td></tr> |
114 | 114 | </table></div> |
115 | 115 | <p> |
116 | 116 | (Of course, there can be other C++ code between all of these steps.) |
117 | 117 | </p> |
118 | | -<div class="blockquote"><blockquote class="blockquote"> |
119 | | -<p> |
120 | | - </p> |
121 | | -<p> |
122 | | - <span class="emphasis"><em><span class="bold"><b>Now that we can embed the interpreter in |
123 | | - our programs, lets see how to put it to use...</b></span></em></span> |
124 | | - </p> |
125 | | -<p> |
126 | | - </p> |
127 | | -</blockquote></div> |
128 | | -<div class="section" lang="en"> |
| 118 | +<div class="blockquote"><blockquote class="blockquote"><p> |
| 119 | + <span class="emphasis"><em><span class="bold"><strong>Now that we can embed the interpreter in |
| 120 | + our programs, lets see how to put it to use...</strong></span></em></span> |
| 121 | + </p></blockquote></div> |
| 122 | +<div class="section"> |
129 | 123 | <div class="titlepage"><div><div><h3 class="title"> |
130 | 124 | <a name="python.using_the_interpreter"></a>Using the interpreter</h3></div></div></div> |
131 | 125 | <p> |
132 | 126 | As you probably already know, objects in Python are reference-counted. Naturally, |
133 | | - the <tt class="literal">PyObject</tt>s of the Python C API are also reference-counted. |
| 127 | + the <code class="literal">PyObject</code>s of the Python C API are also reference-counted. |
134 | 128 | There is a difference however. While the reference-counting is fully automatic |
135 | 129 | in Python, the Python C API requires you to do it <a href="http://www.python.org/doc/current/c-api/refcounting.html" target="_top">by |
136 | 130 | hand</a>. This is messy and especially hard to get right in the presence |
137 | 131 | of C++ exceptions. Fortunately Boost.Python provides the <a href="../../../../v2/handle.html" target="_top">handle</a> |
138 | 132 | and <a href="../../../../v2/object.html" target="_top">object</a> class templates to |
139 | 133 | automate the process. |
140 | 134 | </p> |
141 | | -<a name="using_the_interpreter.running_python_code"></a><h2> |
142 | | -<a name="id472601"></a> |
| 135 | +<a name="using_the_interpreter.running_python_code"></a><h3> |
| 136 | +<a name="id773549"></a> |
143 | 137 | Running Python code |
144 | | - </h2> |
| 138 | + </h3> |
145 | 139 | <p> |
146 | 140 | Boost.python provides three related functions to run Python code from C++. |
147 | 141 | </p> |
|
155 | 149 | and exec_file executes the code contained in the given file. |
156 | 150 | </p> |
157 | 151 | <p> |
158 | | - The <tt class="literal">globals</tt> and <tt class="literal">locals</tt> parameters are |
| 152 | + The <code class="literal">globals</code> and <code class="literal">locals</code> parameters are |
159 | 153 | Python dictionaries containing the globals and locals of the context in which |
160 | 154 | to run the code. For most intents and purposes you can use the namespace |
161 | | - dictionary of the <tt class="literal"><span class="underline">_main</span>_</tt> |
| 155 | + dictionary of the <code class="literal"><span class="underline">_main</span>_</code> |
162 | 156 | module for both parameters. |
163 | 157 | </p> |
164 | 158 | <p> |
|
171 | 165 | first), and returns it. |
172 | 166 | </p> |
173 | 167 | <p> |
174 | | - Let's import the <tt class="literal"><span class="underline">_main</span>_</tt> |
| 168 | + Let's import the <code class="literal"><span class="underline">_main</span>_</code> |
175 | 169 | module and run some Python code in its namespace: |
176 | 170 | </p> |
177 | 171 | <pre class="programlisting"><span class="identifier">object</span> <span class="identifier">main_module</span> <span class="special">=</span> <span class="identifier">import</span><span class="special">(</span><span class="string">"__main__"</span><span class="special">);</span> |
|
186 | 180 | This should create a file called 'hello.txt' in the current directory containing |
187 | 181 | a phrase that is well-known in programming circles. |
188 | 182 | </p> |
189 | | -<a name="using_the_interpreter.manipulating_python_objects"></a><h2> |
190 | | -<a name="id473185"></a> |
| 183 | +<a name="using_the_interpreter.manipulating_python_objects"></a><h3> |
| 184 | +<a name="id774064"></a> |
191 | 185 | Manipulating Python objects |
192 | | - </h2> |
| 186 | + </h3> |
193 | 187 | <p> |
194 | 188 | Often we'd like to have a class to manipulate Python objects. But we have |
195 | 189 | already seen such a class above, and in the <a href="object.html" target="_top">previous |
196 | | - section</a>: the aptly named <tt class="literal">object</tt> class and its |
197 | | - derivatives. We've already seen that they can be constructed from a <tt class="literal">handle</tt>. |
| 190 | + section</a>: the aptly named <code class="literal">object</code> class and its |
| 191 | + derivatives. We've already seen that they can be constructed from a <code class="literal">handle</code>. |
198 | 192 | The following examples should further illustrate this fact: |
199 | 193 | </p> |
200 | 194 | <pre class="programlisting"><span class="identifier">object</span> <span class="identifier">main_module</span> <span class="special">=</span> <span class="identifier">import</span><span class="special">(</span><span class="string">"__main__"</span><span class="special">);</span> |
|
203 | 197 | <span class="keyword">int</span> <span class="identifier">five_squared</span> <span class="special">=</span> <span class="identifier">extract</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(</span><span class="identifier">main_namespace</span><span class="special">[</span><span class="string">"result"</span><span class="special">]);</span> |
204 | 198 | </pre> |
205 | 199 | <p> |
206 | | - Here we create a dictionary object for the <tt class="literal"><span class="underline">_main</span>_</tt> |
| 200 | + Here we create a dictionary object for the <code class="literal"><span class="underline">_main</span>_</code> |
207 | 201 | module's namespace. Then we assign 5 squared to the result variable and read |
208 | 202 | this variable from the dictionary. Another way to achieve the same result |
209 | 203 | is to use eval instead, which returns the result directly: |
210 | 204 | </p> |
211 | 205 | <pre class="programlisting"><span class="identifier">object</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">eval</span><span class="special">(</span><span class="string">"5 ** 2"</span><span class="special">);</span> |
212 | 206 | <span class="keyword">int</span> <span class="identifier">five_squared</span> <span class="special">=</span> <span class="identifier">extract</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(</span><span class="identifier">result</span><span class="special">);</span> |
213 | 207 | </pre> |
214 | | -<a name="using_the_interpreter.exception_handling"></a><h2> |
215 | | -<a name="id473554"></a> |
| 208 | +<a name="using_the_interpreter.exception_handling"></a><h3> |
| 209 | +<a name="id774390"></a> |
216 | 210 | Exception handling |
217 | | - </h2> |
| 211 | + </h3> |
218 | 212 | <p> |
219 | 213 | If an exception occurs in the evaluation of the python expression, <a href="../../../../v2/errors.html#error_already_set-spec" target="_top">error_already_set</a> |
220 | 214 | is thrown: |
|
231 | 225 | </span><span class="special">}</span> |
232 | 226 | </pre> |
233 | 227 | <p> |
234 | | - The <tt class="literal">error_already_set</tt> exception class doesn't carry any |
| 228 | + The <code class="literal">error_already_set</code> exception class doesn't carry any |
235 | 229 | information in itself. To find out more about the Python exception that occurred, |
236 | 230 | you need to use the <a href="http://www.python.org/doc/api/exceptionHandling.html" target="_top">exception |
237 | 231 | handling functions</a> of the Python C API in your catch-statement. This |
|
261 | 255 | </div> |
262 | 256 | <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> |
263 | 257 | <td align="left"></td> |
264 | | -<td align="right"><div class="copyright-footer">Copyright © 2002-2005 Joel |
| 258 | +<td align="right"><div class="copyright-footer">Copyright © 2002-2005 Joel |
265 | 259 | de Guzman, David Abrahams<p> |
266 | 260 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
267 | 261 | file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top"> |
|
271 | 265 | </tr></table> |
272 | 266 | <hr> |
273 | 267 | <div class="spirit-nav"> |
274 | | -<a accesskey="p" href="object.html"><img src="../../../../../../../doc/html/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../../../doc/html/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../../../doc/html/images/home.png" alt="Home"></a><a accesskey="n" href="iterators.html"><img src="../../../../../../../doc/html/images/next.png" alt="Next"></a> |
| 268 | +<a accesskey="p" href="object.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="iterators.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> |
275 | 269 | </div> |
276 | 270 | </body> |
277 | 271 | </html> |
0 commit comments