~ipython-contrib/+junk/ipython-zmq

« back to all changes in this revision

Viewing changes to IPython/gui/wx/ThreadEx.py

  • Committer: ville
  • Date: 2008-02-16 09:50:47 UTC
  • mto: (0.12.1 ipython_main)
  • mto: This revision was merged to the branch mainline in revision 990.
  • Revision ID: ville@ville-pc-20080216095047-500x6dluki1iz40o
initialization (no svn history)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import threading
 
2
import inspect
 
3
import ctypes
 
4
 
 
5
 
 
6
def _async_raise(tid, exctype):
 
7
    """raises the exception, performs cleanup if needed"""
 
8
    if not inspect.isclass(exctype):
 
9
        raise TypeError("Only types can be raised (not instances)")
 
10
    res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
 
11
    if res == 0:
 
12
        raise ValueError("invalid thread id")
 
13
    elif res != 1:
 
14
        # """if it returns a number greater than one, you're in trouble, 
 
15
        # and you should call it again with exc=NULL to revert the effect"""
 
16
        ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, 0)
 
17
        raise SystemError("PyThreadState_SetAsyncExc failed")
 
18
 
 
19
 
 
20
class Thread(threading.Thread):
 
21
    def _get_my_tid(self):
 
22
        """determines this (self's) thread id"""
 
23
        if not self.isAlive():
 
24
            raise threading.ThreadError("the thread is not active")
 
25
        
 
26
        # do we have it cached?
 
27
        if hasattr(self, "_thread_id"):
 
28
            return self._thread_id
 
29
        
 
30
        # no, look for it in the _active dict
 
31
        for tid, tobj in threading._active.items():
 
32
            if tobj is self:
 
33
                self._thread_id = tid
 
34
                return tid
 
35
        
 
36
        raise AssertionError("could not determine the thread's id")
 
37
    
 
38
    def raise_exc(self, exctype):
 
39
        """raises the given exception type in the context of this thread"""
 
40
        _async_raise(self._get_my_tid(), exctype)
 
41
    
 
42
    def kill(self):
 
43
        """raises SystemExit in the context of the given thread, which should 
 
44
        cause the thread to exit silently (unless caught)"""
 
45
        self.raise_exc(SystemExit)