python.py (2690B)
1 # test python (sample from offlineimap) 2 3 4 class ExitNotifyThread(Thread): 5 """This class is designed to alert a "monitor" to the fact that a thread has 6 exited and to provide for the ability for it to find out why.""" 7 8 def run(self): 9 global exitthreads, profiledir 10 self.threadid = thread.get_ident() 11 try: 12 if not profiledir: # normal case 13 Thread.run(self) 14 else: 15 try: 16 import cProfile as profile 17 except ImportError: 18 import profile 19 prof = profile.Profile() 20 try: 21 prof = prof.runctx("Thread.run(self)", globals(), locals()) 22 except SystemExit: 23 pass 24 prof.dump_stats( 25 profiledir 26 + "/" 27 + str(self.threadid) 28 + "_" 29 + self.getName() 30 + ".prof" 31 ) 32 except: 33 self.setExitCause("EXCEPTION") 34 if sys: 35 self.setExitException(sys.exc_info()[1]) 36 tb = traceback.format_exc() 37 self.setExitStackTrace(tb) 38 else: 39 self.setExitCause("NORMAL") 40 if not hasattr(self, "exitmessage"): 41 self.setExitMessage(None) 42 43 if exitthreads: 44 exitthreads.put(self, True) 45 46 def setExitCause(self, cause): 47 self.exitcause = cause 48 49 def getExitCause(self): 50 """Returns the cause of the exit, one of: 51 'EXCEPTION' -- the thread aborted because of an exception 52 'NORMAL' -- normal termination.""" 53 return self.exitcause 54 55 def setExitException(self, exc): 56 self.exitexception = exc 57 58 def getExitException(self): 59 """If getExitCause() is 'EXCEPTION', holds the value from 60 sys.exc_info()[1] for this exception.""" 61 return self.exitexception 62 63 def setExitStackTrace(self, st): 64 self.exitstacktrace = st 65 66 def getExitStackTrace(self): 67 """If getExitCause() is 'EXCEPTION', returns a string representing 68 the stack trace for this exception.""" 69 return self.exitstacktrace 70 def setExitMessage(self, msg): 71 """Sets the exit message to be fetched by a subsequent call to 72 getExitMessage. This message may be any object or type except 73 None.""" 74 self.exitmessage = msg 75 76 def getExitMessage(self): 77 """For any exit cause, returns the message previously set by 78 a call to setExitMessage(), or None if there was no such message 79 set.""" 80 return self.exitmessage