首页 > 解决方案 > “PyThreadState_SetAsyncExc”导致“SystemError:异常 Exception()不是 BaseException 子类”

问题描述

我正在用线程和钩子编写 Python 扩展模块。我需要从我的一个线程向 Python 主线程抛出异常。为此我使用int PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc). 在文档中说:

exc是要引发的异常对象

没有什么比这更容易的了。但是当我PyThreadState_SetAsyncExcException(实际上PyExc_Exception)实例调用时exc,我得到

SystemError: exception Exception('Error text') not a BaseException subclass

我可以阅读:),所以我尝试不放置类实例(我相信如文档中所述),而是直接放置类对象exc并且它可以工作。但是,因此,我无法向它传递参数。

此代码导致主线程中的系统错误:

PyGILState_STATE gstate = PyGILState_Ensure();
PyThreadState_SetAsyncExc(self->main_thread_id, PyObject_CallObject(PyExc_Exception, Py_BuildValue("(s)", "Error text")));
PyGILState_Release(gstate);

Exception此代码导致主线程中的预期:

PyGILState_STATE gstate = PyGILState_Ensure();
PyThreadState_SetAsyncExc(self->main_thread_id, PyExc_Exception);
PyGILState_Release(gstate);

完整的错误文本,IDLE 显示:

Traceback (most recent call last):
** IDLE Internal Exception: 
  File "C:\Python37-32\lib\idlelib\run.py", line 137, in main
    request = rpc.request_queue.get(block=True, timeout=0.05)
  File "C:\Python37-32\lib\queue.py", line 179, in get
    self.not_empty.wait(remaining)
  File "C:\Python37-32\lib\threading.py", line 300, in wait
    gotit = waiter.acquire(True, timeout)
SystemError: exception Exception('Error text') not a BaseException subclass

所以,我希望PyThreadState_SetAsyncExc支持异常实例对象,但看起来它只支持类/类型对象。我对么?或者也许我做错了?

标签: pythonmultithreadingexceptionpython-c-api

解决方案


是的,你是对的。您需要指定异常类型,而不是实例。文档可能对此更清楚。

如果要为异常提供自定义错误消息,可以使用该PyErr_NewException函数创建自己的异常类型:

PyGILState_STATE gstate = PyGILState_Ensure();
PyObject* exc = PyErr_NewException("mymodule.MyError: error message", NULL, NULL);
PyThreadState_SetAsyncExc(self->main_thread_id, exc);
Py_DECREF(exc);
PyGILState_Release(gstate);

推荐阅读