Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7715801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:28:41+00:00 2026-06-01T02:28:41+00:00

In a boost python embedding in C++, I have C++ parsing a python (via

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:28:43+00:00Added an answer on June 1, 2026 at 2:28 am

    You could define a function which removes 0x0D character from the string, something like:

    #include <algorithm>
    
    std::string removeWindowsLineEndings(std::string s)
    {
        s.erase(std::remove(s.begin(), s.end(), '\x0D'), s.end());
    
        return s;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program which embeds Python (via Boost.Python), and provides an 'extension' framework.
I have a function that takes an int-pointer and exposed it via boost::python. How
I have a C++ value type wrapped with Boost.Python which has a concept of
I have some boost python classes, which I instantiate in python. I want to
I'm using boost python. I've exported some function which takes class CL_DomElement in arguments.
I have a simple library written in C++ which I'm creating a Python wrapper
I have started to play around with boost python a bit and ran into
Trying to build the following simple example #include <boost/python.hpp> using namespace boost::python; tuple head_and_tail(object
How can I construct a boost::python::object from a std::function ?
I'm trying to use Boost.Python as a wrapper for a C++ function that receives

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.