mirror of
https://github.com/boostorg/python.git
synced 2026-01-21 17:12:22 +00:00
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]
This commit is contained in:
@@ -6,13 +6,32 @@
|
||||
#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;
|
||||
@@ -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())
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user