<?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"
	>

<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>
	<pubDate>Sat, 22 Nov 2008 05:49:53 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<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[     <link rel="alternate" type="application/atom+xml" title="ArunRocks Category: General" href="http://www.arunrocks.com/blog/archives/category/general/feed/" />
     <link rel="alternate" type="application/atom+xml" title="ArunRocks Category: Python" href="http://www.arunrocks.com/blog/archives/category/technical/python/feed/" />
     <link rel="alternate" type="application/atom+xml" title="ArunRocks Category: Technical" href="http://www.arunrocks.com/blog/archives/category/technical/feed/" />
     <link rel="alternate" type="application/atom+xml" title="ArunRocks Category: py2exe" href="http://www.arunrocks.com/blog/archives/category/py2exe/feed/" />
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>
<div>
<textarea name="code" class="python:nogutter" cols="60" rows="10">
    # 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

    </textarea>
</div>
<p><em>EDIT:</em> This is how it looks like in PyMail, one of my scripts-that-grew-into-an-app <img src='http://www.arunrocks.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><img src="http://i17.tinypic.com/61oz9d3.jpg" alt="Screenshot of a Python Stacktrace in a Messagebox" /><!--12850e4e5bbcaead4138d9450f16213b--></p>
<div class="socm_link">
<p class="bookm_links"> Bookmark this to      <a href="http://del.icio.us/post?url=http%3A%2F%2Fwww.arunrocks.com%2Fblog%2Farchives%2F2007%2F06%2F20%2Fmaking-python-scripts-show-windows-friendly-errorsstacktrace%2F&#038;title=Making+Python+Scripts+Show+Windows-friendly+Errors%2FStacktrace" id="socialm_del.icio.us" title="Submit this post to del.icio.us">del.icio.us</a> |       <a href="http://digg.com/submit?phase=2&#038;url=http%3A%2F%2Fwww.arunrocks.com%2Fblog%2Farchives%2F2007%2F06%2F20%2Fmaking-python-scripts-show-windows-friendly-errorsstacktrace%2F&#038;title=Making+Python+Scripts+Show+Windows-friendly+Errors%2FStacktrace" id="socialm_Digg" title="Submit this post to Digg">Digg</a> |       <a href="http://furl.net/storeIt.jsp?u=http%3A%2F%2Fwww.arunrocks.com%2Fblog%2Farchives%2F2007%2F06%2F20%2Fmaking-python-scripts-show-windows-friendly-errorsstacktrace%2F&#038;t=Making+Python+Scripts+Show+Windows-friendly+Errors%2FStacktrace" id="socialm_Furl" title="Submit this post to Furl">Furl</a> |       <a href=" http://www.netscape.com/submit/?U=http%3A%2F%2Fwww.arunrocks.com%2Fblog%2Farchives%2F2007%2F06%2F20%2Fmaking-python-scripts-show-windows-friendly-errorsstacktrace%2F&#038;T=Making+Python+Scripts+Show+Windows-friendly+Errors%2FStacktrace" id="socialm_Netscape" title="Submit this post to Netscape">Netscape</a> |       <a href="  http://www.google.com/bookmarks/mark?op=edit&#038;bkmk=http%3A%2F%2Fwww.arunrocks.com%2Fblog%2Farchives%2F2007%2F06%2F20%2Fmaking-python-scripts-show-windows-friendly-errorsstacktrace%2F&#038;title=Making+Python+Scripts+Show+Windows-friendly+Errors%2FStacktrace" id="socialm_Google Bookmarks" title="Submit this post to Google Bookmarks">Google Bookmarks</a> |       <a href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.arunrocks.com%2Fblog%2Farchives%2F2007%2F06%2F20%2Fmaking-python-scripts-show-windows-friendly-errorsstacktrace%2F" id="socialm_Technorati" title="Submit this post to Technorati">Technorati</a> |       <a href="http://reddit.com/submit?url=http%3A%2F%2Fwww.arunrocks.com%2Fblog%2Farchives%2F2007%2F06%2F20%2Fmaking-python-scripts-show-windows-friendly-errorsstacktrace%2F&#038;title=Making+Python+Scripts+Show+Windows-friendly+Errors%2FStacktrace" id="socialm_reddit" title="Submit this post to reddit">reddit</a> |  </p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.arunrocks.com/blog/archives/2007/06/20/making-python-scripts-show-windows-friendly-errorsstacktrace/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
