import traceback import StringIO from time import sleep done = False imported = False loadBroken = False runBroken = False reinitBroken = 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. """ message = getError() print ErrString,"\n", message print "\nFix livegame_stateless.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_stateless.py while not imported: try: import livegame_stateless except: lastError = handleException(loadBroken, lastError ,"Failed to import livegame_stateless.py.") # set a flag so that the traceback is only printed # once loadBroken = True else: imported = True loadBroken = False lastError = None framecount = 0 # loop until livegame_stateless.play() returns True while not done: # attempt to reload the code try: reload (livegame_stateless) except: lastError = handleException(loadBroken, lastError ,"livegame_stateless.py is broken.") # if the reload fails, print the traceback once. loadBroken = True else: loadBroken = False lastError = None # if the reload is succesful, try to run livegame_stateless.play() try: done = livegame_stateless.play(framecount) except: lastError = handleException(runBroken, lastError ,"play() in livegame_stateless.py is broken.") runBroken = True #livegame.reinit() # this is a bit of a hack else: runBroken = False triedReinit = False framecount += 1 lastError = None