|
Boost : |
From: Stefan Seefeld (seefeld_at_[hidden])
Date: 2007-02-28 21:33:40
The attached patch fixes the boost::python::import implementation.
It used to work fine for modules already ('physically') imported
into the process, but not for new ones.
I'd like to apply to HEAD, and would like to ask whether it is OK
for the R_1_34 branch, too.
The problem is that the 'import' function is new (well, it's
actually two years old, but not in any official release), and
no unit test exists. Thus, part of this patch is a unit test
that demonstrates the failure on the existing implementation,
as well as the fix.
OK to commit to HEAD and R_1_34 ?
Thanks,
Stefan
-- ...ich hab' noch einen Koffer in Berlin...
Index: libs/python/src/import.cpp
===================================================================
RCS file: /cvsroot/boost/boost/libs/python/src/import.cpp,v
retrieving revision 1.2
diff -u -r1.2 import.cpp
--- libs/python/src/import.cpp 27 Oct 2006 21:17:25 -0000 1.2
+++ libs/python/src/import.cpp 1 Mar 2007 02:00:47 -0000
@@ -17,7 +17,7 @@
{
// should be 'char const *' but older python versions don't use 'const' yet.
char *n = python::extract<char *>(name);
- python::handle<> module(python::borrowed(PyImport_AddModule(n)));
+ python::handle<> module(python::borrowed(PyImport_ImportModule(n)));
return python::object(module);
}
Index: libs/python/test/Jamfile.v2
===================================================================
RCS file: /cvsroot/boost/boost/libs/python/test/Jamfile.v2,v
retrieving revision 1.22
diff -u -r1.22 Jamfile.v2
--- libs/python/test/Jamfile.v2 13 Oct 2006 21:33:37 -0000 1.22
+++ libs/python/test/Jamfile.v2 1 Mar 2007 02:00:48 -0000
@@ -151,6 +151,7 @@
/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)
# {
Index: libs/python/test/import_.cpp
===================================================================
RCS file: libs/python/test/import_.cpp
diff -N libs/python/test/import_.cpp
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ libs/python/test/import_.cpp 1 Mar 2007 02:00:48 -0000
@@ -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 <boost/python.hpp>
+
+#include <boost/detail/lightweight_test.hpp>
+#include <iostream>
+
+
+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)
+{
+ // 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"
Index: libs/python/test/import_.py
===================================================================
RCS file: libs/python/test/import_.py
diff -N libs/python/test/import_.py
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ libs/python/test/import_.py 1 Mar 2007 02:00:48 -0000
@@ -0,0 +1 @@
+value = 42
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk