2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-19 16:32:16 +00:00
Files
python/test/import_.cpp
Stefan Seefeld e61401d27e Fix import_ failure.
[SVN r37141]
2007-03-05 18:44:45 +00:00

66 lines
1.8 KiB
C++

// Copyright Stefan Seefeld 2007.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/python.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <iostream>
#include <sstream>
namespace bpl = boost::python;
void import_test()
{
// Retrieve the main module
bpl::object import_ = bpl::import("import_");
int value = bpl::extract<int>(import_.attr("value")) BOOST_EXTRACT_WORKAROUND;
std::cout << value << std::endl;
BOOST_TEST(value == 42);
}
int main(int argc, char **argv)
{
BOOST_TEST(argc == 2);
// Initialize the interpreter
Py_Initialize();
// Retrieve the main module
bpl::object main = bpl::import("__main__");
// Retrieve the main module's namespace
bpl::object global(main.attr("__dict__"));
// Inject search path for import_ module
std::ostringstream script;
script << "import sys, os.path\n"
<< "path = os.path.dirname('" << argv[1] << "')\n"
<< "sys.path.insert(0, path)\n";
bpl::object result = bpl::exec(bpl::str(script.str()), global, global);
if (bpl::handle_exception(import_test))
{
if (PyErr_Occurred())
{
BOOST_ERROR("Python Error detected");
PyErr_Print();
}
else
{
BOOST_ERROR("A C++ exception was thrown for which "
"there was no exception handler registered.");
}
}
// Boost.Python doesn't support Py_Finalize yet.
// Py_Finalize();
return boost::report_errors();
}
// Including this file makes sure
// that on Windows, any crashes (e.g. null pointer dereferences) invoke
// the debugger immediately, rather than being translated into structured
// exceptions that can interfere with debugging.
#include "module_tail.cpp"