In a boost python embedding in C++, I have C++ parsing a python (via boost-python) file containing a simple function, which in turn calls a C++ method to complete a certain implementation. While these seems ridiculous, we choose to do it for the advantages of logging and the other flexibilities that python offers.
I’ve noticed that on Linux, if the python file containing the function to be implemented was edited in Windows and thus has the annoying carriage return character (^M) at the end of each line, boost python fails to parse it with a syntax error. Of course, running dos2unix to strip the ^M characters from the python file resolves this issue. A snippet of my boost-python invocation in C++ is as follows, if it helps:
bool exec_command(const std::string& cmd, ...) {
...
...
try {
boost::python::object main = boost::python::import("__main__");
boost::python::object global(main.attr("__dict__"));
if( !context.empty() ) {
boost::python::exec(
"import _project",
global,
global
);
for(smart_handle_context::const_iterator itr = handle_context.begin(); itr != hndle_context.end(); ++itr) {
global[itr->first] = boost::python::object(itr->second);
}
}
boost::python::exec(
cmd.c_str(),
global,
global
);
}
catch( boost::python::error_already_set& ) {
PyErr_Print();
return false;
}
return true;
}
In the above snippet, smart_handle_context is a typedef of a map of std::strings to an implementation specific smart handle. I’ve noted also that running python directly on a file with ^M characters in Linux poses no problems to it’s parser. Any ideas on why how to get around the ^M issue without having to run the dos2unix workaround (a fix in the code hopefully) is appreciated. Thanks.
You could define a function which removes 0x0D character from the string, something like: