2014-10-25 22:07:09 +01:00
|
|
|
# test compile builtin
|
|
|
|
|
2014-10-26 15:38:28 +00:00
|
|
|
def have_compile():
|
|
|
|
try:
|
|
|
|
compile
|
|
|
|
return True
|
|
|
|
except NameError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def test():
|
2015-04-04 22:05:30 +01:00
|
|
|
global x
|
|
|
|
|
2014-10-26 15:38:28 +00:00
|
|
|
c = compile("print(x)", "file", "exec")
|
|
|
|
|
|
|
|
try:
|
|
|
|
exec(c)
|
|
|
|
except NameError:
|
|
|
|
print("NameError")
|
2014-10-25 22:07:09 +01:00
|
|
|
|
2015-04-04 22:05:30 +01:00
|
|
|
# global variable for compiled code to access
|
|
|
|
x = 1
|
|
|
|
|
2014-10-25 22:07:09 +01:00
|
|
|
exec(c)
|
|
|
|
|
2014-10-26 15:38:28 +00:00
|
|
|
exec(c, {"x":2})
|
|
|
|
exec(c, {}, {"x":3})
|
2014-10-25 22:07:09 +01:00
|
|
|
|
2015-04-04 22:05:30 +01:00
|
|
|
# single/eval mode
|
|
|
|
exec(compile('print(1 + 1)', 'file', 'single'))
|
|
|
|
print(eval(compile('1 + 1', 'file', 'eval')))
|
|
|
|
|
|
|
|
# bad mode
|
|
|
|
try:
|
|
|
|
compile('1', 'file', '')
|
|
|
|
except ValueError:
|
|
|
|
print("ValueError")
|
|
|
|
|
|
|
|
# exception within compiled code
|
|
|
|
try:
|
|
|
|
exec(compile('noexist', 'file', 'exec'))
|
|
|
|
except NameError:
|
|
|
|
print("NameError")
|
|
|
|
print(x) # check 'x' still exists as a global
|
|
|
|
|
2014-10-26 15:38:28 +00:00
|
|
|
if have_compile():
|
|
|
|
test()
|
|
|
|
else:
|
|
|
|
print("SKIP")
|