Embedded python console and FILE* in python 3.2

Thomas Jollans t at jollybox.de
Mon Aug 8 13:44:23 EDT 2011

On 08/08/11 19:14, F L wrote:
>> Is the `code` module (http://docs.python.org/library/code.html) an
> insufficiently exact copy of an interpreter for you?
> The problem isn't really to emulate the behavior of the interpreter as
> to obtain the result of the execution as a string in c++. 
> The code module doesn't seem to help with this matter.
>> I assume you could simply use PyFile_FromFd?
> According to the documentation using PyFile_FromFd seems unadvised, but
> I'll give it a try.
>> The more elegant solution might be to create custom Python file-like
>> objects for std{in,out,err} that communicate directly with your application.
> I thought about this, but I need the console window to be non-modal and
> I'm afraid that replacing stdin with
> a custom file-like object would cause a blocking call. Executing python
> in a second thread would solve this problem
> but our application isn't multi-threaded and executing python in another
> thread causes errors.

If you use the same workflow as you do currently, it won't:

1. Feed input to your custom stdin object, which will buffer the code.
2. Call PyRun_IteractiveOne - it will read from your object, which will
   return the buffer contents.
3. The result is somehow handled by custom stdout and stderr objects.

> Maybe I could use a file-like object to communicate the results to my
> c++ application and use the code 
> module to execute code only when the c++ application needs it.

More information about the Python-list mailing list