extending Python with C anywhere (was Re: Extending Python with C/C++ on a win32 platform)

David M. Cooke cookedm+news at physics.mcmaster.ca
Wed May 23 13:55:10 EDT 2001


At some point, "Alex Martelli" <aleaxit at yahoo.com> wrote:

> ::: bill.c
> #include "Python.h"
> 
> static PyObject *bill_module;
> staticforward PyTypeObject bill_Type;
> typedef struct {
>     PyObject_HEAD
>     PyObject* pdict;
> } billObject;
> 
> static billObject*
> bill_new(PyObject* pdict)
> {
>     billObject* newobj = PyObject_NEW(billObject, &bill_Type);
>     if(!newobj) return 0;
>     if(pdict) {
>         Py_INCREF(pdict);
>         newobj->pdict = pdict;
>     } else {
>         newobj->pdict = PyDict_New();
>     }
>     return newobj;
> }
> static void
> bill_dealloc(billObject* oldobj)
> {
>     Py_DECREF(oldobj->pdict);
>     PyMem_DEL(oldobj);
> }

Note that you're going to have problems with a python compiled with
pymalloc: PyObject_NEW and PyMem_DEL don't play nicely together. You
should be using PyObject_DEL. (Besides, PyMem_DEL is deprecated in 2.1)

-- 
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca



More information about the Python-list mailing list