• <i id='WwTWA'><tr id='WwTWA'><dt id='WwTWA'><q id='WwTWA'><span id='WwTWA'><b id='WwTWA'><form id='WwTWA'><ins id='WwTWA'></ins><ul id='WwTWA'></ul><sub id='WwTWA'></sub></form><legend id='WwTWA'></legend><bdo id='WwTWA'><pre id='WwTWA'><center id='WwTWA'></center></pre></bdo></b><th id='WwTWA'></th></span></q></dt></tr></i><div id='WwTWA'><tfoot id='WwTWA'></tfoot><dl id='WwTWA'><fieldset id='WwTWA'></fieldset></dl></div>

    1. <tfoot id='WwTWA'></tfoot>
        <bdo id='WwTWA'></bdo><ul id='WwTWA'></ul>

        <small id='WwTWA'></small><noframes id='WwTWA'>

      1. <legend id='WwTWA'><style id='WwTWA'><dir id='WwTWA'><q id='WwTWA'></q></dir></style></legend>

      2. 如何在 C++ 代码中捕获 python 标准输出

        时间:2023-09-19

          <tbody id='Q8zZJ'></tbody>
          1. <legend id='Q8zZJ'><style id='Q8zZJ'><dir id='Q8zZJ'><q id='Q8zZJ'></q></dir></style></legend>

                <small id='Q8zZJ'></small><noframes id='Q8zZJ'>

                <tfoot id='Q8zZJ'></tfoot>
                • <bdo id='Q8zZJ'></bdo><ul id='Q8zZJ'></ul>
                  <i id='Q8zZJ'><tr id='Q8zZJ'><dt id='Q8zZJ'><q id='Q8zZJ'><span id='Q8zZJ'><b id='Q8zZJ'><form id='Q8zZJ'><ins id='Q8zZJ'></ins><ul id='Q8zZJ'></ul><sub id='Q8zZJ'></sub></form><legend id='Q8zZJ'></legend><bdo id='Q8zZJ'><pre id='Q8zZJ'><center id='Q8zZJ'></center></pre></bdo></b><th id='Q8zZJ'></th></span></q></dt></tr></i><div id='Q8zZJ'><tfoot id='Q8zZJ'></tfoot><dl id='Q8zZJ'><fieldset id='Q8zZJ'></fieldset></dl></div>
                • 本文介绍了如何在 C++ 代码中捕获 python 标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个程序,它在运行期间有时需要调用 python 来执行某些任务.我需要一个函数来调用 python 并捕获 python 标准输出并将其放入某个文件中.这是函数的声明

                  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)
                  

                  我的问题是捕获给定命令的所有 python 输出 (pythonInput).我没有使用 python API 的经验,我不知道什么是正确的技术来做到这一点.我尝试的第一件事是使用 Py_run_SimpleString 重定向 python 的 sdtout 和 stderr这是我编写的代码的一些示例.

                  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 "boostpython.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;
                  }
                  

                  有没有更好的方法来做到这一点?此外,由于某些原因,PyRun_SimpleString 在得到一些数学表达式时什么也不做,例如 PyRun_SimpleString("5**3") 什么都不打印(python conlsul 打印结果:125)

                  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)

                  也许这很重要,我正在使用 Visual Studio 2008.谢谢,亚历克斯

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

                  我根据 Mark 的建议所做的更改:

                  Changes I've made according Mark's suggestion:

                    #include <python.h>
                    #include <string>
                  
                    using namespace std;
                  
                    void PythonPrinting(string inputStr){ 
                       string stdOutErr =
                      "import sys
                  
                       class CatchOut:
                  
                          def __init__(self):
                  
                             self.value = ''
                  
                          def write(self, txt):
                  
                             self.value += txt
                  
                       catchOut = CatchOut()
                  
                       sys.stdout = catchOut
                  
                       sys.stderr = catchOut
                  
                      "; //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
                  ", 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;
                    }
                  

                  运行 main 后得到的输出:

                  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
                  

                  我不知道为什么但是在运行这个命令之后:PythonPrinting("1+5"), PyString_AsString(output) 命令返回一个空字符串 (char*) 而不是 6... :( 有什么我不能做的吗?松开这个输出?

                  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?

                  谢谢,亚历克斯

                  推荐答案

                  如果我正确阅读了您的问题,您想将 stdout/stderr 捕获到 C++ 中的变量中吗?您可以通过将 stdout/stderr 重定向到一个 python 变量,然后将该变量查询到您的 C++ 中来做到这一点.请注意,我没有在下面进行正确的引用计数:

                  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
                  
                  class CatchOutErr:
                  
                      def __init__(self):
                  
                          self.value = ''
                  
                      def write(self, txt):
                  
                          self.value += txt
                  
                  catchOutErr = CatchOutErr()
                  
                  sys.stdout = catchOutErr
                  
                  sys.stderr = catchOutErr
                  
                  "; //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:
                   %s", PyString_AsString(output)); //it's not in our C++ portion
                  
                      Py_Finalize();
                  
                  
                      return 0;
                  
                  }
                  

                  这篇关于如何在 C++ 代码中捕获 python 标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:更改静态链接 DLL 的 DLL 搜索路径 下一篇:Windows C++:如何重定向 stderr 以调用 fprintf?

                  相关文章

                  最新文章

                • <tfoot id='sn3TM'></tfoot>
                  <i id='sn3TM'><tr id='sn3TM'><dt id='sn3TM'><q id='sn3TM'><span id='sn3TM'><b id='sn3TM'><form id='sn3TM'><ins id='sn3TM'></ins><ul id='sn3TM'></ul><sub id='sn3TM'></sub></form><legend id='sn3TM'></legend><bdo id='sn3TM'><pre id='sn3TM'><center id='sn3TM'></center></pre></bdo></b><th id='sn3TM'></th></span></q></dt></tr></i><div id='sn3TM'><tfoot id='sn3TM'></tfoot><dl id='sn3TM'><fieldset id='sn3TM'></fieldset></dl></div>

                  <small id='sn3TM'></small><noframes id='sn3TM'>

                  1. <legend id='sn3TM'><style id='sn3TM'><dir id='sn3TM'><q id='sn3TM'></q></dir></style></legend>
                    • <bdo id='sn3TM'></bdo><ul id='sn3TM'></ul>