sgzwiz / brython

Automatically exported from code.google.com/p/brython
BSD 3-Clause "New" or "Revised" License
0 stars 1 forks source link

exec not working #87

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
exec() is not working.
I need exec to create a Brython test application (to test Brython 
functionnalities).

Original issue reported on code.google.com by d...@famillepinault.fr on 20 Feb 2013 at 9:16

GoogleCodeExporter commented 9 years ago
Salut Nicolas
Could you give an example where exec() doesn't work ? 

On the Brython site console, if you enter :

x = "for i in range(10):\n  print(i)"
exec(x)

you get the expected result

Original comment by pierre.q...@gmail.com on 21 Feb 2013 at 9:20

GoogleCodeExporter commented 9 years ago
Well... I am trying to reproduce the problem in a simple test case but I can't.
However, there is a big difference with Python3 implementation : With Python3, 
locals modified by exec() are not visible from the caller. You have to pass 
your own dict :

def req_str(req):
    loc = {}
    exec(req, globals(), loc)
    return loc["output"]

Original comment by d...@famillepinault.fr on 21 Feb 2013 at 9:51

GoogleCodeExporter commented 9 years ago
req_str is called like this for it to work correctly :

o = req_str("output = 'test'")
print(o)

Original comment by d...@famillepinault.fr on 22 Feb 2013 at 7:53