Hello.

The use of AST is a pretty way BUT you must not use *eval* or
*exec*because of real security issues. It's easy to find explanations
about that
on the web.

Christophe


2013/11/3 Nils Bruin <nbr...@sfu.ca>

> On Sunday, November 3, 2013 12:42:04 PM UTC-8, Nils Bruin wrote:
>
>> Note that you only have to do modifications on top level, so there is not
>> much ast "walking" involved.
>>
> That is of course only true if you want that
>
> for i in range(1000):
>     i
>
> doesn't produce output (which I think is reasonable, but not what python's
> command line does. I guess compile(...,...,"single") does that. So perhaps
> you'd want something along the lines of:
>
> S="""
> for i in range(10):
>     i+1
> for j in range(10):
>     j-1
> """
>
> def execute_block_interactively(code_string):
>     M=ast.parse(code_string)
>     c=compile(ast.Interactive(M.body),"<string>","single")
>     eval(c)
>
> execute_block_interactively(S)
>
> --
> You received this message because you are subscribed to the Google Groups
> "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sage-support+unsubscr...@googlegroups.com.
> To post to this group, send email to sage-support@googlegroups.com.
> Visit this group at http://groups.google.com/group/sage-support.
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to