embedding Python in Python

Jp Calderone exarkun at intarweb.us
Tue Mar 4 03:05:37 CET 2003


On Tue, Mar 04, 2003 at 03:32:59AM +0100, Kasper Souren wrote:
> [snip]
> 
> I wouldn't mind writing the Python_Embedded_Interpreter class myself. I played 
> with the idea of simply using eval() but eval("print") doesn't even work as 
> expected.
> 

  You need to differentiate between statement and expressions.  Here's some
dodgy code I just knocked out, it should at least get you going in the right
direction:

    def runPythonCode(s, globals = {}, locals = None):
        if locals is None:
            locals = {}
        try:
            code = compile(s, '<string>', 'eval')
        except:
            code = compile(s, '<string>', 'single')
        result = eval(code, globals, locals)
        return result

  Jp

-- 
 up 17:58, 7 users, load average: 0.09, 0.10, 0.08





More information about the Python-list mailing list