function causing core dump
Peter Otten
__peter__ at web.de
Tue May 11 04:13:47 EDT 2004
Xaver Hinterhuber wrote:
> I don't show you codeString because it is a real string variable.
You could show an _example_ codeString, though. That would have greatly
simplified the diagnosis.
> And yes, its uncompiled.
> The user in my program enters a python program in this variable without a
> def statement
> and then the code is executed and the result returned.
>
> Maybe you can help me to add a "def f():" to the codeString, take care of
> the
> indentation and then execute the code?
>
> I really don't know how to handle the indentation thing. A "def
> f():\n"+codeString is easy, but this doesn't solve
> this issue.
Maybe you can get away with a simple expression:
>>> from math import *
>>> codeString = "sin(0.5) + 23"
>>> eval(codeString)
23.479425538604204
If not, try
>>> codeString2 = "a=0.5\nb=23\nreturn sin(a) + b"
>>> funcString = "def f():\n " + "\n ".join(codeString2.split("\n"))
>>> funcString
'def f():\n a=0.5\n b=23\n return sin(a) + b'
>>> exec funcString
>>> f()
23.479425538604204
>>>
Peter
More information about the Python-list
mailing list