Gabriel Genellina <[email protected]> added the comment:
The compiler doesn't know how the code is going to be used apart from
the "mode" parameter:
py> c=compile("x=1","","exec")
py> import dis
py> dis.dis(c)
1 0 LOAD_CONST 0 (1)
3 STORE_NAME 0 (x)
6 LOAD_CONST 1 (None)
9 RETURN_VALUE
py> c=compile("global x; x=1","","exec")
py> dis.dis(c)
1 0 LOAD_CONST 0 (1)
3 STORE_GLOBAL 0 (x)
6 LOAD_CONST 1 (None)
9 RETURN_VALUE
The generated code is different, and I may exec it at global or local
scope, with different results.
compile would require a new mode, different from "exec", to
mean "compile this as a module at global scope; forbid global
statements"
If not, this would become invalid:
def foo():
c=compile("global x; x=1","","exec")
exec c
since -at the compile phase- the code is indistinghishable from a
module.
Also, since PEP3003 has been approved (moratorium), language changes
like this will have to wait a few years.
----------
nosy: +gagenellina
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue7329>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com