C API : setting the message of an exception

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Benoit Dejean

    C API : setting the message of an exception

    (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
  • Thomas Heller

    #2
    Re: C API : setting the message of an exception

    Benoit Dejean <bnet@ifrance.c om> writes:
    [color=blue]
    > (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[/color]

    I have used this code:

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

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

    PyErr_Fetch(&tp , &v, &tb);
    PyString_Concat AndDel(&s, v);
    PyErr_Restore(t p, s, tb);
    }

    and then you can:

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

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

    Thomas

    Comment

    Working...