Extension programming question (2.2)

Ken Seehof kseehof at neuralintegrator.com
Fri Jan 31 15:13:57 EST 2003


At 07:49 AM 1/31/2003 Friday, you wrote:
>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.

Well that's what it was doing yesterday, I swear it! :-)  Today, type is
ni_object.  Hmm, must have something to do with the day of the week.

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

Well, that explains it.  Silly mistake.  BTW, I implemented tp_new instead
of using PyType_GenericAlloc because I wanted some initialization (deleted
in  my sample code for simplicity).  Actually as it turns out I can work with
tp_init instead.

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

Thanks Michael,
- Ken








More information about the Python-list mailing list