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

Stefan Seefeld seefeld at sympatico.ca
Thu Aug 28 19:14:15 CEST 2008


Alex Mohr wrote:
>>
>> 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));
> }

Thanks ! Actually, the lack of a check for 'type' was intentional: Even 
if an error is set, 'value' and 'traceback' may be set to NULL.
'type' can only be NULL if no error is set, and thus has to be handled 
special. (For example, we could throw an exception in that case, as 
calling fetch_error if no error is set is an error (or may be, if we 
make it so).

Of course, with that then come other function wrappers for 'restore', 
'clear', etc.

I'll think about a patch, if no-one else beats me to it <wink/>

Regards,
       Stefan

-- 

      ...ich hab' noch einen Koffer in Berlin...




More information about the Cplusplus-sig mailing list