Skip to content

Commit e61401d

Browse files
committed
Fix import_ failure.
[SVN r37141]
1 parent be7ca7d commit e61401d

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

test/Jamfile.v2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
use-project /boost/python : ../build ;
66
project /boost/python/test ;
77

8-
rule py-run ( sources * )
8+
rule py-run ( sources * : input-file ? )
99
{
1010
return [ run $(sources) /boost/python//boost_python /python//python
1111
: # args
12-
: # input files
12+
: $(input-file)
1313
: #requirements
1414
<define>BOOST_PYTHON_SUPPRESS_REGISTRY_INITIALIZATION
1515

@@ -150,7 +150,7 @@ bpl-test crossmod_opaque
150150
/boost/python//boost_python ]
151151
[ bpl-test
152152
map_indexing_suite : map_indexing_suite.py map_indexing_suite_ext ]
153-
[ py-run import_.cpp ]
153+
[ py-run import_.cpp : import_.py ]
154154

155155
# if $(TEST_BIENSTMAN_NON_BUGS)
156156
# {

test/import_.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include <boost/detail/lightweight_test.hpp>
99
#include <iostream>
10-
10+
#include <sstream>
1111

1212
namespace bpl = boost::python;
1313

@@ -22,9 +22,23 @@ void import_test()
2222

2323
int main(int argc, char **argv)
2424
{
25+
BOOST_TEST(argc == 2);
26+
2527
// Initialize the interpreter
2628
Py_Initialize();
29+
30+
// Retrieve the main module
31+
bpl::object main = bpl::import("__main__");
32+
33+
// Retrieve the main module's namespace
34+
bpl::object global(main.attr("__dict__"));
2735

36+
// Inject search path for import_ module
37+
std::ostringstream script;
38+
script << "import sys, os.path\n"
39+
<< "path = os.path.dirname('" << argv[1] << "')\n"
40+
<< "sys.path.insert(0, path)\n";
41+
bpl::object result = bpl::exec(bpl::str(script.str()), global, global);
2842
if (bpl::handle_exception(import_test))
2943
{
3044
if (PyErr_Occurred())

0 commit comments

Comments
 (0)