| Store | Cart

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

From: Jian <thec...@126.com>
Fri, 21 Jul 2017 22:03:18 +0800 (CST)
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" <ste...@seefeld.name> 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>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