From: Austin Bingham on
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
From: Gabriel Genellina on
En Fri, 29 Jan 2010 11:37:09 -0300, Austin Bingham
<austin.bingham(a)gmail.com> escribi�:

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

It's simple: you have to check *every* function call for failure. Many
functions return new object references and you have to properly decrement
them in case of failure, so in most cases this means that you have to
check each and every call.

--
Gabriel Genellina

From: Austin Bingham on
Maybe I'm not following what you're saying. In my case, I already know
that an exception has been thrown. In the course of processing that
exception, I call another function which, for whatever reason and even
when it succeeds, clears the exception indicators. How can I address
this issue by checking function calls for failure?

Austin

On Fri, Jan 29, 2010 at 9:04 PM, Gabriel Genellina
<gagsl-py2(a)yahoo.com.ar> wrote:
> En Fri, 29 Jan 2010 11:37:09 -0300, Austin Bingham
> <austin.bingham(a)gmail.com> escribió:
>
>> 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.
>
> It's simple: you have to check *every* function call for failure. Many
> functions return new object references and you have to properly decrement
> them in case of failure, so in most cases this means that you have to check
> each and every call.
>
> --
> Gabriel Genellina
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
From: Gabriel Genellina on
En Fri, 29 Jan 2010 18:25:14 -0300, Austin Bingham
<austin.bingham(a)gmail.com> escribi�:

> Maybe I'm not following what you're saying. In my case, I already know
> that an exception has been thrown. In the course of processing that
> exception, I call another function which, for whatever reason and even
> when it succeeds, clears the exception indicators. How can I address
> this issue by checking function calls for failure?

Maybe if you provide an actual use case we can suggest how to handle it.
The code in your original post does not make any sense to me (except by
showing that PyImport_ImportModule does clear the error indicator). If you
already know there was an error, and you even have retrieved the error
details, why do you care if the error indicator gets reset?

--
Gabriel Genellina

From: Austin Bingham on
The original post was, in a nutshell, the "use case"; it's very scaled
down the from the real example, and not intended to "make sense". The
notion on which I was (apparently incorrectly) basing my exception
translation was that I could 1) get and reset references to the error
indicators, 2) call other python methods that don't themselves throw,
and 3) later retrieve the same "active" exceptions. I was relying on
this ability to "re-fetch" exceptions insofar as the functions in my
code which dealt with python exceptions all looked up the exception
separately. The predicate that "a successful function won't modify the
error indicators" appears to be wrong, however, and I've modified my
code accordingly.

Austin

On Sat, Jan 30, 2010 at 1:11 AM, Gabriel Genellina
<gagsl-py2(a)yahoo.com.ar> wrote:
> En Fri, 29 Jan 2010 18:25:14 -0300, Austin Bingham
> <austin.bingham(a)gmail.com> escribió:
>
>> Maybe I'm not following what you're saying. In my case, I already know
>> that an exception has been thrown. In the course of processing that
>> exception, I call another function which, for whatever reason and even
>> when it succeeds, clears the exception indicators. How can I address
>> this issue by checking function calls for failure?
>
> Maybe if you provide an actual use case we can suggest how to handle it. The
> code in your original post does not make any sense to me (except by showing
> that PyImport_ImportModule does clear the error indicator). If you already
> know there was an error, and you even have retrieved the error details, why
> do you care if the error indicator gets reset?
>
> --
> Gabriel Genellina
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
 |  Next  |  Last
Pages: 1 2
Prev: Killing a Thread
Next: SimpleXMLRPCServer daemon