# tested with python 2.6 and 3.1 def exc_browse(browser = None): """exc_browser() --> True on success, False on failure Display the last exception raised in a web browser. This function is meant to be used in except statements (see example below). The optional 'browser' argument is a string which can have a value accepted by webbrowser.get(). A temporary html file is created (named after the current process pid). example: try: c = "hello world" except IndexError: exc_browse() # displays the exception in the web browser """ import os, sys, cgitb html = cgitb.html(sys.exc_info()) tdir = None if sys.platform == 'linux2' and os.path.isdir('/dev/shm'): # under linux, try to create the temporary file in shared memory tdir = '/dev/shm' if not os.path.isdir(tdir): tdir = None if tdir is None: from tempfile import gettempdir tdir = gettempdir() tdir = os.path.join(tdir, "exc_browse") if not os.path.isdir(tdir): try: os.mkdir(tdir) except OSError: return False name = "exc%d.html" % os.getpid() fname = os.path.join(tdir, name) try: if os.path.isfile(fname): try: os.unlink(fname) except OSError: return False f = open(fname, "w") f.write(html) f.close() except (OSError, IOError): return False import webbrowser if browser is not None: w = webbrowser.get(browser) else: w = webbrowser return w.open(fname)
Are you able to help answer this sponsored question?
Questions asked by members who have earned a lot of community kudos are featured in order to give back and encourage quality replies.