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 3451838
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:11:19+00:00 2026-05-18T09:11:19+00:00

I have a program which during it’s run sometimes needs to call python in

  • 0

I have a program which during it’s run sometimes needs to call python in order to preform some tasks. I need a function that calls python and catches pythons stdout and puts it in some file.
This is a declaration of the function

  pythonCallBackFunc(const char* pythonInput)

My problem is to catch all the python output for a given command (pythonInput).
I have no experience with python API and I don’t know what is the right technique to do this.
First thing I’ve tried is to redirect python’s sdtout and stderr using Py_run_SimpleString
this is some example of the code i’ve written.

#include "boost\python.hpp"
#include <iostream>

void pythonCallBackFunc(const char* inputStr){   

    PyRun_SimpleString(inputStr); 
}


int main () {
    ...
   //S0me outside functions does this
   Py_Initialize();
   PyRun_SimpleString("import sys");
   PyRun_SimpleString("old_stdout = sys.stdout");
   PyRun_SimpleString("fsock = open('python_out.log','a')");
   PyRun_SimpleString("sys.stdout = fsock");
   ...

   //my func   
   pythonCallBackFunc("print 'HAHAHAHAHA'");
   pythonCallBackFunc("result = 5");
   pythonCallBackFunc("print result");

   pythonCallBackFunc("result = 'Hello '+'World!'");
   pythonCallBackFunc("print result");

   pythonCallBackFunc("'KUKU '+'KAKA'");
   pythonCallBackFunc("5**3");

   pythonCallBackFunc("prinhghult");

   pythonCallBackFunc("execfile('stdout_close.py')");
   ... 

   //Again anothers function code
   PyRun_SimpleString("sys.stdout = old_stdout");
   PyRun_SimpleString("fsock.close()");

   Py_Finalize();
   return 0;
}

Is there a better way to do this? Besides, for some reason PyRun_SimpleString does nothing when it gets some mathematical expression, for example PyRun_SimpleString(“5**3”) prints nothing (python conlsul prints the result: 125)

maybe it is important, i am using visual studio 2008.
Thanks,
Alex


Changes I’ve made according Mark’s suggestion:

  #include <python.h>
  #include <string>

  using namespace std;

  void PythonPrinting(string inputStr){ 
     string stdOutErr =
    "import sys\n\
     class CatchOut:\n\
        def __init__(self):\n\
           self.value = ''\n\
        def write(self, txt):\n\
           self.value += txt\n\
     catchOut = CatchOut()\n\
     sys.stdout = catchOut\n\
     sys.stderr = catchOut\n\
    "; //this is python code to redirect stdouts/stderr

     PyObject *pModule = PyImport_AddModule("__main__"); //create main module
     PyRun_SimpleString(stdOutErr.c_str()); //invoke code to redirect

     PyRun_SimpleString(inputStr.c_str());
     PyObject *catcher = PyObject_GetAttrString(pModule,"catchOut");

     PyObject *output = PyObject_GetAttrString(catcher,"value");
     printf("Here's the output: %s\n", PyString_AsString(output)); 
     }

  int main(int argc, char** argv){
         Py_Initialize();

     PythonPrinting("print 123");
     PythonPrinting("1+5");
     PythonPrinting("result = 2");
         PythonPrinting("print result");

         Py_Finalize();
         return 0;
  }

The output i get after running main:

 Here's the output: 123

 Here's the output:
 Here's the output: 
 Here's the output: 2

It is good for me , but only one problem, it should be

 Here's the output: 123

 Here's the output: 6

 Here's the output: 
 Here's the output: 2

I dont know why but after running this command: PythonPrinting(“1+5”), PyString_AsString(output) command returns an empty string (char*) instead of 6… 🙁 Is there somthing i can do not to loose this output?

Thaks,
Alex

  • 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-05-18T09:11:20+00:00Added an answer on May 18, 2026 at 9:11 am

    If I’m reading your question correctly, you want to capture stdout/stderr into a variable within your C++? You can do this by redirecting stdout/stderr into a python variable and then querying this variable into your C++. Please not that I have not done the proper ref counting below:

    #include <Python.h>
    #include <string>
    
    int main(int argc, char** argv)
    {
        std::string stdOutErr =
    "import sys\n\
    class CatchOutErr:\n\
        def __init__(self):\n\
            self.value = ''\n\
        def write(self, txt):\n\
            self.value += txt\n\
    catchOutErr = CatchOutErr()\n\
    sys.stdout = catchOutErr\n\
    sys.stderr = catchOutErr\n\
    "; //this is python code to redirect stdouts/stderr
    
        Py_Initialize();
        PyObject *pModule = PyImport_AddModule("__main__"); //create main module
        PyRun_SimpleString(stdOutErr.c_str()); //invoke code to redirect
        PyRun_SimpleString("print(1+1)"); //this is ok stdout
        PyRun_SimpleString("1+a"); //this creates an error
        PyObject *catcher = PyObject_GetAttrString(pModule,"catchOutErr"); //get our catchOutErr created above
        PyErr_Print(); //make python print any errors
    
        PyObject *output = PyObject_GetAttrString(catcher,"value"); //get the stdout and stderr from our catchOutErr object
    
        printf("Here's the output:\n %s", PyString_AsString(output)); //it's not in our C++ portion
    
        Py_Finalize();
    
    
        return 0;
    
    }
    
    • 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 needs to behave slightly differently on Tiger than on
I have a program which has some Textareas / Labels these can be anywhere
I have a program which needs installing on windows 64 boxes. Most of the
I have a program which only needs a NotifyIcon to work as intended. So
I have a program which deliberately performs a divide by zero (and stores the
Consider this problem: I have a program which should fetch (let's say) 100 records
I have an AppleScript program which creates XML tags and elements within an Adobe
I have a program in which I've lost the C++ source code. Are there
I have a program in which the user adds multiple objects to a scene.
I have an MPI program which compiles and runs, but I would like to

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.