Extension programming question (2.2)

Ken Seehof kseehof at neuralintegrator.com
Thu Jan 30 17:33:52 EST 2003


I am writing an extension type c_ni_object to be a base for
a class ni_object.  For some reason the type of an instance
of ni_object is actually c_ni_object.

 >>> class ni_object(c_ni_object):
...    pass

 >>> x = ni_object()
 >>> type(x)
<type 'c_ni_object'>

Presumably related to this, is another problem.  In the new()
function for c_ni_object, the 'type' argument is set to c_ni_object
when 'x' is instantiated as x = ni_object() (I'm expecting the type
to be ni_object).  This is important because I want my new()
function to know the derived class that is being instantiated.

When I experiment with built-in types such as int, I get the results
that I expect.  e.g.:

 >>> class pyint(int):
...    pass

 >>> n = pyint(5)
 >>> type(n)
<class 'PyCrust-Shell.pyint'>

Therefore I suspect that there is something wrong with my
c_ni_object code.  Any ideas?

Here's my code (stripped down and tested) which exhibits the
behavior described above:


#include "Python.h"

typedef struct {
	PyObject_HEAD
} c_ni_object__object;

staticforward PyTypeObject c_ni_object__type;     /* shared type-descriptor */

static PyObject *
c_ni_object__new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
     c_ni_object__object *self = PyObject_NEW(c_ni_object__object, 
&c_ni_object__type);
     return (PyObject *)self;
}

PyTypeObject c_ni_object__type = {
	PyObject_HEAD_INIT(&PyType_Type)
	0,                              // ob_size
	"c_ni_object",                  // tp_name
	sizeof(c_ni_object__object),    // tp_basicsize
	0,                              // tp_itemsize
	0, //(destructor)c_ni_object__dealloc,// tp_dealloc
	(printfunc)0,                   // tp_print
	0,//(getattrfunc)c_ni_object__getattr,// tp_getattr
	0,//(setattrfunc)c_ni_object__setattr,// tp_setattr
	(cmpfunc)0,                     // tp_compare
	0,//(reprfunc)c_ni_object__repr,    // tp_repr
	0,                              // tp_as_number
	0,                              // tp_as_sequence
	0,                              // tp_as_mapping
	(hashfunc)0,                    // tp_hash
	(ternaryfunc)0,                 // tp_call
	(reprfunc)0,                    // tp_str
	0,		/* tp_getattro */
	0,					/* tp_setattro */
	0,			/* tp_as_buffer */
	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
	"stripped down c_ni_object", // Documentation string
	0,					/* tp_traverse */
	0,					/* tp_clear */
	0,	/* tp_richcompare */
	0,					/* tp_weaklistoffset */
	0,					/* tp_iter */
	0,					/* tp_iternext */
	0, //c_ni_object__methods,				/* tp_methods */
	0,					/* tp_members */
	0,					/* tp_getset */
	0,					/* tp_base */
	0,					/* tp_dict */
	0,					/* tp_descr_get */
	0,					/* tp_descr_set */
	0,					/* tp_dictoffset */
	0,//(initproc)c_ni_object__init,					/* tp_init */
	0,					/* tp_alloc */
	c_ni_object__new,				/* tp_new */
};







More information about the Python-list mailing list