Looking for a way to include Pyhtho scripting INSIDE a python program

Ivan Illarionov ivan.illarionov at gmail.com
Sun Apr 13 08:27:55 EDT 2008


On Apr 13, 7:16 am, John Antypas <respon... at antypas.net> wrote:
> Hello all,
>
> I'm writing in tool in Python that manipulates various data objects read
> from various streams.  I wanted to give the user a chance to do advanced
> work that could not easily be done from a GUI.
>
> At first, I tried putting in a lightweight scripting language, and then
> I thought, why not include Python in itself -- it is certainly powerful
> enough.
>
> I had assumed I'd present the user with a text window in which they
> could type arbitrary python code.  I'd wrap that code around a function
> and pass that function a call of objects they could manipulate by
> calling the methods of that class.
>
> 1. How can a python program invoke ANOTHER interpreter?
> 2. How can I pass the class in as its argument and get the modified
> class back?
>
> I know I can do something very ugly -- call a C method that calls a new
> python interpreter but that seems VERY ugly.
>
> Help?
>
> Thanks.

You don't need to envoke another interpreter.
Python can interpret arbitrary python code with exec statement.
Wrap user's string inside function definition, and exec it.

You might want to disable words like `import`, `exec` and `eval` in
user's code because it's a big security risk.

--
Ivan Illarionov



More information about the Python-list mailing list