Retrieving exception value in C

Discussion in 'Python' started by swapnil, Dec 23, 2009.

  1. swapnil

    swapnil Guest

    I am trying to retrieve the value of the exception (the message part)
    raised in python, in C.

    Running the below script,

    import shutil
    fd= open("testfile","w")
    fd.write("some junk")
    fd.close()
    shutil.copy("testfile","testfile")

    will generate an exception like this,

    Traceback (most recent call last):
    File "C:\Python26\lib\myscript.py", line 10, in <module>
    shutil.copy("testfile","testfile")
    File "C:\Python26\lib\shutil.py", line 88, in copy
    copyfile(src, dst)
    File "C:\Python26\lib\shutil.py", line 47, in copyfile
    raise Error, "`%s` and `%s` are the same file" % (src, dst)
    shutil.Error: `testfile` and `testfile` are the same file


    But if I run (actually import) the script from within a C code
    (embedding python in C), I am able to get the exception object but not
    the value.
    The code looks like,

    int main()
    {
    PyObject *exc, *val, *tbk, *module, *name;
    PyObject *exc_str;
    Py_Initialize();
    name = PyString_FromString("myscript");
    module = PyImport_Import(name);
    Py_DECREF(name);
    if(!module)
    {
    printf("error in running script\n");
    if( PyErr_Occurred())
    {
    if(PyErr_ExceptionMatches(PyExc_Exception))
    {
    printf("exception received in C\n");
    }
    PyErr_Fetch(&exc, &val, &tbk);
    exc_str = PyObject_Str(exc);
    printf("exception received: %s\n", PyString_AsString(exc_str));
    printf("exception value: %s\n",PyString_AsString(val));
    Py_DECREF(exc_str);
    Py_DECREF(exc);
    Py_DECREF(val);
    Py_DECREF(tbk);
    }
    else{
    printf("no exception received in C\n");
    }

    }
    Py_XDECREF(module);
    PyErr_Clear();
    Py_Finalize();
    return 0;
    }

    I get output,

    error in running script
    exception received in C
    exception received: <class 'shutil.Error'>
    exception value: (null)

    While the last line should be,

    exception value: `testfile` and `testfile` are the same file

    Although I think its not required, FYI I'm running python 2.6.2 on
    WinXP
    swapnil, Dec 23, 2009
    #1
    1. Advertising

  2. En Wed, 23 Dec 2009 04:24:04 -0300, swapnil <>
    escribió:

    > I am trying to retrieve the value of the exception (the message part)
    > raised in python, in C.
    >
    > But if I run (actually import) the script from within a C code
    > (embedding python in C), I am able to get the exception object but not
    > the value.
    > The code looks like,
    >
    > int main()
    > {
    > PyObject *exc, *val, *tbk, *module, *name;
    > PyObject *exc_str;
    > Py_Initialize();
    > name = PyString_FromString("myscript");
    > module = PyImport_Import(name);
    > Py_DECREF(name);
    > if(!module)
    > {
    > printf("error in running script\n");
    > if( PyErr_Occurred())
    > {
    > if(PyErr_ExceptionMatches(PyExc_Exception))
    > {
    > printf("exception received in C\n");
    > }
    > PyErr_Fetch(&exc, &val, &tbk);
    > exc_str = PyObject_Str(exc);
    > printf("exception received: %s\n", PyString_AsString(exc_str));
    > printf("exception value: %s\n",PyString_AsString(val));
    > Py_DECREF(exc_str);
    > Py_DECREF(exc);
    > Py_DECREF(val);
    > Py_DECREF(tbk);
    > }
    > else{
    > printf("no exception received in C\n");
    > }
    >
    > }
    > Py_XDECREF(module);
    > PyErr_Clear();
    > Py_Finalize();
    > return 0;
    > }
    >
    > I get output,
    >
    > error in running script
    > exception received in C
    > exception received: <class 'shutil.Error'>
    > exception value: (null)
    >
    > While the last line should be,
    >
    > exception value: `testfile` and `testfile` are the same file


    PyErr_fetch retrieves the exception *type*, the exception *instance*, and
    the traceback. So in your code exc is shutil.Error (the class) and val is
    the specific instance raised.
    The NULL comes from PyString_AsString(val) - it fails because val is not a
    string object.
    In Python code you'd get the message using str(val) - that would be
    written PyObject_Str(val) in C.

    --
    Gabriel Genellina
    Gabriel Genellina, Dec 23, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve C. Orr, MCSD
    Replies:
    0
    Views:
    811
    Steve C. Orr, MCSD
    Jul 22, 2003
  2. bill yeager
    Replies:
    1
    Views:
    356
    =?Utf-8?B?VHJldm9yIEJlbmVkaWN0IFI=?=
    Jun 7, 2004
  3. =?Utf-8?B?UFZWMzA=?=

    Out of Memory exception while retrieving data

    =?Utf-8?B?UFZWMzA=?=, Jul 28, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    4,025
    Alvin Bruney [MVP]
    Jul 28, 2004
  4. Praveen
    Replies:
    6
    Views:
    4,616
    Praveen
    Mar 1, 2005
  5. Samuel
    Replies:
    3
    Views:
    261
    Samuel
    Jun 22, 2007
Loading...

Share This Page