I am trying this out, parts of it are direct from the Python 2.4
manual.
//----------- icallbk.c
#include "Python.h"
PyObject *fcallback = NULL;
static PyObject*
call_func(PyObj ect* self, PyObject* args)
{
PyObject *result;
PyObject *arglist;
int arg = 123;
arglist = Py_BuildValue(" (i)", arg);
result = PyEval_CallObje ct(fcallback, arglist);
Py_DECREF(argli st);
Py_DECREF(resul t);
Py_RETURN_NONE;
}
static PyObject *
set_callback(Py Object *self, PyObject *args)
{
PyObject *result = NULL;
PyObject *temp;
if (PyArg_ParseTup le(args, "O:set_callback ", &temp)) {
if (!PyCallable_Ch eck(temp)) {
PyErr_SetString (PyExc_TypeErro r, "parameter must be
callable");
return NULL;
}
Py_XINCREF(temp ); /* Add a reference to new callback */
Py_XDECREF(fcal lback); /* Dispose of previous callback */
fcallback = temp; /* Remember new callback */
/* Boilerplate to return "None" */
Py_INCREF(Py_No ne);
result = Py_None;
}
return result;
}
static PyMethodDef icallbk_methods[] = {
{ "call_func" , call_func, METH_VARARGS, "call_func" },
{ "set_callba ck", set_callback, METH_VARARGS, "set_callba ck" },
{ 0,0,0,0 }
};
PyMODINIT_FUNC
initicallbk(voi d)
{
(void) Py_InitModule(" icallbk", icallbk_methods );
}
"""
I use the following code to run the extension above.
"""
# cb.py: Code to run the extension above.
import icallbk as c
def f(a): print a
c.set_callback( f)
c.call_func()
"""
But when I run that for a hundred times, I think the system is losing
100KB or thereabouts that is no longer re-claimed. Can someone confirm
this, and if it so, kindly inform me what is wrong with the code?
Thanks
manual.
//----------- icallbk.c
#include "Python.h"
PyObject *fcallback = NULL;
static PyObject*
call_func(PyObj ect* self, PyObject* args)
{
PyObject *result;
PyObject *arglist;
int arg = 123;
arglist = Py_BuildValue(" (i)", arg);
result = PyEval_CallObje ct(fcallback, arglist);
Py_DECREF(argli st);
Py_DECREF(resul t);
Py_RETURN_NONE;
}
static PyObject *
set_callback(Py Object *self, PyObject *args)
{
PyObject *result = NULL;
PyObject *temp;
if (PyArg_ParseTup le(args, "O:set_callback ", &temp)) {
if (!PyCallable_Ch eck(temp)) {
PyErr_SetString (PyExc_TypeErro r, "parameter must be
callable");
return NULL;
}
Py_XINCREF(temp ); /* Add a reference to new callback */
Py_XDECREF(fcal lback); /* Dispose of previous callback */
fcallback = temp; /* Remember new callback */
/* Boilerplate to return "None" */
Py_INCREF(Py_No ne);
result = Py_None;
}
return result;
}
static PyMethodDef icallbk_methods[] = {
{ "call_func" , call_func, METH_VARARGS, "call_func" },
{ "set_callba ck", set_callback, METH_VARARGS, "set_callba ck" },
{ 0,0,0,0 }
};
PyMODINIT_FUNC
initicallbk(voi d)
{
(void) Py_InitModule(" icallbk", icallbk_methods );
}
"""
I use the following code to run the extension above.
"""
# cb.py: Code to run the extension above.
import icallbk as c
def f(a): print a
c.set_callback( f)
c.call_func()
"""
But when I run that for a hundred times, I think the system is losing
100KB or thereabouts that is no longer re-claimed. Can someone confirm
this, and if it so, kindly inform me what is wrong with the code?
Thanks