C API : setting the message of an exception

Thomas Heller theller at python.net
Fri Nov 14 14:56:18 EST 2003


Benoit Dejean <bnet at ifrance.com> writes:

> (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




More information about the Python-list mailing list