| Store | Cart

Re: [C++-sig] How to get output from python embedded in VC++

From: Jian <thec...@126.com>
Fri, 21 Jul 2017 22:28:28 +0800 (CST)
Dear Imre,


Thanks a lot. I'm not using boost::python but the original way from the official website. Main code to invoke py module are below 2 lines.


pName = PyUnicode_DecodeFSDefault("test1");
pModule = PyImport_Import(pName);


and below is my py file:


import emb
print("Number of arguments", emb.numargs())
print("test for 中文(Chinese)")
#import numpy as np
props = []
props = emb.getproperty(23)
print(props)


As you can see above. I have a embedded object (emb) in my C++ program and exposed a function named 'getproperty(int)'. Also there are some output from the py file. what I want is to capture those output and also need to feedback input if needed(if something like input() is used in the future). So I could get data from inside my program and update some part using py file.


In addition, I have tried to redirect std::out by using derived class from std::basic_streambuf. It can be easily redirect to a EDIT control through a callback function(as below code). 


m_newBuffer = new mrsStreamBuf<>(std::cout, outCallBack, &m_cmdWindow);


As a result, only output from std::out are captured. The output from py file are totally missing (Displayed in the separate console window created by my program). Is it because I'm not using boost::python?

Thanks & Best Regards,


John


At 2017-07-21 03:22:47, "Horváth Imre István" <imre...@gmail.com> wrote:
>Hi John!>>If you want to embed python in c++ app and exchange data between c++ and>python, take a look at the boost::python library.>It's also a platform independent solution (my app's target platforms are>windows vs2015 and linux CentOS7).>>You can do something like this:>>    using namespace boost::python;>    Py_Initialize();>    try {>        boost::python::object main_module =>boost::python::import("__main__");>        boost::python::object main_namespace =>main_module.attr("__dict__");>>        boost::python::dict mydata;>  	mydata["x"] = 5;>        mydata["foo"] = boost::python::dict();>        mydata["foo"]["bar1"] = "data1";>	mydata["foo"]["bar2"] = "data2";>>	// you can declare variables in the python namespace from c++>        main_namespace["mydata"] = mydata;>>        // in the script there is a mydata variable: {'x': 5, 'foo':>{'bar1': 'data1', 'bar2': 'data2'}}>        boost::python::object ignored = exec_file("script.py",>main_namespace, main_namespace);>        >        // declare a variable named return_value in the script, >        // and you can extract it's value to c++ like this>	std::wstring output =>extract<std::wstring>(main_namespace["return_value"]);>>    } catch( boost::python::error_already_set) {>        PyErr_Print();>    }>>(If your python script has some output, don't forget to redirect boost>std::cout etc)>>Best regards:>Imre>>2017. 07. 20, csütörtök keltezéssel 11.25-kor Jian ezt írta:>> Dear Gurus,>> >> I want to embed python interpreter in my program. What I want is to>> redirect those output to a control such as RICHEDIT which can be>> modified as a output window in my program. So I can write *.py files>> outside of program and invoke it inside program as a script file. In>> order to get information output by the *.py file I need to get the>> stdin&stdout. I have tried some workflow but not perfect.>> >> 1). I have tried use Allocconsle(). But I can only get the output info>> printed by std::out & printf() in the current code. all things which>> are printed by python35.dll are missing. I used print('xxxx') in the>> *.py file to test the output. Those *.py files are OK in command line>> mode.>> >> 2). I also tried to derive class basic_streambuf and overwrite the>> in/out functions. It works only for output from std::out. Text from>> printf() as well as from dlls are missing.>> >> 3). then I tried to use linker settings as below. >> >> #pragma comment( linker,>> "/subsystem:console /entry:wWinMainCRTStartup" )>> >> A cmd window is created along with the program. everything output from>> current process and dlls are retrieved successfully as I want. But the>> cmd window is not easy to control.>> >> Is there a better way for this purpose? >> >> Thanks in advance for your kind help.>> >> Best Regards,>> >> John. >> >> >> >> >>  >> >> _______________________________________________>> Cplusplus-sig mailing list>> Cplu...@python.org>> https://mail.python.org/mailman/listinfo/cplusplus-sig>>>_______________________________________________>Cplusplus-sig mailing list>Cplu...@python.org>https://mail.python.org/mailman/listinfo/cplusplus-sig
_______________________________________________
Cplusplus-sig mailing list
Cplu...@python.org
https://mail.python.org/mailman/listinfo/cplusplus-sig

Recent Messages in this Thread
Jian Jul 20, 2017 03:25 am
Stefan Seefeld Jul 20, 2017 02:34 pm
Jian Jul 21, 2017 02:03 pm
Stefan Seefeld Jul 21, 2017 02:15 pm
Horváth Imre István Jul 20, 2017 07:22 pm
Jian Jul 21, 2017 02:28 pm
Horváth Imre István Jul 21, 2017 03:13 pm
Jian Jul 22, 2017 03:17 am
Stefan Seefeld Jul 22, 2017 05:01 pm
Jian Jul 23, 2017 07:15 am
Stefan Seefeld Jul 23, 2017 03:06 pm
Jian Aug 01, 2017 02:33 am
Messages in this thread