C API : setting the message of an exception

B

Benoit Dejean

(sorry for my english)

when an excpetion is raised (like Overflow), before i return NULL, i'd
like to modify the message held by the exception. I use PyErr_SetString,
but i don't know how to retreive the current message

excaption is raised -> extending error message -> raising exception

thank you
 
T

Thomas Heller

Benoit Dejean said:
(sorry for my english)

when an excpetion is raised (like Overflow), before i return NULL, i'd
like to modify the message held by the exception. I use PyErr_SetString,
but i don't know how to retreive the current message

excaption is raised -> extending error message -> raising exception

thank you

I have used this code:

void Extend_Error_Info(char *fmt, ...)
{
va_list vargs;
PyObject *tp, *v, *tb, *s;

va_start(vargs, fmt);
s = PyString_FromFormatV(fmt, vargs);
va_end(vargs);
if (!s)
return;

PyErr_Fetch(&tp, &v, &tb);
PyString_ConcatAndDel(&s, v);
PyErr_Restore(tp, s, tb);
}

and then you can:

if (result == NULL) {
Extend_Error_Info("I wasn't expecting %s", "the spanish inquisition");
}

although I'm not sure what happens when PyString_FromFormatV() fails.

Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,170
Messages
2,570,921
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top