Modifying or Reraising Python error in C API
问题 I have a bit of code that tries to parse an object as an integer: long val = PyLong_AsLong(obj); if(val == -1 && PyErr_Occurred()) { return -1; } Here obj is a vanilla PyObject * , and PyLong_AsLong raises a very generic TypeError if obj is not an integer. I would like to transform the error message into something a bit more informative, so I would like to either modify the existing error object, or to reraise it. My current solution is to do this: long val = PyLong_AsLong(obj); if(val == -1