import traceback import StringIO from time import sleep done = False imported = False loadBroken = False runBroken = False lastError = None def getError(): """ Generates the traceback text for the most recent exception, and returns it as a string. """ fp = StringIO.StringIO() traceback.print_exc(file=fp) message = fp.getvalue() return message def genTraceback(ErrString): """ Takes an error message as an arguemnt. Prints the error message and the traceback. Sleeps for 2 seconds before returning. """ message = getError() print ErrString,"\n", message print "\nFix livegame_hackish.py to continue.\n" return message def handleException(tbPrinted, lastError, message): """ Calls function to print traceback. handles logic for only printing unique tracebacks. Sleeps for 2 seconds before returning. """ if not tbPrinted or lastError != getError(): genTraceback(message) sleep(2) return getError() # loop until we sucessfully import livegame_hackish.py while not imported: try: import livegame_hackish except: lastError = handleException(loadBroken, lastError ,"Failed to import livegame_hackish.py.") # set a flag so that the traceback is only printed # once loadBroken = True else: imported = True loadBroken = False lastError = None initWorked = False # loop until we sucessfully initialize livegame_hackish.py while not initWorked: try: livegame_hackish.initState() except: lastError = handleException(loadBroken, lastError ,"Failed to init livegame_hackish.py.") # set a flag so that the traceback is only printed # once initWorked = False else: initWorked = True loadBroken = False lastError = None initError = None initBroken = False runBroken = False # loop until livegame_hackish.play() returns True while not done: # attempt to reload the code try: reload (livegame_hackish) except: lastError = handleException(loadBroken, lastError ,"livegame_hackish.py is broken.") # if the reload fails, print the traceback once. loadBroken = True else: loadBroken = False # if the reload is succesful, try to run livegame_hackish.play() try: done = livegame_hackish.play() except: lastError = handleException(runBroken, lastError ,"play() in livegame_hackish.py is broken.") # hacked in reinit stuff: if not runBroken: print "Attempting to reinit..." # play() is broken, so try a reinit to fix things. try: livegame_hackish.initState() except: initError = handleException(initBroken, initError ,"initState() in livegame_hackish.py is broken.") initBroken = True runBroken = True else: runBroken = False triedReinit = False lastError = None initError = None initBroken = False