<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>Arunrocks &#187; py2exe</title> <atom:link href="http://www.arunrocks.com/blog/archives/category/py2exe/feed/" rel="self" type="application/rss+xml" /><link>http://www.arunrocks.com/blog</link> <description>Representing Anti-monotonistic Tendencies</description> <lastBuildDate>Tue, 27 Jul 2010 14:41:38 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.8.6</generator> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>Making Python Scripts Show Windows-friendly Errors/Stacktrace</title><link>http://www.arunrocks.com/blog/archives/2007/06/20/making-python-scripts-show-windows-friendly-errorsstacktrace/</link> <comments>http://www.arunrocks.com/blog/archives/2007/06/20/making-python-scripts-show-windows-friendly-errorsstacktrace/#comments</comments> <pubDate>Tue, 19 Jun 2007 19:33:41 +0000</pubDate> <dc:creator>Arun Bhai</dc:creator> <category><![CDATA[General]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[Technical]]></category> <category><![CDATA[py2exe]]></category><guid
isPermaLink="false">http://www.arunrocks.com/blog/archives/2007/06/20/making-python-scripts-show-windows-friendly-errorsstacktrace/</guid> <description><![CDATA[Most of us love to distribute our python programs to others once you have finished coding a neat little script. For Windows users we package it using Py2exe or cx_freeze. However, many of the end-users will not be happy with a black command window popping up, say, when an error is thrown.Of course the alternative [...]]]></description> <content:encoded><![CDATA[<p>Most of us love to distribute our python programs to others once you have finished coding a neat little script. For Windows users we package it using <a
href="http://www.py2exe.org/index.cgi/">Py2exe</a> or <a
href="http://www.cxtools.net/default.aspx?nav=cxfrlb">cx_freeze</a>. However, many of the end-users will not be happy with a black command window popping up, say, when an error is thrown.</p><p>Of course the alternative is to write a full blown GUI application using <a
href="http://www.wxpython.org/">WXPython</a> or <a
href="http://pyfltk.sourceforge.net/">PyFLTK</a>. Even the latter, though quite lightweight, adds several megabytes to the distribution, when all you need is a simple message-box indicating an error or showing some informational text. Clearly, its an overkill for your throwaway python scripts.</p><p>This is the kind of problem I typically face and I have found a good solution. The answer is <a
href="http://python.net/crew/theller/ctypes/">ctypes library</a> which comes as a part of the standard distribution from Python 2.5 onwards. It simply calls the messagebox function from user32.dll (which is always present in a windows installation). With the main problem solved, what remained was to obtain the error text and stack trace.</p><p>Let&#8217;s see how the code looks like:</p><pre><code># Importing all the works for a native Win32 Message Box
from ctypes import c_int, WINFUNCTYPE, windll
from ctypes.wintypes import HWND, LPCSTR, UINT
prototype = WINFUNCTYPE(c_int, HWND, LPCSTR, LPCSTR, UINT)
paramflags = (1, "hwnd", 0), (1, "text", "Hi"), (1, "caption", None), (1, "flags", 0)
MessageBox = prototype(("MessageBoxA", windll.user32), paramflags)

# For printing the stack
import sys
import traceback
from time import sleep

def show_popup(text):
    print text
    MessageBox(text=text, caption="Sample App Says...")

def mainloop():
    raise "Uff!"

if __name__ == '__main__':
    try:
        mainloop()
    except:
        type, value, sys.last_traceback = sys.exc_info()
        lines = traceback.format_exception(type, value,sys.last_traceback)
        show_popup("Aiyooooo..... there has been an error!\n" +
            "Exception in user code:\n" +
            "".join(lines) +
            "===== Please mail a screenshot to arunvr@gmail.com ===="
            )
    finally:
        sleep(1) # show the console output for a second so that users can read it
</code></pre><p><em>EDIT:</em> This is how it looks like in PyMail, one of my scripts-that-grew-into-an-app ;)</p><p><img
src="http://i17.tinypic.com/61oz9d3.jpg" alt="Screenshot of a Python Stacktrace in a Messagebox" /></p> ]]></content:encoded> <wfw:commentRss>http://www.arunrocks.com/blog/archives/2007/06/20/making-python-scripts-show-windows-friendly-errorsstacktrace/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss><!--
This site's performance optimized by W3 Total Cache. Dramatically improve the speed and reliability of your blog!

Learn more about our WordPress Plugins: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (user agent is rejected)
Database Caching 8/12 queries in 0.007 seconds using disk

Served from: mshawking.asmallorange.com @ 2010-07-31 11:27:28 -->