forked from boostorg/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembedding.html
More file actions
205 lines (199 loc) · 14.4 KB
/
Copy pathembedding.html
File metadata and controls
205 lines (199 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Chapter 6. Embedding</title>
<link rel="stylesheet" href="../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
<link rel="home" href="index.html" title="Boost.Python Reference Manual">
<link rel="up" href="index.html" title="Boost.Python Reference Manual">
<link rel="prev" href="to_from_python_type_conversion/boost_python_register_ptr_to_pyt.html" title="boost/python/register_ptr_to_python.hpp">
<link rel="next" href="embedding/boost_python_import_hpp.html" title="boost/python/import.hpp">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../images/bpl.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="to_from_python_type_conversion/boost_python_register_ptr_to_pyt.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="embedding/boost_python_import_hpp.html"><img src="../images/next.png" alt="Next"></a>
</div>
<div class="chapter">
<div class="titlepage"><div><div><h1 class="title">
<a name="embedding"></a>Chapter 6. Embedding</h1></div></div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl class="toc">
<dt><span class="section"><a href="embedding.html#embedding.boost_python_exec_hpp">boost/python/exec.hpp</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="embedding.html#embedding.boost_python_exec_hpp.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="embedding.html#embedding.boost_python_exec_hpp.function_eval">Function
<code class="computeroutput"><span class="identifier">eval</span></code></a></span></dt>
<dt><span class="section"><a href="embedding.html#embedding.boost_python_exec_hpp.function_exec">Function
<code class="computeroutput"><span class="identifier">exec</span></code></a></span></dt>
<dt><span class="section"><a href="embedding.html#embedding.boost_python_exec_hpp.function_exec_file">Function
<code class="computeroutput"><span class="identifier">exec_file</span></code></a></span></dt>
<dt><span class="section"><a href="embedding.html#embedding.boost_python_exec_hpp.examples">Examples</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="embedding/boost_python_import_hpp.html">boost/python/import.hpp</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="embedding/boost_python_import_hpp.html#embedding.boost_python_import_hpp.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="embedding/boost_python_import_hpp.html#embedding.boost_python_import_hpp.function_import">Function
<code class="computeroutput"><span class="identifier">import</span></code></a></span></dt>
<dt><span class="section"><a href="embedding/boost_python_import_hpp.html#embedding.boost_python_import_hpp.examples">Examples</a></span></dt>
</dl></dd>
</dl>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="embedding.boost_python_exec_hpp"></a><a class="link" href="embedding.html#embedding.boost_python_exec_hpp" title="boost/python/exec.hpp">boost/python/exec.hpp</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="embedding.html#embedding.boost_python_exec_hpp.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="embedding.html#embedding.boost_python_exec_hpp.function_eval">Function
<code class="computeroutput"><span class="identifier">eval</span></code></a></span></dt>
<dt><span class="section"><a href="embedding.html#embedding.boost_python_exec_hpp.function_exec">Function
<code class="computeroutput"><span class="identifier">exec</span></code></a></span></dt>
<dt><span class="section"><a href="embedding.html#embedding.boost_python_exec_hpp.function_exec_file">Function
<code class="computeroutput"><span class="identifier">exec_file</span></code></a></span></dt>
<dt><span class="section"><a href="embedding.html#embedding.boost_python_exec_hpp.examples">Examples</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="embedding.boost_python_exec_hpp.introduction"></a><a class="link" href="embedding.html#embedding.boost_python_exec_hpp.introduction" title="Introduction">Introduction</a>
</h3></div></div></div>
<p>
Exposes a mechanism for embedding the python interpreter into C++ code.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="embedding.boost_python_exec_hpp.function_eval"></a><a class="link" href="embedding.html#embedding.boost_python_exec_hpp.function_eval" title="Function eval">Function
<code class="computeroutput"><span class="identifier">eval</span></code></a>
</h3></div></div></div>
<pre class="programlisting"><span class="identifier">object</span> <span class="identifier">eval</span><span class="special">(</span><span class="identifier">str</span> <span class="identifier">expression</span><span class="special">,</span>
<span class="identifier">object</span> <span class="identifier">globals</span> <span class="special">=</span> <span class="identifier">object</span><span class="special">(),</span>
<span class="identifier">object</span> <span class="identifier">locals</span> <span class="special">=</span> <span class="identifier">object</span><span class="special">());</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Effects</span></dt>
<dd><p>
Evaluate Python expression from expression in the context specified
by the dictionaries globals and locals.
</p></dd>
<dt><span class="term">Returns</span></dt>
<dd><p>
An instance of object which holds the value of the expression.
</p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="embedding.boost_python_exec_hpp.function_exec"></a><a class="link" href="embedding.html#embedding.boost_python_exec_hpp.function_exec" title="Function exec">Function
<code class="computeroutput"><span class="identifier">exec</span></code></a>
</h3></div></div></div>
<pre class="programlisting"><span class="identifier">object</span> <span class="identifier">exec</span><span class="special">(</span><span class="identifier">str</span> <span class="identifier">code</span><span class="special">,</span>
<span class="identifier">object</span> <span class="identifier">globals</span> <span class="special">=</span> <span class="identifier">object</span><span class="special">(),</span>
<span class="identifier">object</span> <span class="identifier">locals</span> <span class="special">=</span> <span class="identifier">object</span><span class="special">());</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Effects</span></dt>
<dd><p>
Execute Python source code from code in the context specified by
the dictionaries globals and locals.
</p></dd>
<dt><span class="term">Returns</span></dt>
<dd><p>
An instance of object which holds the result of executing the code.
</p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="embedding.boost_python_exec_hpp.function_exec_file"></a><a class="link" href="embedding.html#embedding.boost_python_exec_hpp.function_exec_file" title="Function exec_file">Function
<code class="computeroutput"><span class="identifier">exec_file</span></code></a>
</h3></div></div></div>
<pre class="programlisting"><span class="identifier">object</span> <span class="identifier">exec_file</span><span class="special">(</span><span class="identifier">str</span> <span class="identifier">filename</span><span class="special">,</span>
<span class="identifier">object</span> <span class="identifier">globals</span> <span class="special">=</span> <span class="identifier">object</span><span class="special">(),</span>
<span class="identifier">object</span> <span class="identifier">locals</span> <span class="special">=</span> <span class="identifier">object</span><span class="special">());</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Effects</span></dt>
<dd><p>
Execute Python source code from the file named by filename in the
context specified by the dictionaries globals and locals.
</p></dd>
<dt><span class="term">Returns</span></dt>
<dd><p>
An instance of object which holds the result of executing the code.
</p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="embedding.boost_python_exec_hpp.examples"></a><a class="link" href="embedding.html#embedding.boost_python_exec_hpp.examples" title="Examples">Examples</a>
</h3></div></div></div>
<p>
The following example demonstrates the use of import and exec to define
a function in python, and later call it from within C++.
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span>
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">string</span><span class="special">></span>
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span>
<span class="keyword">void</span> <span class="identifier">greet</span><span class="special">()</span>
<span class="special">{</span>
<span class="comment">// Retrieve the main module.</span>
<span class="identifier">object</span> <span class="identifier">main</span> <span class="special">=</span> <span class="identifier">import</span><span class="special">(</span><span class="string">"__main__"</span><span class="special">);</span>
<span class="comment">// Retrieve the main module's namespace</span>
<span class="identifier">object</span> <span class="identifier">global</span><span class="special">(</span><span class="identifier">main</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"__dict__"</span><span class="special">));</span>
<span class="comment">// Define greet function in Python.</span>
<span class="identifier">object</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">exec</span><span class="special">(</span>
<span class="string">"def greet(): \n"</span>
<span class="string">" return 'Hello from Python!' \n"</span><span class="special">,</span>
<span class="identifier">global</span><span class="special">,</span> <span class="identifier">global</span><span class="special">);</span>
<span class="comment">// Create a reference to it.</span>
<span class="identifier">object</span> <span class="identifier">greet</span> <span class="special">=</span> <span class="identifier">global</span><span class="special">[</span><span class="string">"greet"</span><span class="special">];</span>
<span class="comment">// Call it.</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">message</span> <span class="special">=</span> <span class="identifier">extract</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">>(</span><span class="identifier">greet</span><span class="special">());</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">message</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
Instead of embedding the python script into a string, we could also store
it in an a file...
</p>
<pre class="programlisting"><span class="identifier">def</span> <span class="identifier">greet</span><span class="special">():</span>
<span class="keyword">return</span> <span class="char">'Hello from Python!'</span>
</pre>
<p>
... and execute that instead.
</p>
<pre class="programlisting"> <span class="comment">// ...</span>
<span class="comment">// Load the greet function from a file.</span>
<span class="identifier">object</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">exec_file</span><span class="special">(</span><span class="identifier">script</span><span class="special">,</span> <span class="identifier">global</span><span class="special">,</span> <span class="identifier">global</span><span class="special">);</span>
<span class="comment">// ...</span>
<span class="special">}</span>
</pre>
</div>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="to_from_python_type_conversion/boost_python_register_ptr_to_pyt.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="embedding/boost_python_import_hpp.html"><img src="../images/next.png" alt="Next"></a>
</div>
</body>
</html>