import traceback import StringIO from time import sleep done = False imported = False loadBroken = False runBroken = False def genTraceback(ErrString): """ Takes an error message as an arguemnt. Generates a traceback of the most recent exception, and prints it with the error message. Sleeps for 2 seconds before returning. """ fp = StringIO.StringIO() traceback.print_exc(file=fp) message = fp.getvalue() print ErrString,"\n", message print "\nFix livegame.py to continue.\n" sleep(2) # loop until we sucessfully import livegame.py while not imported: try: import livegame except: if not loadBroken: genTraceback("Failed to import livegame.py.") # set a flag so that the traceback is only printed # once loadBroken = True else: imported = True # loop until livegame.play() returns True while not done: # attempt to reload the code try: reload (livegame) except: # if the reload fails, print the traceback once. if not loadBroken: genTraceback("livegame.py is broken.") loadBroken = True else: loadBroken = False # if the reload is succesful, try to run livegame.play() try: done = livegame.play() except: if not runBroken: genTraceback("play() in livegame.py is broken.") runBroken = True else: runBroken = False