Creating exception class with custom fields in Python/C API

T

ty ty

Hello, list!

I'm writing a wrapper for C-library. When something goes wrong in that library, i can get error details. And i want to assign them to fields of my own exception class.

For this purpose, i looked throught Modules/_ctypes/_ctypes.c (in python source tree) and implemented same things. Briefly:

define PyObject * Error in header file,
write init and other necessary functions
assign them to PyMethodDef array
initialize class's dict with methods above (in function create_error)
create new exception with no base class and with dict (in function create_error)
call to create_error and assign Error class to module (PyModule_AddObject)
throw error with PyErr_SetObject(Error, tpl);, where tpl is tuple with error details, which are assigned to Error's fields in init function
All of this is consistent with what i saw in Modules/_ctypes/_ctypes.c, i think. But it doesn't work: when i call PyErr_SetObject(Error, tpl);, Python's runtime raises error:
TypeError: unbound method __init__() must be called with Error instance as first argument (got str instance instead)

And PyObject_Print(Error, stdout, 0); returns:
{'__init__': <unbound method Error.__init__> ...}

It's item for init function in PyMethodDef array:
{"__init__", myerror_init, METH_VARARGS, "initialize error"}

and it's function's signature:
static PyObject * myerror_init(PyObject * self, PyObject *args)

(python version -- 2.6.4)

Why methods are unbound? And what i've missed? Or what is the right and pythonic way to define exception with custom class attributes?

Thanks.

(crosspost from stackoverflow: http://stackoverflow.com/questions/...tion-class-with-custom-fields-in-python-c-api )
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,068
Latest member
MakersCBDIngredients

Latest Threads

Top