|
3 | 3 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
4 | 4 | <title>Embedding</title> |
5 | 5 | <link rel="stylesheet" href="../boostbook.css" type="text/css"> |
6 | | -<meta name="generator" content="DocBook XSL Stylesheets V1.72.0"> |
| 6 | +<meta name="generator" content="DocBook XSL Stylesheets V1.66.1"> |
7 | 7 | <link rel="start" href="../index.html" title="Chapter 1. python 1.0"> |
8 | 8 | <link rel="up" href="../index.html" title="Chapter 1. python 1.0"> |
9 | | -<link rel="prev" href="object.html" title="Object Interface"> |
| 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"> |
13 | | -<table cellpadding="2" width="100%"> |
| 13 | +<table cellpadding="2" width="100%"><tr> |
14 | 14 | <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td> |
15 | 15 | <td align="center"><a href="../../../../../../../index.htm">Home</a></td> |
16 | 16 | <td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td> |
17 | 17 | <td align="center"><a href="../../../../../../../people/people.htm">People</a></td> |
18 | 18 | <td align="center"><a href="../../../../../../../more/faq.htm">FAQ</a></td> |
19 | 19 | <td align="center"><a href="../../../../../../../more/index.htm">More</a></td> |
20 | | -</table> |
| 20 | +</tr></table> |
21 | 21 | <hr> |
22 | 22 | <div class="spirit-nav"> |
23 | | -<a accesskey="p" href="object.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="iterators.html"><img src="../images/next.png" alt="Next"></a> |
| 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> |
24 | 24 | </div> |
25 | 25 | <div class="section" lang="en"> |
26 | 26 | <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|
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><h3> |
43 | | -<a name="id2654982"></a> |
| 42 | +<a name="embedding.building_embedded_programs"></a><h2> |
| 43 | +<a name="id471088"></a> |
44 | 44 | Building embedded programs |
45 | | - </h3> |
| 45 | + </h2> |
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 | | - <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 |
| 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 |
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 <code class="literal">/libs</code> subdirectory |
| 58 | + Python's library can be found in the <tt class="literal">/libs</tt> 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 <code class="literal">/include</code> subdirectory has to be added |
| 63 | + Additionally, Python's <tt class="literal">/include</tt> 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><h3> |
85 | | -<a name="id2655076"></a> |
| 84 | +<a name="embedding.getting_started"></a><h2> |
| 85 | +<a name="id471193"></a> |
86 | 86 | Getting started |
87 | | - </h3> |
| 87 | + </h2> |
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 | 92 | <div class="orderedlist"><ol type="1"> |
93 | 93 | <li> |
94 | | - #include <code class="literal"><boost/python.hpp></code><br><br> |
| 94 | + #include <tt class="literal"><boost/python.hpp></tt> |
95 | 95 | </li> |
96 | 96 | <li> |
97 | 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.<br><br> |
100 | | -</li> |
| 98 | + to start the interpreter and create the <tt class="literal"><span class="underline">_main</span>_</tt> |
| 99 | + module. |
| 100 | + </li> |
101 | 101 | <li> |
102 | | - Call other Python C API routines to use the interpreter.<br><br> |
103 | | -</li> |
| 102 | + Call other Python C API routines to use the interpreter. |
| 103 | + </li> |
104 | 104 | </ol></div> |
105 | | -<div class="sidebar"> |
106 | | -<p class="title"><b></b></p> |
107 | | -<p> |
108 | | - <span class="inlinemediaobject"><img src="../images/note.png" alt="note"></span> <span class="bold"><strong>Note that at this time you must |
109 | | - not call <a href="http://www.python.org/doc/current/api/initialization.html#l2h-656" target="_top">Py_Finalize</a>() |
110 | | - to stop the interpreter. This may be fixed in a future version of boost.python.</strong></span> |
111 | | - </p> |
112 | | -</div> |
| 105 | +<div class="note"><table border="0" summary="Note"> |
| 106 | +<tr> |
| 107 | +<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/html/images/note.png"></td> |
| 108 | +<th align="left">Note</th> |
| 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> |
| 113 | + </p></td></tr> |
| 114 | +</table></div> |
113 | 115 | <p> |
114 | 116 | (Of course, there can be other C++ code between all of these steps.) |
115 | 117 | </p> |
116 | 118 | <div class="blockquote"><blockquote class="blockquote"> |
117 | 119 | <p> |
118 | 120 | </p> |
119 | 121 | <p> |
120 | | - <span class="emphasis"><em><span class="bold"><strong>Now that we can embed the interpreter in |
121 | | - our programs, lets see how to put it to use...</strong></span></em></span> |
| 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> |
122 | 124 | </p> |
123 | 125 | <p> |
124 | 126 | </p> |
|
128 | 130 | <a name="python.using_the_interpreter"></a>Using the interpreter</h3></div></div></div> |
129 | 131 | <p> |
130 | 132 | As you probably already know, objects in Python are reference-counted. Naturally, |
131 | | - the <code class="literal">PyObject</code>s of the Python/C API are also reference-counted. |
| 133 | + the <tt class="literal">PyObject</tt>s of the Python/C API are also reference-counted. |
132 | 134 | There is a difference however. While the reference-counting is fully automatic |
133 | 135 | in Python, the Python<span class="emphasis"><em>C API requires you to do it [@http:</em></span>/www.python.org/doc/current/api/refcounts.html |
134 | 136 | by hand]. This is messy and especially hard to get right in the presence |
135 | 137 | of C++ exceptions. Fortunately Boost.Python provides the <a href="../../../../v2/handle.html" target="_top">handle</a> |
136 | 138 | and <a href="../../../../v2/object.html" target="_top">object</a> class templates to |
137 | 139 | automate the process. |
138 | 140 | </p> |
139 | | -<a name="using_the_interpreter.running_python_code"></a><h3> |
140 | | -<a name="id2655255"></a> |
| 141 | +<a name="using_the_interpreter.running_python_code"></a><h2> |
| 142 | +<a name="id471356"></a> |
141 | 143 | Running Python code |
142 | | - </h3> |
| 144 | + </h2> |
143 | 145 | <p> |
144 | 146 | Boost.python provides three related functions to run Python code from C++. |
145 | 147 | </p> |
|
154 | 156 | and exec_file executes the code contained in the given file. |
155 | 157 | </p> |
156 | 158 | <p> |
157 | | - The <code class="literal">globals</code> and <code class="literal">locals</code> parameters are |
| 159 | + The <tt class="literal">globals</tt> and <tt class="literal">locals</tt> parameters are |
158 | 160 | Python dictionaries containing the globals and locals of the context in which |
159 | 161 | to run the code. For most intents and purposes you can use the namespace |
160 | | - dictionary of the <code class="literal"><span class="underline">_main</span>_</code> |
| 162 | + dictionary of the <tt class="literal"><span class="underline">_main</span>_</tt> |
161 | 163 | module for both parameters. |
162 | 164 | </p> |
163 | 165 | <p> |
|
171 | 173 | first), and returns it. |
172 | 174 | </p> |
173 | 175 | <p> |
174 | | - Let's import the <code class="literal"><span class="underline">_main</span>_</code> |
| 176 | + Let's import the <tt class="literal"><span class="underline">_main</span>_</tt> |
175 | 177 | module and run some Python code in its namespace: |
176 | 178 | </p> |
177 | 179 | <pre class="programlisting"> |
|
187 | 189 | This should create a file called 'hello.txt' in the current directory containing |
188 | 190 | a phrase that is well-known in programming circles. |
189 | 191 | </p> |
190 | | -<a name="using_the_interpreter.manipulating_python_objects"></a><h3> |
191 | | -<a name="id2655783"></a> |
| 192 | +<a name="using_the_interpreter.manipulating_python_objects"></a><h2> |
| 193 | +<a name="id471944"></a> |
192 | 194 | Manipulating Python objects |
193 | | - </h3> |
| 195 | + </h2> |
194 | 196 | <p> |
195 | 197 | Often we'd like to have a class to manipulate Python objects. But we have |
196 | 198 | already seen such a class above, and in the <a href="object.html" target="_top">previous |
197 | | - section</a>: the aptly named <code class="literal">object</code> class and its |
198 | | - derivatives. We've already seen that they can be constructed from a <code class="literal">handle</code>. |
| 199 | + section</a>: the aptly named <tt class="literal">object</tt> class and its |
| 200 | + derivatives. We've already seen that they can be constructed from a <tt class="literal">handle</tt>. |
199 | 201 | The following examples should further illustrate this fact: |
200 | 202 | </p> |
201 | 203 | <pre class="programlisting"> |
|
205 | 207 | <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> |
206 | 208 | </pre> |
207 | 209 | <p> |
208 | | - Here we create a dictionary object for the <code class="literal"><span class="underline">_main</span>_</code> |
| 210 | + Here we create a dictionary object for the <tt class="literal"><span class="underline">_main</span>_</tt> |
209 | 211 | module's namespace. Then we assign 5 squared to the result variable and read |
210 | 212 | this variable from the dictionary. Another way to achieve the same result |
211 | 213 | is to use eval instead, which returns the result directly: |
|
214 | 216 | <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> |
215 | 217 | <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> |
216 | 218 | </pre> |
217 | | -<a name="using_the_interpreter.exception_handling"></a><h3> |
218 | | -<a name="id2656116"></a> |
| 219 | +<a name="using_the_interpreter.exception_handling"></a><h2> |
| 220 | +<a name="id472316"></a> |
219 | 221 | Exception handling |
220 | | - </h3> |
| 222 | + </h2> |
221 | 223 | <p> |
222 | 224 | 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> |
223 | 225 | is thrown: |
|
235 | 237 | </span><span class="special">}</span> |
236 | 238 | </pre> |
237 | 239 | <p> |
238 | | - The <code class="literal">error_already_set</code> exception class doesn't carry any |
| 240 | + The <tt class="literal">error_already_set</tt> exception class doesn't carry any |
239 | 241 | information in itself. To find out more about the Python exception that occurred, |
240 | 242 | you need to use the <a href="http://www.python.org/doc/api/exceptionHandling.html" target="_top">exception |
241 | 243 | handling functions</a> of the Python<span class="emphasis"><em>C API in your catch-statement. |
|
271 | 273 | </tr></table> |
272 | 274 | <hr> |
273 | 275 | <div class="spirit-nav"> |
274 | | -<a accesskey="p" href="object.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="iterators.html"><img src="../images/next.png" alt="Next"></a> |
| 276 | +<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> |
275 | 277 | </div> |
276 | 278 | </body> |
277 | 279 | </html> |
0 commit comments