Registering C methods when writing a C extension type?

Tom Epperly tepperly at llnl.gov
Wed Jan 3 18:18:58 EST 2001


I planning to write a C extension type (not module) to provide a Python
binding for an object or interface defined in an IDL (interface definition
language) for high performance scientific computing. In the example from
Mark Lutz's Programming Python (the only documentation I've found that
directly addresses writing extension types), it does something like the
following to make the object methods visible (in myobject.c):

static struct PyMethodDef myobject_methods[] = {
  {"method_one", (PyCFunction)method_one, 1},
  {"method_two", (PyCFunction)method_two, 1},
  {"method_three", (PyCFunction)method_three, 1},
  {NULL, NULL}
};

static PyObject *
myobject_getattr(myobject *self, char *name) {
 /* other stuff deleted */
 return Py_FindMethod(object_methods, self, name);
}

/* tp_getattr points to myobject_getattr */

I am wondering what the relative merits/penalties of doing something like
the following in the constructor for the hypothetical myobject instead of
the above. I would like feedback about issues of style (am I violating the
designers intent or using functions intended for internal use only),
forward/backward portability, and efficiency.

static myobject *
new_myobject()
{
  myobject *self;
  const int len = sizeof(object_methods)/sizeof(PyMethodDef);
  int i;
  self = PyObject_NEW(myobject, &myobjecttype);
  if (self == NULL) return NULL;
  for(i = 0 ; i < len ; i++){
    PyObject *func = PyCFunction_New(object_methods + i, self);
    if (func != NULL) {
      PyObject_SetAttrString(self, object_methods[i].ml_name, func);
      Py_DECREF(func); /* remove extra reference */
    }
  }
  return self;
}

In this case, the getattr method does not call Py_FindMethod.

Tom

--
------------------------------------------------------------------------
Tom Epperly
Center for Applied Scientific Computing   Phone: 925-424-3159
Lawrence Livermore National Laboratory      Fax: 925-424-2477
L-661, P.O. Box 808, Livermore, CA 94551  Email: tepperly at llnl.gov
------------------------------------------------------------------------





More information about the Python-list mailing list