[C++-sig] [boost.python] PyErr_Fetch and object

Alex Mohr amohr at pixar.com
Thu Aug 28 19:03:12 CEST 2008


>> Yes. Something like this should work:
>>
>> void fetch_error(object &type, object &value, object &traceback)
>> {
>>  PyObject *t, *v, *b;
>>  PyErr_Fetch(&t, &v, &b);
>>  type = detail::new_reference(t);
>>  value = v ? detail::new_reference(v) : object();
>>  traceback = b ? detail::new_reference(b) : object();
>> }
>>
>> (There are more functions like this that could be added. Some day I'll 
>> submit a new patch...
> 
> Thanks, would be great. Anyway, In the source of boost.python I saw a 
> lot of used detail::.. stuff. The raw:pyobject header says this are to 
> hide to the user. What are the correspondent user functions for
> new_reference, borrowed_reference and new_non_null_reference which are 
> more in common sense of the python C/API documentation.

I tried to rewrite the above using more public-api.  Also I think the 
original function does not check 't' for NULL, and so may only be safely 
called if there is a python exception.  I tried to change it so this 
code will set all three objects to None if there is no exception.

I haven't tried compiling or testing this.

void fetch_error(object &type, object &value, object &traceback) {
     PyObject *t, *v, *b;
     PyErr_Fetch(&t, &v, &b);
     type = object(handle<>(allow_null(t)));
     value = object(handle<>(allow_null(v)));
     traceback = object(handle<>(allow_null(b));
}

Alex




More information about the Cplusplus-sig mailing list