How do I properly use Python's C API and exceptions?

空扰寡人 提交于 2019-12-01 03:01:33

Raising an exception in C is done by setting the exception object or string and then returning NULL from the function.

As Ignacio Vazquez-Abrams said:

Raising an exception in C is done by setting the exception object or string and then returning NULL from the function.

There are convenience functions which make this easy to do for common exception types. For example, PyErr_NoMemory can be used like this:

PyObject *my_function(void)
{
    return PyErr_NoMemory();  // Sets the exception and returns NULL
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!