Retrieving result from embedded execution
Hello everyone, We are trying to implement our own interactive interpreter in our applicationusing an embedded Python interpreter. I was wondering what would be the best way to retreive as text the result of executing Python code. The text must be exactly the same as it would be in thestandalone interpreter. We used to do this by changing the embedded interpreter's sys.stdout and sys.stderrwith a FILE created in C++. We could then execute code using the PyRun_InteractiveOnefunction and easily retrieve the result from the FILE. The problem is, our applicationshouldn't create any files if possible. We also tried replacing sys.stdout and sys.stderr with a custom made Python objectwhich could be more easily read from C++. We then used the function PyRun_Stringto execute the code. The problem here is that using the Py_file_input start tokendoesn't write everything we need to sys.stdout. (i.e. executing 2+2 does not write 4).It also doesn't print a traceback to sys.stderr when there is an exception. Using theother two start tokens is impossible since we need to be able to execute more than one instruction at once. We also tried using the function PyRun_SimpleString but weencounter the same problems. We are using python 2.7. Any suggestions? Thank you for your time. Also, I'm new to mailling lists, what is the proper way to reply to someone? Should Ireply directly to someone's email adress or is there a way to answer trough the server. F. Lab -- http://mail.python.org/mailman/listinfo/python-list
RE: Embedded python console and FILE* in python 3.2
> 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 witha custom file-like object would cause a blocking call. > Executing python in a second thread would solve this problembut our > application isn't multi-threaded and executing python in another thread > causes errors. 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. Thanks a lot for the tips, have a nice day. F.L. -- http://mail.python.org/mailman/listinfo/python-list
RE: Embedded python console and FILE* in python 3.2
> If you use the same workflow as you do currently, it won't You're right it shouldn't cause a blocking call. > Come to think of it, you might as well just use StringIO. Yes I could probably replace std{in, out, err} with stringIO objects and retrieve their contents throughthe api. I'll look into it, thank again. F.L. > Date: Mon, 8 Aug 2011 19:52:14 +0200 > From: t...@jollybox.de > To: python-list@python.org > Subject: Re: Embedded python console and FILE* in python 3.2 > > On 08/08/11 19:44, Thomas Jollans wrote: > > > > > 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. > > > > Come to think of it, you might as well just use StringIO. > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list