Running an interactive interpreter inside a python

Alan J. Salmoni salmoni at gmail.com
Thu May 15 00:08:45 EDT 2008


I'm not sure if this is exactly what you're after, but try looking
into the 'code' module.

It's fairly easy to make an interactive interpreter that runs within
your program. If you import your programs variables into
__main__.__dict__, you can have access to them which can be funky. You
can even override the showtraceback method to catch various exceptions
and do daft things like adding new methods to strings. I guess it
would even be possible to have the commands compared to a list of
commands and keywords to build a restricted interpreter, though how
secure this would be against a determined attack is another matter.

Alan

On May 15, 11:31 am, ro... at panix.com (R. Bernstein) wrote:
> The next release of pydb will have the ability to go into ipython from
> inside the debugger. Sort of like how in ruby-debug you can go into
> irb :-)
>
> For ipython, this can be done pretty simply; there is an IPShellEmbed
> method which returns something you can call. But how could one do the
> same for the stock python interactive shell?
>
> To take this out of the realm of debugging. What you want to do is to
> write a python program that goes into the python interactive shell -
> without having to write your own a read/eval loop and deal with
> readline, continuation lines, etc.
>
> The solution should also allow
>  - variables/methods in the calling PYthon program to be visible
>    in the shell
>  - variables set in the interactive (sub) shell should persist after the shell
>    terminates, although this is a weaker requirement. POSIX subshells
>    for example *don't* work this way.
>
> There has been much written about how to embed Python from C, so I
> suppose this may offer one way. And at worst, I could write
> a C extension which follows how C Python does this for itself.
>
> But is there a simpler way?
>
> Thanks.




More information about the Python-list mailing list