Bug in execfile?

Fernando Perez fperez.net at gmail.com
Thu Aug 2 14:32:43 EDT 2007


Hi all,

I'm finding the following behavior truly puzzling, but before I post a bug
report on the site, I'd rather be corrected if I'm just missing somethin
obvious.

Consider the following trivial script:

# Simple script that imports something from the stdlib
from math import sin, pi

wav = lambda k,x: sin(2*pi*k*x)

print wav(1,0.25)

# EOF


The above runs just fine from a prompt, or even interactively via execfile(). 
Now, consider calling it by using this instead:

#!/usr/bin/env python
"""Test for a bug (?) in scope handling by the execfile() builtin."""


def runscript(fname):
    """Run a file by calling execfile()."""
    execfile(fname)


# Note: if you activate this section so that execfile() is directly called
# first, then the bug below disappears!!!
if 0:
    print '='*80
    print '# Trying execfile:'
    execfile('execfilebugscript.py')

# The bug: we get an exception from running the little script, where the 'sin'
# name imported from math is not visible to the lambda.
print '-'*80
print '# Trying the runscript wrapper:'
runscript('execfilebugscript.py')

##### EOF


If I run the above, calling the first script 'execfilebugscript.py' and the
second 'execfilebug.py', I get this:

planck[test]> ./execfilebug.py
--------------------------------------------------------------------------------
# Trying the runscript wrapper:
Traceback (most recent call last):
  File "./execfilebug.py", line 21, in <module>
    runscript('execfilebugscript.py')
  File "./execfilebug.py", line 7, in runscript
    execfile(fname)
  File "execfilebugscript.py", line 6, in <module>
    print wav(1,0.25)
  File "execfilebugscript.py", line 4, in <lambda>
    wav = lambda k,x: sin(2*pi*k*x)
NameError: global name 'sin' is not defined


WTF???  Now even weirder, if the 'if 0' is turned into 'if 1' so that *first*
execfile is called at the top-level (not inside a function), then *both* calls
work:

planck[test]> ./execfilebug.py
================================================================================
# Trying execfile:
1.0
--------------------------------------------------------------------------------
# Trying the runscript wrapper:
1.0


I'm really, really puzzled by this.  From reading the execfile() docs, I had
the hunch to change the call to:

    execfile(fname,{})

and now the problem disappears, so I can keep on working.

But I'm still very bothered by the fact that changing that first call 'if 0'
to 'if 1' has any effect on the later call to runscript().  That really
doesn't feel right to me...

Any wisdom will be much appreciated.

Cheers,

f




More information about the Python-list mailing list