[wingide-users] Debugging WITH ... AS

Dan Goldner goldner at alum.mit.edu
Fri Oct 6 22:27:52 MDT 2006


Hi,
I'm a new-ish programmer trying to get my head around how exceptions are
handled by Wing when it evaluates a script in the python shell.

The script below is designed to open Excel in the background, then
deliberately trigger an exception. Through the new (python 2.5)
WITH...AS construct, Excel should quit as if from a "finally" clause.

When I run the script from the command line, it works: Excel is opened,
the exception occurs, and when I check the task manager, Excel has
closed.

When I run the script from Wing using Source > Evaluate File in Python
Shell, the exception occurs, and Excel is still running in the
background. The Quit() call is never made.

What's different about the Wing shell?

Thanks for any pointers,
-DG


from __future__ import with_statement
from contextlib import contextmanager
import win32com.client
import os

@contextmanager
def open_excel(inputname):
     filename = os.path.abspath(inputname)
     app = win32com.client.Dispatch('Excel.Application')
     try:
         try:
             app.Visible = False
             wkbk = app.Workbooks.Add(filename)
         except:
             wkbk = app.Workbooks.Add()
         app.DisplayAlerts = False
         wkbk.SaveAs(filename)
         yield (app, wkbk)
     finally:
         app.DisplayAlerts = False
         app.Quit()

if __name__ == '__main__':
     with open_excel('test.xls') as (app, wkbk):
	# Raise an error, any error, then check that Excel has quit
         raise IOError



More information about the wingide-users mailing list