1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
---|
2 | |
---|
3 | <!-- Copyright David Abrahams 2006. Distributed under the Boost --> |
---|
4 | <!-- Software License, Version 1.0. (See accompanying --> |
---|
5 | <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) --> |
---|
6 | <html> |
---|
7 | <head> |
---|
8 | <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
---|
9 | <link rel="stylesheet" type="text/css" href="../boost.css"> |
---|
10 | |
---|
11 | <title>Boost.Python - <boost/python/exec.hpp></title> |
---|
12 | </head> |
---|
13 | |
---|
14 | <body> |
---|
15 | <table border="0" cellpadding="7" cellspacing="0" width="100%" summary= |
---|
16 | "header"> |
---|
17 | <tr> |
---|
18 | <td valign="top" width="300"> |
---|
19 | <h3><a href="../../../../index.htm"><img height="86" width="277" |
---|
20 | alt="C++ Boost" src="../../../../boost.png" border="0"></a></h3> |
---|
21 | </td> |
---|
22 | |
---|
23 | <td valign="top"> |
---|
24 | <h1 align="center"><a href="../index.html">Boost.Python</a></h1> |
---|
25 | |
---|
26 | <h2 align="center">Header <boost/python/exec.hpp></h2> |
---|
27 | </td> |
---|
28 | </tr> |
---|
29 | </table> |
---|
30 | <hr> |
---|
31 | |
---|
32 | <h2>Contents</h2> |
---|
33 | |
---|
34 | <dl class="page-index"> |
---|
35 | <dt><a href="#introduction">Introduction</a></dt> |
---|
36 | |
---|
37 | <dt><a href="#functions">Functions</a></dt> |
---|
38 | |
---|
39 | <dd> |
---|
40 | <dl class="page-index"> |
---|
41 | <dt><a href="#eval-spec"><code>eval</code></a></dt> |
---|
42 | <dt><a href="#exec-spec"><code>exec</code></a></dt> |
---|
43 | <dt><a href="#exec_file-spec"><code>exec_file</code></a></dt> |
---|
44 | </dl> |
---|
45 | </dd> |
---|
46 | <dt><a href="#examples">Examples</a></dt> |
---|
47 | </dl> |
---|
48 | <hr> |
---|
49 | |
---|
50 | <h2><a name="introduction"></a>Introduction</h2> |
---|
51 | |
---|
52 | <p>Exposes a mechanism for embedding the python interpreter into C++ code.</p> |
---|
53 | |
---|
54 | <h2><a name="functions"></a>Functions</h2> |
---|
55 | |
---|
56 | <h3><a name="eval-spec"></a><code>eval</code></h3> |
---|
57 | <pre> |
---|
58 | object eval(str expression, |
---|
59 | object globals = object(), |
---|
60 | object locals = object()); |
---|
61 | </pre> |
---|
62 | <dl class="function-semantics"> |
---|
63 | <dt><b>Effects:</b> |
---|
64 | Evaluate Python expression from <code>expression</code> in the context |
---|
65 | specified by the dictionaries <code>globals</code> and <code>locals</code>. |
---|
66 | </dt> |
---|
67 | <dt><b>Returns:</b> |
---|
68 | An instance of <a href="object.html#object-spec">object</a> |
---|
69 | which holds the value of the expression. |
---|
70 | </dt> |
---|
71 | </dl> |
---|
72 | |
---|
73 | <h3><a name="exec-spec"></a><code>exec</code></h3> |
---|
74 | <pre> |
---|
75 | object exec(str code, |
---|
76 | object globals = object(), |
---|
77 | object locals = object()); |
---|
78 | </pre> |
---|
79 | <dl class="function-semantics"> |
---|
80 | <dt><b>Effects:</b> |
---|
81 | Execute Python source code from <code>code</code> in the context |
---|
82 | specified by the dictionaries <code>globals</code> and <code>locals</code>. |
---|
83 | </dt> |
---|
84 | <dt><b>Returns:</b> |
---|
85 | An instance of <a href="object.html#object-spec">object</a> |
---|
86 | which holds the result of executing the code. |
---|
87 | </dt> |
---|
88 | </dl> |
---|
89 | |
---|
90 | <h3><a name="exec_file-spec"></a><code>exec_file</code></h3> |
---|
91 | <pre> |
---|
92 | object exec_file(str filename, |
---|
93 | object globals = object(), |
---|
94 | object locals = object()); |
---|
95 | </pre> |
---|
96 | <dl class="function-semantics"> |
---|
97 | <dt><b>Effects:</b> |
---|
98 | Execute Python source code from the file named by <code>filename</code> |
---|
99 | in the context specified by the dictionaries <code>globals</code> and |
---|
100 | <code>locals</code>. |
---|
101 | </dt> |
---|
102 | <dt><b>Returns:</b> |
---|
103 | An instance of <a href="object.html#object-spec">object</a> |
---|
104 | which holds the result of executing the code. |
---|
105 | </dt> |
---|
106 | </dl> |
---|
107 | |
---|
108 | <h2><a name="examples"></a>Examples</h2> |
---|
109 | |
---|
110 | <para>The following example demonstrates the use of <function>import</function> |
---|
111 | and <function>exec</function> to define a function in python, and later call |
---|
112 | it from within C++.</para> |
---|
113 | |
---|
114 | <pre> |
---|
115 | #include <iostream> |
---|
116 | #include <string> |
---|
117 | |
---|
118 | using namespace boost::python; |
---|
119 | |
---|
120 | void greet() |
---|
121 | { |
---|
122 | // Retrieve the main module. |
---|
123 | object main = import("__main__"); |
---|
124 | |
---|
125 | // Retrieve the main module's namespace |
---|
126 | object global(main.attr("__dict__")); |
---|
127 | |
---|
128 | // Define greet function in Python. |
---|
129 | object result = exec( |
---|
130 | "def greet(self): \n" |
---|
131 | " return 'Hello from Python!' \n", |
---|
132 | global, global); |
---|
133 | |
---|
134 | // Create a reference to it. |
---|
135 | object greet = global["greet"]; |
---|
136 | |
---|
137 | // Call it. |
---|
138 | std::string message = extract<std::string>(greet()); |
---|
139 | std::cout << message << std::endl; |
---|
140 | } |
---|
141 | </pre> |
---|
142 | |
---|
143 | <para>Instead of embedding the python script into a string, |
---|
144 | we could also store it in an a file...</para> |
---|
145 | |
---|
146 | <pre> |
---|
147 | def greet(self): |
---|
148 | return 'Hello from Python!' |
---|
149 | </pre> |
---|
150 | <para>... and execute that instead.</para> |
---|
151 | <pre> |
---|
152 | // ... |
---|
153 | // Load the greet function from a file. |
---|
154 | object result = exec_file(script, global, global); |
---|
155 | // ... |
---|
156 | } |
---|
157 | </pre> |
---|
158 | <p>Revised 01 November, 2005</p> |
---|
159 | |
---|
160 | <p><i>© Copyright Stefan Seefeld 2005.</i></p> |
---|
161 | </body> |
---|
162 | </html> |
---|
163 | |
---|