From 50034140c45928b130e59a76a497894ad6876664 Mon Sep 17 00:00:00 2001 From: Stefan Seefeld Date: Thu, 1 Mar 2007 15:17:29 +0000 Subject: [PATCH] Fix boost::python::import. [SVN r37120] --- src/import.cpp | 2 +- test/Jamfile.v2 | 1 + test/import_.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ test/import_.py | 1 + 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 test/import_.cpp create mode 100644 test/import_.py diff --git a/src/import.cpp b/src/import.cpp index 9686ab2f..1a066dfb 100644 --- a/src/import.cpp +++ b/src/import.cpp @@ -17,7 +17,7 @@ object BOOST_PYTHON_DECL import(str name) { // should be 'char const *' but older python versions don't use 'const' yet. char *n = python::extract(name); - python::handle<> module(python::borrowed(PyImport_AddModule(n))); + python::handle<> module(python::borrowed(PyImport_ImportModule(n))); return python::object(module); } diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 5b81cabd..9ed7ae0a 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -151,6 +151,7 @@ bpl-test crossmod_opaque /boost/python//boost_python ] [ bpl-test map_indexing_suite : map_indexing_suite.py map_indexing_suite_ext ] +[ py-run import_.cpp ] # if $(TEST_BIENSTMAN_NON_BUGS) # { diff --git a/test/import_.cpp b/test/import_.cpp new file mode 100644 index 00000000..86ca2d70 --- /dev/null +++ b/test/import_.cpp @@ -0,0 +1,51 @@ +// 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 + +#include +#include + + +namespace bpl = boost::python; + +void import_test() +{ + // Retrieve the main module + bpl::object import_ = bpl::import("import_"); + int value = bpl::extract(import_.attr("value")) BOOST_EXTRACT_WORKAROUND; + std::cout << value << std::endl; + BOOST_TEST(value == 42); +} + +int main(int argc, char **argv) +{ + // Initialize the interpreter + Py_Initialize(); + + 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" diff --git a/test/import_.py b/test/import_.py new file mode 100644 index 00000000..67f8b2c1 --- /dev/null +++ b/test/import_.py @@ -0,0 +1 @@ +value = 42