sys.excepthook and syntax errors

Ben Wolfson rumjuggler at cryptarchy.org
Sat Dec 22 19:10:50 EST 2001


Occasionally when I'm in the python prompt and want to change working
directories, I forget where I am and type "cd whatever" instead of
"os.chdir(whatever)".  Obviously this doesn't work.  So I tried overriding
sys.excepthook so that when I did try the former, it would have the same
effect as the latter, but would otherwise report the error correctly.  To
my surprise, it still wouldn't work most of the time.  That is, I could do
this:

>>> cd whatever
  File "<string>", line 1
    cd whatever
              ^
SyntaxError: invalid syntax
>>>

and it didn't seem that sys.excepthook was called at all.  This, however,
worked fine:

>>> exec "cd whatever"
>>>

I can understand why sys.excepthook wouldn't get called for syntax errors
when running python non-interactively, but it seems that it should be
possible for interactive use.

-- 
Barnabas T. Rumjuggler
You're going to set me up as a kind of slovenly attached pig that
Jack Kornfeld can slice down in his violent zen compassion?
 -- Larry Block



More information about the Python-list mailing list