[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
following:
import win32com.client
import os
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)
finally:
app.DisplayAlerts = False
app.Quit()
if __name__ == '__main__':
open_excel('test.xls')
Hope that helps.
Stephan Deibel
--
Wingware
Wing IDE for Python
Advancing Software Development
www.wingware.com
More information about the wingide-users
mailing list