From thecolors at 126.com Wed Jul 19 23:25:33 2017 From: thecolors at 126.com (Jian) Date: Thu, 20 Jul 2017 11:25:33 +0800 (CST) Subject: [C++-sig] How to get output from python embedded in VC++ Message-ID: <6b622f16.395e.15d5e0775a3.Coremail.thecolors@126.com> 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. -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan at seefeld.name Thu Jul 20 10:34:35 2017 From: stefan at seefeld.name (Stefan Seefeld) Date: Thu, 20 Jul 2017 10:34:35 -0400 Subject: [C++-sig] How to get output from python embedded in VC++ In-Reply-To: <6b622f16.395e.15d5e0775a3.Coremail.thecolors@126.com> References: <6b622f16.395e.15d5e0775a3.Coremail.thecolors@126.com> Message-ID: <22e8b686-1d67-ed9b-feae-52a122a8907f@seefeld.name> On 19.07.2017 23:25, Jian wrote: > > 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. > So you want to capture all output produced by the Python session, without changing the behaviour of `std::cout` or `printf()`, correct ? > 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? > I suggest you import the `sys` module and substitute `sys.stdout` and `sys.stderr` to capture output rather than send to stdout and stderr. The technique is described in many places, for example https://www.blog.pythonlibrary.org/2016/06/16/python-101-redirecting-stdout/. Please be aware that due to the way Python3 changed its representation of strings (, unicode, bytes, etc.) you may have to be careful to find a solution that works portably. You could do this either in a Python wrapper script, or directly in the code you use to initialize your Python session (in C++). I'm only a casual Windows user (and even less programmer), so can't comment on any Windows-specific idioms to use. HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin... From imre.i.horvath at gmail.com Thu Jul 20 15:22:47 2017 From: imre.i.horvath at gmail.com (=?ISO-8859-1?Q?Horv=E1th?= Imre =?ISO-8859-1?Q?Istv=E1n?=) Date: Thu, 20 Jul 2017 21:22:47 +0200 Subject: [C++-sig] How to get output from python embedded in VC++ In-Reply-To: <6b622f16.395e.15d5e0775a3.Coremail.thecolors@126.com> References: <6b622f16.395e.15d5e0775a3.Coremail.thecolors@126.com> Message-ID: <1500578567.3304.9.camel@gmail.com> 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(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 > Cplusplus-sig at python.org > https://mail.python.org/mailman/listinfo/cplusplus-sig From stefan at seefeld.name Fri Jul 21 10:15:46 2017 From: stefan at seefeld.name (Stefan Seefeld) Date: Fri, 21 Jul 2017 10:15:46 -0400 Subject: [C++-sig] How to get output from python embedded in VC++ In-Reply-To: <7dec4240.9c99.15d6575b213.Coremail.thecolors@126.com> References: <6b622f16.395e.15d5e0775a3.Coremail.thecolors@126.com> <22e8b686-1d67-ed9b-feae-52a122a8907f@seefeld.name> <7dec4240.9c99.15d6575b213.Coremail.thecolors@126.com> Message-ID: On 21.07.2017 10:03, Jian wrote: > > Then, I have tried this method. It works. I got the text print from > the file. below are the code I used following your advice. > > import sys > original = sys.stdout > sys.stdout = open('redirect.txt', 'w') > ... > other code! > ... > sys.stdout.close() > sys.stdout = original > > But it still not exactly what I expected. Because the text printed to > the file can't be read into the program at the same time. Then don't redirect to a file; redirect to a buffer that you can attach to your text output widget. The object assigned to `sys.stdout` doesn't have to be a file, it only needs to be a "file-like" object. So, typically you'd use something like StringIO, or, as is done by the document I pointed you to, a GUI widget directly (see section "Redirecting stdout in wxPython"). > > As you mentioned you are not familiar with Windows. I guess you are > using Linux. So could you give me some advice if it can be done in > C/C++ in Linux? See above. The problem is entirely unrelated to the platform you are running on. I was just commenting on the approach you had taken, which was Windows-specific. > > Thanks again for your kind help. > Best Regards, > > Jian Stefan -- ...ich hab' noch einen Koffer in Berlin... From imre.i.horvath at gmail.com Fri Jul 21 11:13:37 2017 From: imre.i.horvath at gmail.com (=?ISO-8859-1?Q?Horv=E1th?= Imre =?ISO-8859-1?Q?Istv=E1n?=) Date: Fri, 21 Jul 2017 17:13:37 +0200 Subject: [C++-sig] How to get output from python embedded in VC++ In-Reply-To: <6b622f16.395e.15d5e0775a3.Coremail.thecolors@126.com> References: <6b622f16.395e.15d5e0775a3.Coremail.thecolors@126.com> Message-ID: <1500650017.17835.0.camel@gmail.com> 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(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 > Cplusplus-sig at python.org > https://mail.python.org/mailman/listinfo/cplusplus-sig From thecolors at 126.com Fri Jul 21 10:28:28 2017 From: thecolors at 126.com (Jian) Date: Fri, 21 Jul 2017 22:28:28 +0800 (CST) Subject: [C++-sig] How to get output from python embedded in VC++ In-Reply-To: <1500578567.3304.9.camel@gmail.com> References: <6b622f16.395e.15d5e0775a3.Coremail.thecolors@126.com> <1500578567.3304.9.camel@gmail.com> Message-ID: <3d42d5d9.9dff.15d658cbb3e.Coremail.thecolors@126.com> 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" 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(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 >> Cplusplus-sig at python.org >> https://mail.python.org/mailman/listinfo/cplusplus-sig > > >_______________________________________________ >Cplusplus-sig mailing list >Cplusplus-sig at python.org >https://mail.python.org/mailman/listinfo/cplusplus-sig -------------- next part -------------- An HTML attachment was scrubbed... URL: From thecolors at 126.com Fri Jul 21 10:03:18 2017 From: thecolors at 126.com (Jian) Date: Fri, 21 Jul 2017 22:03:18 +0800 (CST) Subject: [C++-sig] How to get output from python embedded in VC++ In-Reply-To: <22e8b686-1d67-ed9b-feae-52a122a8907f@seefeld.name> References: <6b622f16.395e.15d5e0775a3.Coremail.thecolors@126.com> <22e8b686-1d67-ed9b-feae-52a122a8907f@seefeld.name> Message-ID: <7dec4240.9c99.15d6575b213.Coremail.thecolors@126.com> Hi Stefan, Thanks for your suggestion. First of all, I answer your question. I only want to capture the output&input from python when I call py file or import a module in C/C++ code. It works something like a debug output window does. I could only guess that Python uses something like printf(). That's why I want to redirect the stdout/in. Then, I have tried this method. It works. I got the text print from the file. below are the code I used following your advice. import sys original = sys.stdout sys.stdout = open('redirect.txt', 'w') ... other code! ... sys.stdout.close() sys.stdout = original But it still not exactly what I expected. Because the text printed to the file can't be read into the program at the same time. As you mentioned you are not familiar with Windows. I guess you are using Linux. So could you give me some advice if it can be done in C/C++ in Linux? Thanks again for your kind help. Best Regards, Jian At 2017-07-20 22:34:35, "Stefan Seefeld" wrote: >On 19.07.2017 23:25, Jian wrote: >> >> 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. >> > >So you want to capture all output produced by the Python session, >without changing the behaviour of `std::cout` or `printf()`, correct ? > >> 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? >> > >I suggest you import the `sys` module and substitute `sys.stdout` and >`sys.stderr` to capture output rather than send to stdout and stderr. >The technique is described in many places, for example >https://www.blog.pythonlibrary.org/2016/06/16/python-101-redirecting-stdout/. >Please be aware that due to the way Python3 changed its representation >of strings (, unicode, bytes, etc.) you may have to be careful to find a >solution that works portably. > >You could do this either in a Python wrapper script, or directly in the >code you use to initialize your Python session (in C++). > >I'm only a casual Windows user (and even less programmer), so can't >comment on any Windows-specific idioms to use. > >HTH, > Stefan > >-- > > ...ich hab' noch einen Koffer in Berlin... > >_______________________________________________ >Cplusplus-sig mailing list >Cplusplus-sig at python.org >https://mail.python.org/mailman/listinfo/cplusplus-sig -------------- next part -------------- An HTML attachment was scrubbed... URL: From thecolors at 126.com Fri Jul 21 23:17:29 2017 From: thecolors at 126.com (Jian) Date: Sat, 22 Jul 2017 11:17:29 +0800 (CST) Subject: [C++-sig] How to get output from python embedded in VC++ In-Reply-To: <1500650017.17835.0.camel@gmail.com> References: <6b622f16.395e.15d5e0775a3.Coremail.thecolors@126.com> <1500650017.17835.0.camel@gmail.com> Message-ID: <1378af21.160d.15d684cca04.Coremail.thecolors@126.com> Hi Imre, I tried to use boost.python as you advised. Build boost.python with But I got a error in the import.hpp as below: ------------------------import.hpp start ---------------------------------------- namespace boost { namespace python { object BOOST_PYTHON_DECL import(str name) { // should be 'char const *' but older python versions don't use 'const' yet. char *n = python::extract(name); python::handle<> module(PyImport_ImportModule(n)); <---here is where the error raised. n is "__main__" return python::object(module); } } // namespace boost::python } // namespace boost ------------------------import.hpp end---------------------------------------- I copied your code and just replaced 'script.py' with the name of my own file. I found the error raised when the code importing module "__main__". That's my first time to use boost.python. Could you help about this issue? Thanks a lot. Best Regards, John. At 2017-07-21 23:13:37, "Horv?th Imre Istv?n" 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(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 >> Cplusplus-sig at python.org >> https://mail.python.org/mailman/listinfo/cplusplus-sig > > >_______________________________________________ >Cplusplus-sig mailing list >Cplusplus-sig at python.org >https://mail.python.org/mailman/listinfo/cplusplus-sig -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan at seefeld.name Sat Jul 22 13:01:29 2017 From: stefan at seefeld.name (Stefan Seefeld) Date: Sat, 22 Jul 2017 13:01:29 -0400 Subject: [C++-sig] How to get output from python embedded in VC++ In-Reply-To: <1378af21.160d.15d684cca04.Coremail.thecolors@126.com> References: <6b622f16.395e.15d5e0775a3.Coremail.thecolors@126.com> <1500650017.17835.0.camel@gmail.com> <1378af21.160d.15d684cca04.Coremail.thecolors@126.com> Message-ID: <7eec9b1e-79a0-25f4-788f-529d7f62c9ea@seefeld.name> On 21.07.2017 23:17, Jian wrote: > Hi Imre, > > I tried to use boost.python as you advised. Build boost.python with > > But I got a error in the import.hpp as below: > ------------------------import.hpp start > ---------------------------------------- > namespace boost > { > namespace python > { > > object BOOST_PYTHON_DECL import(str name) > { > // should be 'char const *' but older python versions don't use > 'const' yet. > char *n = python::extract(name); > * python::handle<> module(PyImport_ImportModule(n)); <---here is > where the error raised. n is "__main__"* > return python::object(module); > } > > } // namespace boost::python > } // namespace boost > ------------------------import.hpp > end---------------------------------------- > > I copied your code and just replaced 'script.py' with the name of my > own file. I found the error raised when the code importing module > "__main__". > > That's my first time to use boost.python. Could you help about this issue? This probably means you haven't initialized Python correctly. A good starting point might be this example: https://github.com/boostorg/python/blob/develop/example/quickstart/embedding.cpp (I'm the Boost.Python maintainer. While I'd be happy to help you using Boost.Python, I think this is mostly orthogonal to your original question about how to capture output generated by you embedded Python.) Best, Stefan -- ...ich hab' noch einen Koffer in Berlin... From thecolors at 126.com Sun Jul 23 03:15:23 2017 From: thecolors at 126.com (Jian) Date: Sun, 23 Jul 2017 15:15:23 +0800 (CST) Subject: [C++-sig] How to get output from python embedded in VC++ In-Reply-To: <7eec9b1e-79a0-25f4-788f-529d7f62c9ea@seefeld.name> References: <6b622f16.395e.15d5e0775a3.Coremail.thecolors@126.com> <1500650017.17835.0.camel@gmail.com> <1378af21.160d.15d684cca04.Coremail.thecolors@126.com> <7eec9b1e-79a0-25f4-788f-529d7f62c9ea@seefeld.name> Message-ID: <4c138090.19fb.15d6e4cf638.Coremail.thecolors@126.com> Hi Stefan, I still got a assertion failed at Py_Initialize(). Below is the message. Program: C:\Python35\python35_d.dll File: ..\Objects\object.c Line: 84 Expression: (op->_ob_prev == NULL) == (op->_ob_next == NULL) I also pasted mo code below. This function is invoked by a button click function. No other codes around it. int runBoostPython() { Py_Initialize(); try { //boost::python::object main_module = // boost::python::import("__main__"); //boost::python::object main_namespace = // main_module.attr("__dict__"); boost::python::dict main_namespace; boost::python::object ignored = exec_file("test1.py", main_namespace, main_namespace); std::wstring output = boost::python::extract(main_namespace["return_value"]); } catch (boost::python::error_already_set) { PyErr_Print(); } return 0; } Please help check it. Thanks & Best Regards, John. At 2017-07-23 01:01:29, "Stefan Seefeld" wrote: >On 21.07.2017 23:17, Jian wrote: >> Hi Imre, >> >> I tried to use boost.python as you advised. Build boost.python with >> >> But I got a error in the import.hpp as below: >> ------------------------import.hpp start >> ---------------------------------------- >> namespace boost >> { >> namespace python >> { >> >> object BOOST_PYTHON_DECL import(str name) >> { >> // should be 'char const *' but older python versions don't use >> 'const' yet. >> char *n = python::extract(name); >> * python::handle<> module(PyImport_ImportModule(n)); <---here is >> where the error raised. n is "__main__"* >> return python::object(module); >> } >> >> } // namespace boost::python >> } // namespace boost >> ------------------------import.hpp >> end---------------------------------------- >> >> I copied your code and just replaced 'script.py' with the name of my >> own file. I found the error raised when the code importing module >> "__main__". >> >> That's my first time to use boost.python. Could you help about this issue? > >This probably means you haven't initialized Python correctly. A good >starting point might be this example: >https://github.com/boostorg/python/blob/develop/example/quickstart/embedding.cpp > >(I'm the Boost.Python maintainer. While I'd be happy to help you using >Boost.Python, I think this is mostly orthogonal to your original >question about how to capture output generated by you embedded Python.) > > > >Best, > Stefan > > > > >-- > > ...ich hab' noch einen Koffer in Berlin... > >_______________________________________________ >Cplusplus-sig mailing list >Cplusplus-sig at python.org >https://mail.python.org/mailman/listinfo/cplusplus-sig -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan at seefeld.name Sun Jul 23 11:06:38 2017 From: stefan at seefeld.name (Stefan Seefeld) Date: Sun, 23 Jul 2017 11:06:38 -0400 Subject: [C++-sig] How to get output from python embedded in VC++ In-Reply-To: <4c138090.19fb.15d6e4cf638.Coremail.thecolors@126.com> References: <6b622f16.395e.15d5e0775a3.Coremail.thecolors@126.com> <1500650017.17835.0.camel@gmail.com> <1378af21.160d.15d684cca04.Coremail.thecolors@126.com> <7eec9b1e-79a0-25f4-788f-529d7f62c9ea@seefeld.name> <4c138090.19fb.15d6e4cf638.Coremail.thecolors@126.com> Message-ID: <11123d6c-72a8-8110-d214-3f2a1e7d9fad@seefeld.name> On 23.07.2017 03:15, Jian wrote: > > Hi Stefan, > > I still got a assertion failed at Py_Initialize(). Below is the message. > > Program: C:\Python35\python35_d.dll > File: ..\Objects\object.c > Line: 84 > Expression: (op->_ob_prev == NULL) == (op->_ob_next == NULL) And you get this output from the Py_Initialize() call itself ?? I have no idea how this could happen. I have very little experience with Windows, and so am not really able to help debug this. My first guess would be that you are linking to the wrong library. (I see 'python35_d.dll'. What happens if you link to 'python35.dll', i.e. the non-debug version ?) > > I also pasted mo code below. This function is invoked by a button > click function. No other codes around it. > > int runBoostPython() > { > Py_Initialize(); > try { > //boost::python::object main_module = > // boost::python::import("__main__"); > //boost::python::object main_namespace = > // main_module.attr("__dict__"); > boost::python::dict main_namespace; > > boost::python::object ignored = exec_file("test1.py", > main_namespace, main_namespace); > > std::wstring output = > boost::python::extract(main_namespace["return_value"]); I'm not sure it's possible to use `extract` with std::wstring. At least I have never used it with that. (What encoding is your string using ? How should it be converted to a wide string ?) > > } > catch (boost::python::error_already_set) { > PyErr_Print(); > } > return 0; > } > > Please help check it. > > Thanks & Best Regards, > > John. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From thecolors at 126.com Mon Jul 31 22:33:44 2017 From: thecolors at 126.com (Jian) Date: Tue, 1 Aug 2017 10:33:44 +0800 (CST) Subject: [C++-sig] How to get output from python embedded in VC++ In-Reply-To: <11123d6c-72a8-8110-d214-3f2a1e7d9fad@seefeld.name> References: <6b622f16.395e.15d5e0775a3.Coremail.thecolors@126.com> <1500650017.17835.0.camel@gmail.com> <1378af21.160d.15d684cca04.Coremail.thecolors@126.com> <7eec9b1e-79a0-25f4-788f-529d7f62c9ea@seefeld.name> <4c138090.19fb.15d6e4cf638.Coremail.thecolors@126.com> <11123d6c-72a8-8110-d214-3f2a1e7d9fad@seefeld.name> Message-ID: <1e5d5e26.2775.15d9ba4557c.Coremail.thecolors@126.com> Hi Stefan, Sorry for the late update. Changing to 'python35.dll' does not work. I guess there were something wrong when I complied the boost::python. I have been working on this for few days but I failed to find any error. So now I'm trying to use the console created along with main MFC frame. It captures everything although it still looks not very good . I have found the way to optimize its behaviors as an embedded window control. then I could continue to focus on the main works. Thanks a lot for your kind help. Best Regards, John. At 2017-07-23 23:06:38, "Stefan Seefeld" wrote: >On 23.07.2017 03:15, Jian wrote: >> >> Hi Stefan, >> >> I still got a assertion failed at Py_Initialize(). Below is the message. >> >> Program: C:\Python35\python35_d.dll >> File: ..\Objects\object.c >> Line: 84 >> Expression: (op->_ob_prev == NULL) == (op->_ob_next == NULL) > >And you get this output from the Py_Initialize() call itself ?? I have >no idea how this could happen. I have very little experience with >Windows, and so am not really able to help debug this. My first guess >would be that you are linking to the wrong library. (I see >'python35_d.dll'. What happens if you link to 'python35.dll', i.e. the >non-debug version ?) > >> >> I also pasted mo code below. This function is invoked by a button >> click function. No other codes around it. >> >> int runBoostPython() >> { >> Py_Initialize(); >> try { >> //boost::python::object main_module = >> // boost::python::import("__main__"); >> //boost::python::object main_namespace = >> // main_module.attr("__dict__"); >> boost::python::dict main_namespace; >> >> boost::python::object ignored = exec_file("test1.py", >> main_namespace, main_namespace); >> >> std::wstring output = >> boost::python::extract(main_namespace["return_value"]); > >I'm not sure it's possible to use `extract` with std::wstring. At least >I have never used it with that. (What encoding is your string using ? >How should it be converted to a wide string ?) > > >> >> } >> catch (boost::python::error_already_set) { >> PyErr_Print(); >> } >> return 0; >> } >> >> Please help check it. >> >> Thanks & Best Regards, >> >> John. > >Regards, > Stefan > >-- > > ...ich hab' noch einen Koffer in Berlin... > >_______________________________________________ >Cplusplus-sig mailing list >Cplusplus-sig at python.org >https://mail.python.org/mailman/listinfo/cplusplus-sig -------------- next part -------------- An HTML attachment was scrubbed... URL: