PyErr_SetObject weirdness (HELP!)

Jason Tackaberry tack-py at sault.org
Thu Mar 2 23:01:43 EST 2000


I'm experiencing some weird behaviour with PyErr_SetObject, and I need
some guidance from you Python Gods. :)

I have a function that passes an instance of a class as the value
parameter
of PyErr_SetObject.  But in fact, I'm experiencing the weirdness
described
below when I pass any object (except a string object), so for the sake
of
simplicity, let's say I'm passiing a dict.  Consider:

PyObject *dict = PyDict_New();
PyDict_SetItemString(dict, "foo", PyString_FromString("bar"));
// UserException is defined somewhere else
PyErr_SetObject(UserException, dict);

Now when I call this code from Python:

try: call_C_function_that_raises_exception()
except UserException, data:
   print data["foo"]

I would expect this should work, but it doesn't!  Referencing
data["foo"]
raises this exception:

Traceback (innermost last):
  File "./blah", line 15, in ?
    print data["foo"]
  File "/usr/lib/python1.5/exceptions.py", line 77, in __getitem__
    return self.args[i]
TypeError: sequence index must be integer

In fact, the magic incantation is:
   print data[0]["foo"]

I have _no_ idea why it is doing this, especially because:

   print data
   print data[0]
yields:
   {'foo': 'bar'}
   {'foo': 'bar'}

Which clearly _look_ the same.

My question is why is this happening, and how can I make it work the way

one would expect?

Please CC your reply to my email address (tack-py at sault.org).

Thanks for reading this far. :)
Jason




More information about the Python-list mailing list