Skip to content

Commit 6afe0d4

Browse files
committed
Enhance and clean up tests.
[SVN r30907]
1 parent f8280b0 commit 6afe0d4

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

test/exec.cpp

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
namespace python = boost::python;
1010

11-
std::string script;
12-
1311
// An abstract base class
1412
class Base : public boost::noncopyable
1513
{
@@ -72,46 +70,52 @@ void exec_test()
7270

7371
// Creating and using instances of the C++ class is as easy as always.
7472
CppDerived cpp;
75-
std::cout << cpp.hello() << std::endl;
73+
if (cpp.hello() != "Hello from C++!")
74+
throw std::runtime_error("cpp.hello() returned unexpected string");
7675

7776
// But now creating and using instances of the Python class is almost
7877
// as easy!
7978
python::object py_base = PythonDerived();
8079
Base& py = python::extract<Base&>(py_base) BOOST_EXTRACT_WORKAROUND;
81-
std::cout << py.hello() << std::endl;
80+
81+
// Make sure the right 'hello' method is called.
82+
if (py.hello() != "Hello from Python!")
83+
throw std::runtime_error("py.hello() returned unexpected string");
8284
}
8385

84-
// void exec_file_test(std::string const &script)
85-
void exec_file_test()
86+
void exec_file_test(std::string const &script)
8687
{
87-
python::object main = python::import("__main__");
88-
python::dict global(main.attr("__dict__"));
89-
global.clear();
88+
// Run a python script in an empty environment.
89+
python::dict global;
9090
python::object result = python::exec_file(script.c_str(), global, global);
91-
std::string global_as_string = python::extract<std::string>(python::str(global))
92-
BOOST_EXTRACT_WORKAROUND;
93-
std::cout << global_as_string << std::endl;
91+
92+
// Extract an object the script stored in the global dictionary.
93+
if (python::extract<int>(global["number"]) != 42)
94+
throw std::runtime_error("'number' has unexpected value");
9495
}
9596

9697
void exec_test_error()
9798
{
98-
python::object main = python::import("__main__");
99-
python::dict global(main.attr("__dict__"));
99+
// Execute a statement that raises a python exception.
100+
python::dict global;
100101
python::object result = python::exec("print unknown \n", global, global);
101102
}
102103

103104
int main(int argc, char **argv)
104105
{
105106
assert(argc == 2);
106-
script = argv[1];
107+
std::string script = argv[1];
107108
bool success = true;
108109
// Initialize the interpreter
109110
Py_Initialize();
110111

111112
if (python::handle_exception(exec_test) ||
112-
python::handle_exception(exec_file_test))
113+
python::handle_exception(boost::bind(exec_file_test, script)))
113114
{
114-
if (PyErr_Occurred()) PyErr_Print();
115+
if (PyErr_Occurred())
116+
{
117+
PyErr_Print();
118+
}
115119
else
116120
{
117121
std::cerr << "A C++ exception was thrown for which "
@@ -121,16 +125,21 @@ int main(int argc, char **argv)
121125
}
122126
if (python::handle_exception(exec_test_error))
123127
{
124-
if (PyErr_Occurred()) PyErr_Print();
128+
if (PyErr_Occurred())
129+
{
130+
PyErr_Print();
131+
}
125132
else
126133
{
127134
std::cerr << "A C++ exception was thrown for which "
128135
<< "there was no exception handler registered." << std::endl;
129136
success = false;
130137
}
131138
}
132-
else success = false;
133-
139+
else
140+
{
141+
success = false;
142+
}
134143
// Boost.Python doesn't support Py_Finalize yet.
135144
// Py_Finalize();
136145
return success ? 0 : 1;

0 commit comments

Comments
 (0)