Embedded python console and FILE* in python 3.2

Terry Reedy tjreedy at udel.edu
Mon Aug 8 14:35:10 EDT 2011

On 8/8/2011 11:17 AM, Francis Labarre wrote:
> Hello everyone,
> I'm currentlytrying to port some embedded code from python 2.7 to python
> 3.2.
> The current code replicate the basic behavior of the python interpreter
> in an
> MFC application. When a command is entered in our embedded interpreter,
> we write it to a FILE* then transform this FILE* into a Python file with
> the api
> function PyFile_FromFile and assign it to python's stdin. We also assign
> another
> FILE* as python's stdout and stderr. We then call PyRun_InteractiveOne
> to execute
> the statement as the python interpreter normally would. Finally, we can
> easily retrieve
> the result of the execution from the output FILE*.
> This is currently the only approach we have found that allows us to
> retrieve the
> result of executing a statement exactly as the interpreter would.
> The problem is that the implementation of files has changed in python 3
> and the function
> PyFile_FromFile has been removed.

The source for the function is still available in 2.7. You might be able 
to adapt it to work in 3.x.

IDLE imitates the interactive interpreter by running code in a separate 
non-console pythonw process. There have been issues with getting that to 
work right on Windows (see issue 12540), but it works well when it does. 
Depending on your use case, this might work for you too. Communication 
is through (wrapped) OS pipes, not normal Python file objects.

Terry Jan Reedy

More information about the Python-list mailing list