Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Some C-API functions clear the error indicator?

Reply
Thread Tools

Some C-API functions clear the error indicator?

 
 
Austin Bingham
Guest
Posts: n/a
 
      01-29-2010
I've noticed that several (many?) python functions seem to clear the
error/exception indicators when they're called from a C/C++ program.
For example, both PyImport_ImportModule and traceback.extract_tb()
(called via the function call methods) do this: if error indicators
are set prior to their call (as indicated by PyErr_Fetch, and
including a call to PyErr_Restore), I see that they are unset (using
the same method) after the call. This happens even when the functions
succeed.

The functions that do this don't seem to indicate in their
documentation that this will happen. So first, does anyone know why
this is happening? Is it because of the context in which I'm making
the calls? Is there any pattern or reason behind which functions will
do this? Or am I just doing something wrong?

If the problem is context-dependent (e.g. I haven't properly
established a call stack, or something of that flavor), any pointers
on doing things properly would be great.

Here's some example code demonstrating the problem:

---

#include <Python.h>

int main(int argc, char** argv)
{
Py_Initialize();

// Cause an IndexError
PyObject* list = PyList_New(0);
PyObject* obj = PyList_GetItem(list, 100);

PyObject *t = NULL, *v = NULL, *tb = NULL;

// Verify that we see the error
PyErr_Fetch(&t, &v, &tb);
assert(t);
PyErr_Restore(t, v, tb);

// Import a module, which seems to be clearing the error indicator
PyObject* mod = PyImport_ImportModule("sys");
assert(PyObject_HasAttrString(mod, "path"));

// Verify that the error indicator has been cleared
PyErr_Fetch(&t, &v, &tb);
assert(!t); // <=== The error is gone!
PyErr_Restore(t, v, tb);

Py_Finalize();

return 0;
}

---

Thanks in advance.

Austin
 
Reply With Quote
 
 
 
 
Austin Bingham
Guest
Posts: n/a
 
      01-30-2010
That makes a lot of sense. And if I take the approach that any Py*
function might do this, it actually looks like I can simplify my code
(rather than managing some list of ill-behaved functions or
something.) Thanks!

On Fri, Jan 29, 2010 at 3:58 PM, Duncan Booth
<(E-Mail Removed)> wrote:
> Austin Bingham <(E-Mail Removed)> wrote:
>
>> The functions that do this don't seem to indicate in their
>> documentation that this will happen. So first, does anyone know why
>> this is happening? Is it because of the context in which I'm making
>> the calls? Is there any pattern or reason behind which functions will
>> do this? Or am I just doing something wrong?
>>

> (Just guessing here)
> I would expect that any function that executes Python code will clear the
> error.
>
> I think that has to happen otherwise the Python code will throw an
> exception whenever it gets round to checking for errors. In the past I've
> found that if you fail to check for an error in C code before returning to
> the interpreter you get the exception thrown a few instructions later, so
> something similar would probably happen if you call other Python code from
> C.
>
> If it is anything that executes Python then that would include any function
> that creates or destroys an object with Python constructor or destructor
> code. or that compares or otherwise operates on instances defined in
> Python. In particular it might mean that any function that doesn't appear
> to clear the error could do so in a slightly different situation.
>
> --
> Duncan Booth http://kupuguy.blogspot.com
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Response.Clear() doesn't clear David ASP .Net 2 01-31-2008 08:32 PM
Unrecognized element 'add' after <clear></clear> InvalidLastName ASP .Net Web Services 3 03-06-2007 03:07 AM
STL clear() functions jois.de.vivre@gmail.com C++ 4 09-29-2006 03:37 PM
Still not clear about global functions Alan Silver ASP .Net 14 06-21-2005 10:41 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM



Advertisments