में पाइथन स्टडआउट को कैसे पकड़ें मेरे पास एक प्रोग्राम है जो कभी-कभी किसी भी प्रोग्राम को प्रीफ़ॉर्म करने के लिए पाइथन को कॉल करने की आवश्यकता होती है। मुझे एक फ़ंक्शन की आवश्यकता है जो पायथन और को पाइथन stdout पकड़ता है और इसे कुछ फ़ाइल में रखता है। इस समारोहसी ++ कोड
pythonCallBackFunc(const char* pythonInput)
मेरे समस्या किसी दिए गए आदेश (pythonInput) के लिए सभी अजगर उत्पादन को पकड़ने के लिए है की एक घोषणा है। मुझे पाइथन एपीआई के साथ कोई अनुभव नहीं है और मुझे नहीं पता कि ऐसा करने के लिए सही तकनीक क्या है। मैंने पहली बार कोशिश की है कि Py_run_SimpleString का उपयोग करके पायथन के एसडीटीओटी और स्टडर को रीडायरेक्ट करना है, यह मेरे द्वारा लिखे गए कोड का कुछ उदाहरण है।
#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;
}
क्या ऐसा करने का कोई बेहतर तरीका है? इसके अलावा, किसी कारण से PyRun_SimpleString कुछ गणितीय अभिव्यक्ति प्राप्त करने पर कुछ भी नहीं करता है, उदाहरण के लिए PyRun_SimpleString ("5 ** 3") कुछ भी प्रिंट नहीं करता है (पायथन कॉन्सलुल परिणाम प्रिंट करता है: 125)
शायद यह महत्वपूर्ण है, मैं दृश्य का उपयोग कर रहा हूं स्टूडियो 2008 धन्यवाद, एलेक्स
परिवर्तन मैं मार्क के सुझाव अनुसार बनाया है:
#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;
}
उत्पादन मैं मुख्य चलाने के बाद मिलती है:
0,123,Here's the output: 123
Here's the output:
Here's the output:
Here's the output: 2
यह मेरे लिए अच्छा है, लेकिन केवल एक ही समस्या है, यह
Here's the output: 123
Here's the output: 6
Here's the output:
Here's the output: 2
होना चाहिए मुझे पता है क्यों, लेकिन इस आदेश को चलाने के बाद न: PythonPrinting ("1 + 5"), PyString_AsString (उत्पादन) आदेश 6 के बजाय एक खाली स्ट्रिंग (char *) देता है ... :(क्या मैं कुछ आउटपुट को खोने के लिए कुछ नहीं कर सकता?
Thaks, एलेक्स
प्रोग्रामिंग प्रश्न स्टैक ओवरव्लो पर हैं। –