This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: PyThreadState_SetAsyncExc and the main thread
Type: behavior Stage:
Components: Interpreter Core Versions: Python 2.5
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, pitrou, rotem_yaari, theller
Priority: normal Keywords:

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:26adminsetgithub: 45336
2009-10-18 18:22:47pitrousetstatus: open -> closed
resolution: works for me
messages: + msg94219
2009-10-18 18:19:09pitrousetnosy: + pitrou
messages: + msg94218
2008-07-11 13:30:04amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg69551
2008-05-29 22:20:50benjamin.petersonsettype: behavior
2008-05-29 19:04:43thellersetassignee: theller ->
2008-01-19 14:21:48christian.heimessetassignee: theller
nosy: + theller
2007-08-22 07:49:19rotem_yaaricreate