[wingide-users] Debugging WITH ... AS

Wingware Support support at wingware.com
Sat Oct 7 22:42:07 MDT 2006

On Fri, 6 Oct 2006, Dan Goldner wrote:
> 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?

This logic may rely on garbage collection / cleanup done when 
Python exits to actually execute the finally clause.  In the 
shell in Wing, Python does not exit after the script is 
evaluated, so that doesn't happen.

You might just run this in Wing's debugger instead, which more 
closely mimics the normal run environment.  Evaluate in Shell is 
designed more to eval some code and then keep Python around so 
you can try out functions, etc, from the command line (similar 
to "python -i").

Also, I didn't completely understand your code and realize it 
might be partially written, but using an exception and with 
clause this way seems a bit odd.  If you don't really plan to 
yield multiple times in open_excel(), or do anything more than 
cleanup after yielding, then you probably don't need to use 
'with' and contextlib but could just write something like the 

import win32com.client
import os

def open_excel(inputname):
    filename = os.path.abspath(inputname)
    app = win32com.client.Dispatch('Excel.Application')
            app.Visible = False
            wkbk = app.Workbooks.Add(filename)
            wkbk = app.Workbooks.Add()
        app.DisplayAlerts = False
        app.DisplayAlerts = False

if __name__ == '__main__':

Hope that helps.

Stephan Deibel

Wing IDE for Python
Advancing Software Development


More information about the wingide-users mailing list