Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/python/doc/v2/exec.html @ 29

Last change on this file since 29 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 4.7 KB
Line 
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 - &lt;boost/python/exec.hpp&gt;</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 &lt;boost/python/exec.hpp&gt;</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>
58object 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>
75object 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>
92object 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 &lt;iostream&gt;
116#include &lt;string&gt;
117
118using namespace boost::python;
119
120void 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&lt;std::string&gt;(greet());
139  std::cout &lt;&lt; message &lt;&lt; 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>
147def 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>&copy; Copyright Stefan Seefeld 2005.</i></p>
161  </body>
162</html>
163
Note: See TracBrowser for help on using the repository browser.