source command for python scripts

Thiago Jung Bauermann bauerman@br.ibm.com
Fri Nov 7 14:09:00 GMT 2008


El mié, 05-11-2008 a las 11:13 -0700, Tom Tromey escribió:
>     def invoke(self, arg, from_tty):
>         self.dont_repeat()
>         execfile(arg)

Just FYI, this command will not work as is, and it made me scratch my
head for a good while. When you try to source a Python script which
defines a new command, psource fails with:

NameError: global name 'ReverseBacktrace' is not defined

The funny thing is that the same script works when sourced directly with
"python execfile('reverse-backtrace.py')". Due to limitations in the
Python language which I'm not sure I understood yet, you have to change
the execfile call in the invoke method to:

    execfile(arg, globals ())

Then psource will work as expected. This is apparently a common pitfall
with execfile in Python, and you will find users struggling with it if
you search the web about the problem. I found an explanation in:

http://bytes.com/forum/thread21061.html

-- 
[]'s
Thiago Jung Bauermann
IBM Linux Technology Center



More information about the Archer mailing list