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

前端 未结 2 1515
执念已碎
执念已碎 2021-01-04 03:39

if I do something like

 >>> x = int(1,2,3,4,5)

I immediately get a fatal error (one that would end program execution if it was in

相关标签:
2条回答
  • 2021-01-04 03:51

    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
    }
    
    0 讨论(0)
  • 2021-01-04 04:00

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

    0 讨论(0)
提交回复
热议问题