Created
April 14, 2012 19:05
-
-
Save psobot/2386993 to your computer and use it in GitHub Desktop.
Exception-Raising Thread
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Queue | |
| import threading | |
| import sys | |
| __author__ = 'psobot' | |
| class ExceptionThread(threading.Thread): | |
| def __init__(self, *args, **kwargs): | |
| threading.Thread.__init__(self, *args, **kwargs) | |
| self.__status_queue = Queue.Queue() | |
| def run(self, *args, **kwargs): | |
| try: | |
| threading.Thread.run(self) | |
| except Exception: | |
| self.__status_queue.put(sys.exc_info()) | |
| self.__status_queue.put(None) | |
| def join(self, num=None): | |
| if not self.__status_queue: | |
| return | |
| try: | |
| exc_info = self.__status_queue.get(True, num) | |
| if exc_info: | |
| raise exc_info[1], None, exc_info[2] | |
| except Queue.Empty: | |
| return | |
| self.__status_queue = None |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed this to properly raise exceptions with their original context. Before, all exceptions raised here would print:
Now, they show the original raiser of the exception: