From cfe6f96f69d28a5117258543fad3192249fa7887 Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Sun, 28 Oct 2007 19:22:21 +0000 Subject: [PATCH] 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. [SVN r40535] --- test/import_.cpp | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/test/import_.cpp b/test/import_.cpp index f50a0089..7b4e5d1f 100644 --- a/test/import_.cpp +++ b/test/import_.cpp @@ -6,13 +6,32 @@ #include #include +#include #include #include +#include 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(import_.attr("value")) BOOST_EXTRACT_WORKAROUND; @@ -27,19 +46,7 @@ int main(int argc, char **argv) // 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()) {