threads and exception in wxPython

Peter Hansen peter at engcorp.com
Wed Nov 3 10:17:35 EST 2004


Peter Hansen wrote:
>  >>> py.PyThreadState_SetAsyncExc(id(t), byref(exc))
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: expected CData instance

First error in the above: the thread id is not id(thread)
but the value returned by thread.start_new_thread or the
value returned by threading._get_ident().

Also ctypes provides a py_object class which I have no
idea how to use but could guess at wildly.

Also, ctypes provides a "pythonapi" thingy which should
be used in preference to cdll.python23, so the code
becomes more like this:

 >>> asyncexc = ctypes.pythonapi.PyThreadState_SetAsyncExc
 >>> exc = ctypes.py_object(ValueError)
 >>> asyncexc(t.id, exc)
1
 >>> t
<T(Thread-2, started)>

Hmm... clearly that's not enough, but perhaps closer.

(I find it hard to believe nobody has tried this yet, unless
it's the case that those who know how to do it also know that
it's actually not possible for some reason...)

-Peter



More information about the Python-list mailing list