Issue 1779233: PyThreadState_SetAsyncExc and the main thread (original) (raw)

Issue1779233

Created on 2007-08-22 07:49 by rotem_yaari, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg32674 - (view) Author: Rotem (rotem_yaari) Date: 2007-08-22 07:49
Hi, The following does not work in python 2.5: ############################################## import ctypes import thread res = ctypes.pythonapi.PyThreadState_SetAsyncExc( thread.get_ident(), ctypes.py_object(SystemExit)) ############################################## Although according to my understanding this should "schedule" an async exception for the main thread, it does not (res receives the value of 0). When raising exceptions in other threads in this way, it works and the call to PyThreadState_SetAsyncExc returns 1 like it should. Doing so on the main thread doesn't seem to work, even when performed from threads other than the main one.
msg69551 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-07-11 13:30
Two things may prevent the exception from being seen: - First, the async exception is not immediate; it is checked every 100 bytecodes (=sys.getcheckinterval()). When testing from the interactive prompt, try something like "for x in range(100): pass". - Then, chances are that the exceptions is actually raised, but silently swallowed somewhere by the interpreter: for example, if the exception is raised from inside a __getattr__ function of some object, when called by hasattr(). SetAsyncExc does not seem very reliable...
msg94218 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-10-18 18:19
The following works (2.6 and trunk): import ctypes, thread ctypes.pythonapi.PyThreadState_SetAsyncExc( ctypes.c_long(thread.get_ident()), ctypes.py_object(ZeroDivisionError)) for i in range(1000): pass The thing to remember is that PyThreadState_SetAsyncExc() is asynchronous and doesn't guarantee that the exception will be raised timely (that's why I added a small busy loop above).
msg94219 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-10-18 18:22
I've added a test for it in r75499. Now closing this bug, still the function actually works :-)
History
Date User Action Args
2022-04-11 14:56:26 admin set github: 45336
2009-10-18 18:22:47 pitrou set status: open -> closedresolution: works for memessages: +
2009-10-18 18:19:09 pitrou set nosy: + pitroumessages: +
2008-07-11 13:30:04 amaury.forgeotdarc set nosy: + amaury.forgeotdarcmessages: +
2008-05-29 22:20:50 benjamin.peterson set type: behavior
2008-05-29 19:04:43 theller set assignee: theller ->
2008-01-19 14:21:48 christian.heimes set assignee: thellernosy: + theller
2007-08-22 07:49:19 rotem_yaari create