calling a Python method from C

Thierry Cornelis thierry.cornelis at easynet_NO_.be_SPAM
Wed Oct 16 11:04:58 EDT 2002


Hello,

 From C I am trying to build a datstructure in Python. I am trying to do 
this by a callback.
I have 3 classes:
  node
  number_node
  operator_node
number_node and operator_node are child classes of node.
I am able to create objects of the classes number_node and operator_node.
Now I want to call a method, add_operand, that is in the operator_node 
class. I do not know how.
Some help is welcome.


Thank you,
Thierry


This is the work I have done:


 >>>>>node.py<<<<<

# class that implements the nodes that form the tree

class node:
   '''representation of a node'''

   node_nr = 0
   x_coor = -1
   y_coor = -1




 >>>>>number_node.py<<<<<

from node import *

class number_node(node) :
   '''class representing a number node'''

   value = 'NONE'

   def write (self) :
    print '# number node'
    print 'x_coor = '
    print self.x_coor
    print 'y_coor = '
    print self.y_coor
    print 'value = '
    print self.value




 >>>>>operator_node.py<<<<<

from node import *

class operator_node (node) :
   '''class representing an operator node'''

   operator = 'NONE'
   operands = []

   def add_operand (self, operand) :
     self.operands = self.operands + [operand]

   def write (self) :
     print '#operator node'
     print 'operator = '
     print self.operator
     print 'x_coor = '
     print self.x_coor
     print 'y_coor = '
     print self.y_coor
     print 'args = ['
     for a in self.operands:
       a.write()
       print ','
     print ']'





 >>>>>node_creator.c<<<<<

#include <Python.h>

static PyObject *
creat_number_node (PyObject *pself, PyObject *pargs)
{
   PyObject *node;
   PyObject *args;
   PyObject *class;
   PyObject *mod;
   mod = PyImport_ImportModule ("number_node");
   class = PyObject_GetAttrString (mod, "number_node");
   args = Py_BuildValue("()");
   if (class == NULL)
     {
       PyErr_SetString (PyExc_TypeError, "Can't get module.class");
       exit (1);
     }
   node = PyEval_CallObject (class, args);
   Py_DECREF (class);
   Py_DECREF (args);
   if (node == NULL)
     {
       PyErr_SetString (PyExc_TypeError, "Can't call module.class()");
       exit (1);
     }
   return node;
}

static PyObject *
create_operator_node (PyObject *pself, PyObject *pargs)
{
   PyObject *node;
   PyObject *args;
   PyObject *class;
   PyObject *mod;
   mod = PyImport_ImportModule ("operator_node");
   class = PyObject_GetAttrString (mod, "operator_node");
   args = Py_BuildValue("()");
   if (class == NULL)
     {
       PyErr_SetString (PyExc_TypeError, "Can't get module.class");
       exit (1);
     }
   node = PyEval_CallObject (class, args);
   Py_DECREF (class);
   Py_DECREF (args);
   if (node == NULL)
     {
       PyErr_SetString (PyExc_TypeError, "Can't call module.class()");
       exit (1);
     }
   return node;
}

static PyObject *
add_operand_c (PyObject *pself, PyObject *pargs)
{
   PyObject *operator;
   PyObject *operand;
   PyObject *mod;
   PyObject *node;
   mod = PyImport_ImportModule ("operator_node");
   PyArg_ParseTuple (pargs, "OO", &operator, &operand);
   node = PyObject_CallMethod (operator, "add_operand", "O", operand);
   if (node == NULL)
     {
       PyErr_SetString (PyExc_TypeError, "Can't get module.method");
       exit (1);
     }
   return node;
}

static PyMethodDef nodecreator[] = {
   {"number_node_c", creat_number_node, METH_VARARGS, NULL},
   {"operator_node_c", create_operator_node, METH_VARARGS, NULL},
   {"add_operand_c", add_operand_c, METH_VARARGS, NULL},
   {NULL, NULL}
};

void
initnode_creator ()
{
   Py_InitModule ("node_creator", nodecreator);
}




More information about the Python-list mailing list