Skip to content

Commit 126a3ef

Browse files
committed
Add basic embedding support.
[SVN r30601]
1 parent 9205f50 commit 126a3ef

14 files changed

Lines changed: 462 additions & 199 deletions

File tree

build/Jamfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ if [ check-python-config ]
5454
object_protocol.cpp
5555
object_operators.cpp
5656
wrapper.cpp
57+
exec.cpp
58+
import.cpp
5759
;
5860

5961
dll boost_python

doc/v2/exec.html

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2+
3+
<html>
4+
<head>
5+
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
6+
<link rel="stylesheet" type="text/css" href="../boost.css">
7+
8+
<title>Boost.Python - &lt;boost/python/exec.hpp&gt;</title>
9+
</head>
10+
11+
<body>
12+
<table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
13+
"header">
14+
<tr>
15+
<td valign="top" width="300">
16+
<h3><a href="../../../../index.htm"><img height="86" width="277"
17+
alt="C++ Boost" src="../../../../boost.png" border="0"></a></h3>
18+
</td>
19+
20+
<td valign="top">
21+
<h1 align="center"><a href="../index.html">Boost.Python</a></h1>
22+
23+
<h2 align="center">Header &lt;boost/python/exec.hpp&gt;</h2>
24+
</td>
25+
</tr>
26+
</table>
27+
<hr>
28+
29+
<h2>Contents</h2>
30+
31+
<dl class="page-index">
32+
<dt><a href="#introduction">Introduction</a></dt>
33+
34+
<dt><a href="#functions">Functions</a></dt>
35+
36+
<dd>
37+
<dl class="page-index">
38+
<dt><a href="#exec-spec"><code>exec</code></a></dt>
39+
<dt><a href="#exec_file-spec"><code>exec_file</code></a></dt>
40+
</dl>
41+
</dd>
42+
</dl>
43+
<hr>
44+
45+
<h2><a name="introduction"></a>Introduction</h2>
46+
47+
<p>Exposes a mechanism for embedding the python interpreter into C++ code.</p>
48+
49+
<h2><a name="functions"></a>Functions</h2>
50+
51+
<h3><a name="exec-spec"></a><code>exec</code></h3>
52+
<pre>
53+
object exec(str code,
54+
object globals = object(),
55+
object locals = object());
56+
</pre>
57+
<dl class="function-semantics">
58+
<dt><b>Effects:</b>
59+
Execute Python source code from <code>code</code> in the context
60+
specified by the dictionaries <code>globals</code> and <code>locals</code>.
61+
</dt>
62+
<dt><b>Returns:</b>
63+
An instance of <a href="object.html#object-spec">object</a>
64+
which holds the result of executing the code.
65+
</dt>
66+
</dl>
67+
68+
<h3><a name="exec_file-spec"></a><code>exec_file</code></h3>
69+
<pre>
70+
object exec_file(str filename,
71+
object globals = object(),
72+
object locals = object());
73+
</pre>
74+
<dl class="function-semantics">
75+
<dt><b>Effects:</b>
76+
Execute Python source code from the file named by <code>filename</code>
77+
in the context specified by the dictionaries <code>globals</code> and
78+
<code>locals</code>.
79+
</dt>
80+
<dt><b>Returns:</b>
81+
An instance of <a href="object.html#object-spec">object</a>
82+
which holds the result of executing the code.
83+
</dt>
84+
</dl>
85+
86+
</body>
87+
</html>
88+

doc/v2/import.html

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2+
3+
<html>
4+
<head>
5+
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
6+
<link rel="stylesheet" type="text/css" href="../boost.css">
7+
8+
<title>Boost.Python - &lt;boost/python/import.hpp&gt;</title>
9+
</head>
10+
11+
<body>
12+
<table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
13+
"header">
14+
<tr>
15+
<td valign="top" width="300">
16+
<h3><a href="../../../../index.htm"><img height="86" width="277"
17+
alt="C++ Boost" src="../../../../boost.png" border="0"></a></h3>
18+
</td>
19+
20+
<td valign="top">
21+
<h1 align="center"><a href="../index.html">Boost.Python</a></h1>
22+
23+
<h2 align="center">Header &lt;boost/python/import.hpp&gt;</h2>
24+
</td>
25+
</tr>
26+
</table>
27+
<hr>
28+
29+
<h2>Contents</h2>
30+
31+
<dl class="page-index">
32+
<dt><a href="#introduction">Introduction</a></dt>
33+
34+
<dt><a href="#functions">Functions</a></dt>
35+
36+
<dd>
37+
<dl class="page-index">
38+
<dt><a href="#import-spec"><code>import</code></a></dt>
39+
</dl>
40+
</dd>
41+
</dl>
42+
<hr>
43+
44+
<h2><a name="introduction"></a>Introduction</h2>
45+
46+
<p>Exposes a mechanism for importing python modules.</p>
47+
48+
<h2><a name="functions"></a>Functions</h2>
49+
50+
<h3><a name="import-spec"></a><code>import</code></h3>
51+
<pre>
52+
object import(str name);
53+
</pre>
54+
<dl class="function-semantics">
55+
<dt><b>Effects:</b> Imports the module named by <code>name</code>.</dt>
56+
<dt><b>Returns:</b> An instance of <a href="object.html#object-spec">object</a>
57+
which holds a reference to the imported module.</dt>
58+
</dl>
59+
60+
</body>
61+
</html>
62+

doc/v2/reference.html

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ <h2>Contents</h2>
6060

6161
<dt><a href="#type_conversion">To/From Python Type Conversion</a></dt>
6262

63+
<dt><a href="#embedding">Embedding</a></dt>
64+
6365
<dt><a href="#utility">Utility and Infrastructure</a></dt>
6466

6567
<dt><a href="#topics">Topics</a></dt>
@@ -939,6 +941,39 @@ <h2><a name="type_conversion">To/From Python Type Conversion</a></h2>
939941
</dd>
940942
</dl>
941943

944+
<h2><a name="embedding">Embedding</a></h2>
945+
946+
<dl class="index">
947+
<dt><a href="exec.html">exec.hpp</a></dt>
948+
949+
<dd>
950+
<dl class="index">
951+
<dt><a href="exec.html#functions">Functions</a></dt>
952+
953+
<dd>
954+
<dl class="index">
955+
<dt><a href="exec.html#exec-spec">exec</a></dt>
956+
<dt><a href="exec.html#exec_file-spec">exec_file</a></dt>
957+
</dl>
958+
</dd>
959+
</dl>
960+
</dd>
961+
962+
<dt><a href="import.html">import.hpp</a></dt>
963+
964+
<dd>
965+
<dl class="index">
966+
<dt><a href="import.html#functions">Functions</a></dt>
967+
968+
<dd>
969+
<dl class="index">
970+
<dt><a href="import.html#import-spec">import</a></dt>
971+
</dl>
972+
</dd>
973+
</dl>
974+
</dd>
975+
</dl>
976+
942977
<h2><a name="utility">Utility and Infrastructure</a></h2>
943978

944979
<dl>

include/boost/python.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
# include <boost/python/enum.hpp>
2626
# include <boost/python/errors.hpp>
2727
# include <boost/python/exception_translator.hpp>
28+
# include <boost/python/exec.hpp>
2829
# include <boost/python/extract.hpp>
2930
# include <boost/python/handle.hpp>
3031
# include <boost/python/has_back_reference.hpp>
3132
# include <boost/python/implicit.hpp>
3233
# include <boost/python/init.hpp>
34+
# include <boost/python/import.hpp>
3335
# include <boost/python/instance_holder.hpp>
3436
# include <boost/python/iterator.hpp>
3537
# include <boost/python/list.hpp>

include/boost/python/exec.hpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright Stefan Seefeld 2005.
2+
// Distributed under the Boost Software License, Version 1.0. (See
3+
// accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
#ifndef EXEC_SS20050616_HPP
6+
# define EXEC_SS20050616_HPP
7+
8+
# include <boost/python/object.hpp>
9+
# include <boost/python/str.hpp>
10+
11+
namespace boost
12+
{
13+
namespace python
14+
{
15+
16+
// Execute python source code from str.
17+
// global and local are the global and local scopes respectively,
18+
// used during execution.
19+
object
20+
exec(str string, object global = object(), object local = object());
21+
22+
// Execute python source code from file filename.
23+
// global and local are the global and local scopes respectively,
24+
// used during execution.
25+
object
26+
exec_file(str filename, object global = object(), object local = object());
27+
28+
}
29+
}
30+
31+
#endif

include/boost/python/extract.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
# include <boost/utility.hpp>
2323
# include <boost/call_traits.hpp>
2424

25+
#if BOOST_WORKAROUND(BOOST_MSVC, <= 1300) || BOOST_WORKAROUND(BOOST_INTEL_WIN, <= 900)
26+
// workaround for VC++ 6.x or 7.0
27+
# define BOOST_EXTRACT_WORKAROUND ()
28+
#else
29+
# define BOOST_EXTRACT_WORKAROUND
30+
#endif
31+
2532
namespace boost { namespace python {
2633

2734
namespace api

include/boost/python/import.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright Stefan Seefeld 2005.
2+
// Distributed under the Boost Software License, Version 1.0. (See
3+
// accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
#ifndef IMPORT_SS20050624_HPP
6+
# define IMPORT_SS20050624_HPP
7+
8+
# include <boost/python/object.hpp>
9+
# include <boost/python/str.hpp>
10+
11+
namespace boost
12+
{
13+
namespace python
14+
{
15+
16+
// Import the named module and return a reference to it.
17+
object import(str name);
18+
19+
}
20+
}
21+
22+
#endif

src/exec.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright Stefan Seefeld 2005.
2+
// Distributed under the Boost Software License, Version 1.0. (See
3+
// accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#include <boost/python/exec.hpp>
7+
#include <boost/python/borrowed.hpp>
8+
#include <boost/python/extract.hpp>
9+
#include <boost/python/handle.hpp>
10+
11+
namespace boost
12+
{
13+
namespace python
14+
{
15+
16+
object exec(str string, object global, object local)
17+
{
18+
// should be 'char const *' but older python versions don't use 'const' yet.
19+
char *s = python::extract<char *>(string);
20+
PyObject* result = PyRun_String(s, Py_file_input, global.ptr(), local.ptr());
21+
if (!result) throw_error_already_set();
22+
return object(detail::new_reference(result));
23+
}
24+
25+
// Execute python source code from file filename.
26+
// global and local are the global and local scopes respectively,
27+
// used during execution.
28+
object exec_file(str filename, object global, object local)
29+
{
30+
// should be 'char const *' but older python versions don't use 'const' yet.
31+
char *f = python::extract<char *>(filename);
32+
// Let python open the file to avoid potential binary incompatibilities.
33+
PyObject *pyfile = PyFile_FromString(f, "r");
34+
if (!pyfile) throw std::invalid_argument(std::string(f) + " : no such file");
35+
python::handle<> file(pyfile);
36+
PyObject* result = PyRun_File(PyFile_AsFile(file.get()),
37+
f,
38+
Py_file_input,
39+
global.ptr(), local.ptr());
40+
if (!result) throw_error_already_set();
41+
return object(detail::new_reference(result));
42+
}
43+
44+
} // namespace boost::python
45+
} // namespace boost

src/import.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright Stefan Seefeld 2005.
2+
// Distributed under the Boost Software License, Version 1.0. (See
3+
// accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#include <boost/python/import.hpp>
7+
#include <boost/python/borrowed.hpp>
8+
#include <boost/python/extract.hpp>
9+
#include <boost/python/handle.hpp>
10+
11+
namespace boost
12+
{
13+
namespace python
14+
{
15+
16+
object import(str name)
17+
{
18+
// should be 'char const *' but older python versions don't use 'const' yet.
19+
char *n = python::extract<char *>(name);
20+
python::handle<> module(python::borrowed(PyImport_AddModule(n)));
21+
return python::object(module);
22+
}
23+
24+
} // namespace boost::python
25+
} // namespace boost

0 commit comments

Comments
 (0)