|
3 | 3 | <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> |
4 | 4 | <title>Embedding</title> |
5 | 5 | <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css"> |
6 | | -<meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> |
| 6 | +<meta name="generator" content="DocBook XSL Stylesheets V1.76.1"> |
7 | 7 | <link rel="home" href="../index.html" title="Chapter 1. python 2.0"> |
8 | 8 | <link rel="up" href="../index.html" title="Chapter 1. python 2.0"> |
9 | 9 | <link rel="prev" href="object.html" title="Object Interface"> |
|
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="id773307"></a> |
| 42 | +<h3> |
| 43 | +<a name="embedding.building_embedded_programs"></a> |
44 | 44 | Building embedded programs |
45 | 45 | </h3> |
46 | 46 | <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="id773391"></a> |
| 84 | +<h3> |
| 85 | +<a name="embedding.getting_started"></a> |
86 | 86 | Getting started |
87 | 87 | </h3> |
88 | 88 | <p> |
|
95 | 95 | </li> |
96 | 96 | <li class="listitem"> |
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. |
| 98 | + to start the interpreter and create the <code class="literal">__main__</code> module. |
100 | 99 | </li> |
101 | 100 | <li class="listitem"> |
102 | 101 | Call other Python C API routines to use the interpreter. |
|
132 | 131 | and <a href="../../../../v2/object.html" target="_top">object</a> class templates to |
133 | 132 | automate the process. |
134 | 133 | </p> |
135 | | -<a name="using_the_interpreter.running_python_code"></a><h3> |
136 | | -<a name="id773549"></a> |
| 134 | +<h3> |
| 135 | +<a name="using_the_interpreter.running_python_code"></a> |
137 | 136 | Running Python code |
138 | 137 | </h3> |
139 | 138 | <p> |
|
152 | 151 | The <code class="literal">globals</code> and <code class="literal">locals</code> parameters are |
153 | 152 | Python dictionaries containing the globals and locals of the context in which |
154 | 153 | to run the code. For most intents and purposes you can use the namespace |
155 | | - dictionary of the <code class="literal"><span class="underline">_main</span>_</code> |
156 | | - module for both parameters. |
| 154 | + dictionary of the <code class="literal">__main__</code> module for both parameters. |
157 | 155 | </p> |
158 | 156 | <p> |
159 | 157 | Boost.python provides a function to import a module: |
|
165 | 163 | first), and returns it. |
166 | 164 | </p> |
167 | 165 | <p> |
168 | | - Let's import the <code class="literal"><span class="underline">_main</span>_</code> |
169 | | - module and run some Python code in its namespace: |
| 166 | + Let's import the <code class="literal">__main__</code> module and run some Python code |
| 167 | + in its namespace: |
170 | 168 | </p> |
171 | 169 | <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> |
172 | 170 | <span class="identifier">object</span> <span class="identifier">main_namespace</span> <span class="special">=</span> <span class="identifier">main_module</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"__dict__"</span><span class="special">);</span> |
|
180 | 178 | This should create a file called 'hello.txt' in the current directory containing |
181 | 179 | a phrase that is well-known in programming circles. |
182 | 180 | </p> |
183 | | -<a name="using_the_interpreter.manipulating_python_objects"></a><h3> |
184 | | -<a name="id774064"></a> |
| 181 | +<h3> |
| 182 | +<a name="using_the_interpreter.manipulating_python_objects"></a> |
185 | 183 | Manipulating Python objects |
186 | 184 | </h3> |
187 | 185 | <p> |
|
197 | 195 | <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> |
198 | 196 | </pre> |
199 | 197 | <p> |
200 | | - Here we create a dictionary object for the <code class="literal"><span class="underline">_main</span>_</code> |
201 | | - module's namespace. Then we assign 5 squared to the result variable and read |
202 | | - this variable from the dictionary. Another way to achieve the same result |
203 | | - is to use eval instead, which returns the result directly: |
| 198 | + Here we create a dictionary object for the <code class="literal">__main__</code> module's |
| 199 | + namespace. Then we assign 5 squared to the result variable and read this |
| 200 | + variable from the dictionary. Another way to achieve the same result is to |
| 201 | + use eval instead, which returns the result directly: |
204 | 202 | </p> |
205 | 203 | <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> |
206 | 204 | <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> |
207 | 205 | </pre> |
208 | | -<a name="using_the_interpreter.exception_handling"></a><h3> |
209 | | -<a name="id774390"></a> |
| 206 | +<h3> |
| 207 | +<a name="using_the_interpreter.exception_handling"></a> |
210 | 208 | Exception handling |
211 | 209 | </h3> |
212 | 210 | <p> |
|
216 | 214 | <pre class="programlisting"><span class="keyword">try</span> |
217 | 215 | <span class="special">{</span> |
218 | 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/0"</span><span class="special">);</span> |
219 | | - <span class="comment">// execution will never get here: |
220 | | -</span> <span class="keyword">int</span> <span class="identifier">five_divided_by_zero</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> |
| 217 | + <span class="comment">// execution will never get here:</span> |
| 218 | + <span class="keyword">int</span> <span class="identifier">five_divided_by_zero</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> |
221 | 219 | <span class="special">}</span> |
222 | 220 | <span class="keyword">catch</span><span class="special">(</span><span class="identifier">error_already_set</span> <span class="keyword">const</span> <span class="special">&)</span> |
223 | 221 | <span class="special">{</span> |
224 | | - <span class="comment">// handle the exception in some way |
225 | | -</span><span class="special">}</span> |
| 222 | + <span class="comment">// handle the exception in some way</span> |
| 223 | +<span class="special">}</span> |
226 | 224 | </pre> |
227 | 225 | <p> |
228 | 226 | The <code class="literal">error_already_set</code> exception class doesn't carry any |
|
238 | 236 | <span class="special">{</span> |
239 | 237 | <span class="keyword">if</span> <span class="special">(</span><span class="identifier">PyErr_ExceptionMatches</span><span class="special">(</span><span class="identifier">PyExc_ZeroDivisionError</span><span class="special">))</span> |
240 | 238 | <span class="special">{</span> |
241 | | - <span class="comment">// handle ZeroDivisionError specially |
242 | | -</span> <span class="special">}</span> |
| 239 | + <span class="comment">// handle ZeroDivisionError specially</span> |
| 240 | + <span class="special">}</span> |
243 | 241 | <span class="keyword">else</span> |
244 | 242 | <span class="special">{</span> |
245 | | - <span class="comment">// print all other errors to stderr |
246 | | -</span> <span class="identifier">PyErr_Print</span><span class="special">();</span> |
| 243 | + <span class="comment">// print all other errors to stderr</span> |
| 244 | + <span class="identifier">PyErr_Print</span><span class="special">();</span> |
247 | 245 | <span class="special">}</span> |
248 | 246 | <span class="special">}</span> |
249 | 247 | </pre> |
|
0 commit comments