Boost logo

Boost :

From: Stefan Seefeld (seefeld_at_[hidden])
Date: 2007-03-04 14:27:45


The attached patch appears to fix the problem with the import_ test.
Here is how it works:

* The py-run rule now allows an optional input-file argument.
* The import_ test invocation takes the import_.py file as argument,
  which bjam magically turns into a relative path.
* The import_.cpp code extracts that path, and injects it into the
  module search path, before attempting to 'import import_'.

Thanks to Volodya for helping me with the boost.build change.

OK to check in ?

Thanks,
                Stefan

-- 
      ...ich hab' noch einen Koffer in Berlin...

Index: libs/python/test/Jamfile.v2
===================================================================
RCS file: /cvsroot/boost/boost/libs/python/test/Jamfile.v2,v
retrieving revision 1.13.2.10
diff -u -r1.13.2.10 Jamfile.v2
--- libs/python/test/Jamfile.v2 1 Mar 2007 18:31:10 -0000 1.13.2.10
+++ libs/python/test/Jamfile.v2 4 Mar 2007 19:18:25 -0000
@@ -5,11 +5,11 @@
 use-project /boost/python : ../build ;
 project /boost/python/test ;
 
-rule py-run ( sources * )
+rule py-run ( sources * : input-file ? )
 {
     return [ run $(sources) /boost/python//boost_python /python//python
       : # args
- : # input files
+ : $(input-file)
       : #requirements
         <define>BOOST_PYTHON_SUPPRESS_REGISTRY_INITIALIZATION
  
@@ -150,7 +150,7 @@
     /boost/python//boost_python ]
 [ bpl-test
     map_indexing_suite : map_indexing_suite.py map_indexing_suite_ext ]
-[ py-run import_.cpp ]
+[ py-run import_.cpp : import_.py ]
 
 # if $(TEST_BIENSTMAN_NON_BUGS)
 # {
Index: libs/python/test/import_.cpp
===================================================================
RCS file: /cvsroot/boost/boost/libs/python/test/import_.cpp,v
retrieving revision 1.1.2.1
diff -u -r1.1.2.1 import_.cpp
--- libs/python/test/import_.cpp 1 Mar 2007 18:31:10 -0000 1.1.2.1
+++ libs/python/test/import_.cpp 4 Mar 2007 19:18:25 -0000
@@ -7,7 +7,7 @@
 
 #include <boost/detail/lightweight_test.hpp>
 #include <iostream>
-
+#include <sstream>
 
 namespace bpl = boost::python;
 
@@ -22,9 +22,23 @@
 
 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 list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk