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