Boost logo

Boost-Commit :

From: dave_at_[hidden]
Date: 2007-10-28 15:22:21


Author: dave
Date: 2007-10-28 15:22:21 EDT (Sun, 28 Oct 2007)
New Revision: 40535
URL: http://svn.boost.org/trac/boost/changeset/40535

Log:
Closes #1379, really this time. The old code would sandwich argv[1] between quotes and interpret it as a string, so backslashes in windows paths were interpreted as escape sequences.

Text files modified:
   trunk/libs/python/test/import_.cpp | 35 +++++++++++++++++++++--------------
   1 files changed, 21 insertions(+), 14 deletions(-)

Modified: trunk/libs/python/test/import_.cpp
==============================================================================
--- trunk/libs/python/test/import_.cpp (original)
+++ trunk/libs/python/test/import_.cpp 2007-10-28 15:22:21 EDT (Sun, 28 Oct 2007)
@@ -6,14 +6,33 @@
 #include <boost/python.hpp>
 
 #include <boost/detail/lightweight_test.hpp>
+#include <boost/bind.hpp>
 #include <iostream>
 #include <sstream>
+#include <stdlib.h>
 
 namespace bpl = boost::python;
 
-void import_test()
+void import_test(char const *py_file_path)
 {
   // 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
+
+ bpl::str script(
+ "import sys, os.path\n"
+ "path = os.path.dirname(%r)\n"
+ "sys.path.insert(0, path)\n"
+ "print 'sys.path=',sys.path"
+ % bpl::str(py_file_path));
+
+ bpl::object result = bpl::exec(script, global, global);
+
+ // 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;
@@ -27,19 +46,7 @@
   // 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 (bpl::handle_exception(boost::bind(import_test,argv[1])))
   {
     if (PyErr_Occurred())
     {


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk