Extension programming question (2.2)

Michael Hudson mwh at python.net
Fri Jan 31 10:49:32 EST 2003


Ken Seehof <kseehof at neuralintegrator.com> writes:

> 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() 

You sure?  It's not in my tests.

> 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);

I just changed this to PyObject_NEW(c_ni_object__object, type) and it
"worked" (as in, I could create an instance, but got a core dump some
time later).  I think you need to set tp_alloc to PyType_GenericAlloc
and call that instead:

     c_ni_object__object *self = 
            (c_ni_object__object*)type->tp_alloc(type, 0);

The file Modules/_randommodule.c in Python CVS seems to be quite a
nice example of a minimal-ish C base class.

HTH,
M.

-- 
  The gripping hand is really that there are morons everywhere, it's
  just that the Americon morons are funnier than average.
                              -- Pim van Riezen, alt.sysadmin.recovery




More information about the Python-list mailing list