-This is release 1.0 of modulator, a generator of boilerplate code for
+This is release 1.1 of modulator, a generator of boilerplate code for
modules to be written in C.
-Usage when you have tk is *reall* simple: start modulator, fill out
+There is only one difference with release 1.0, really: the templates
+now use "new-style" naming conventions. Many thanks to Chak Tan
+<tan@ee.rochester.edu> for supplying them.
+
+Usage when you have tk is *really* simple: start modulator, fill out
the forms specifying all the objects and methods, tell modulator
whether objects should also be accessible as sequences, etc and press
'generate code'. It will write a complete skeleton module for you.
-#include "allobjects.h"
-#include "modsupport.h" /* For getargs() etc. */
+#include "Python.h"
+/* #include "modsupport.h" /* For getargs() etc. */
-static object *ErrorObject;
+static PyObject *ErrorObject;
/* ----------------------------------------------------- */
-static object *
+static PyObject *
$abbrev$_$method$(self, args)
- object *self; /* Not used */
- object *args;
+ PyObject *self; /* Not used */
+ PyObject *args;
{
- if (!newgetargs(args, ""))
+ if (!PyArg_ParseTuple(args, ""))
return NULL;
- INCREF(None);
- return None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
/* List of methods defined in the module */
-static struct methodlist $abbrev$_methods[] = {
- $methodlist$
- {NULL, NULL} /* sentinel */
+static struct PyMethodDef $abbrev$_methods[] = {
+ $methodlist$
+ {NULL, NULL} /* sentinel */
};
void
init$name$()
{
- object *m, *d;
+ PyObject *m, *d;
/* Create the module and add the functions */
- m = initmodule("$name$", $abbrev$_methods);
+ m = Py_InitModule("$name$", $abbrev$_methods);
/* Add some symbolic constants to the module */
- d = getmoduledict(m);
- ErrorObject = newstringobject("$name$.error");
- dictinsert(d, "error", ErrorObject);
+ d = PyModule_GetDict(m);
+ ErrorObject = PyString_FromString("$name$.error");
+ PyDict_SetItemString(d, "error", ErrorObject);
/* XXXX Add constants here */
/* Check for errors */
- if (err_occurred())
- fatal("can't initialize module $name$");
+ if (PyErr_Occurred())
+ Py_FatalError("can't initialize module $name$");
}
+
+
/* Declarations for objects of type $name$ */
typedef struct {
- OB_HEAD
+ PyObject_HEAD
/* XXXX Add your own stuff here */
} $abbrev$object;
-staticforward typeobject $Abbrev$type;
+staticforward PyTypeObject $Abbrev$type;
+
-#define is_$abbrev$object(v) ((v)->ob_type == &$Abbrev$type)
/* ---------------------------------------------------------------- */
-static object *
+static PyObject *
$abbrev$_$method$(self, args)
$abbrev$object *self;
- object *args;
+ PyObject *args;
{
- if (!newgetargs(args, ""))
+ if (!PyArg_ParseTuple(args, ""))
return NULL;
- INCREF(None);
- return None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
+
-static struct methodlist $abbrev$_methods[] = {
- $methodlist$
- {NULL, NULL} /* sentinel */
+static struct PyMethodDef $abbrev$_methods[] = {
+ $methodlist$
+ {NULL, NULL} /* sentinel */
};
/* ---------- */
+
{
$abbrev$object *self;
- self = NEWOBJ($abbrev$object, &$Abbrev$type);
+ self = PyObject_NEW($abbrev$object, &$Abbrev$type);
if (self == NULL)
return NULL;
/* XXXX Add your own initializers here */
return self;
}
+
+
/* Code to access structure members by accessing attributes */
#include "structmember.h"
static struct memberlist $abbrev$_memberlist[] = {
/* XXXX Add lines like { "foo", T_INT, OFF(foo), RO } */
+
{NULL} /* Sentinel */
};
-static object *
+static PyObject *
$abbrev$_getattr(self, name)
$abbrev$object *self;
char *name;
{
- object *rv;
+ PyObject *rv;
/* XXXX Add your own getattr code here */
- rv = getmember((char *)/*XXXX*/0, $abbrev$_memberlist, name);
+ rv = PyMember_Get((char *)/*XXXX*/0, $abbrev$_memberlist, name);
if (rv)
return rv;
- err_clear();
- return findmethod($abbrev$_methods, (object *)self, name);
+ PyErr_Clear();
+ return Py_FindMethod($abbrev$_methods, (PyObject *)self, name);
}
$abbrev$_setattr(self, name, v)
$abbrev$object *self;
char *name;
- object *v;
+ PyObject *v;
{
/* XXXX Add your own setattr code here */
if ( v == NULL ) {
- err_setstr(AttributeError, "Cannot delete attribute");
+ PyErr_SetString(PyExc_AttributeError, "Cannot delete attribute");
return -1;
}
- return setmember((char *)/*XXXX*/0, $abbrev$_memberlist, name, v);
+ return PyMember_Set((char *)/*XXXX*/0, $abbrev$_memberlist, name, v);
}
-
-static typeobject $Abbrev$type = {
- OB_HEAD_INIT(&Typetype)
+static PyTypeObject $Abbrev$type = {
+ PyObject_HEAD_INIT(&PyType_Type)
0, /*ob_size*/
"$name$", /*tp_name*/
sizeof($abbrev$object), /*tp_basicsize*/
/* End of code for $name$ objects */
/* -------------------------------------------------------- */
+
+
/* Code to access $name$ objects as mappings */
static int
/* XXXX Return the size of the mapping */
}
-static object *
+static PyObject *
$abbrev$_subscript(self, key)
$abbrev$object *self;
- object *key;
+ PyObject *key;
{
/* XXXX Return the item of self indexed by key */
}
static int
$abbrev$_ass_sub(self, v, w)
$abbrev$object *self;
- object *v, *w;
+ PyObject *v, *w;
{
/* XXXX Put w in self under key v */
return 0;
}
-static mapping_methods $abbrev$_as_mapping = {
+static PyMappingMethods $abbrev$_as_mapping = {
(inquiry)$abbrev$_length, /*mp_length*/
(binaryfunc)$abbrev$_subscript, /*mp_subscript*/
(objobjargproc)$abbrev$_ass_sub, /*mp_ass_subscript*/
+
/* Code to access $name$ objects as numbers */
-static object *
+static PyObject *
$abbrev$_add(v, w)
$abbrev$object *v;
$abbrev$object *w;
{
/* XXXX Add them */
- err_setstr(SystemError, "not implemented");
- return NULL;
}
-static object *
+static PyObject *
$abbrev$_sub(v, w)
$abbrev$object *v;
$abbrev$object *w;
{
/* XXXX Subtract them */
- err_setstr(SystemError, "not implemented");
- return NULL;
}
-static object *
+static PyObject *
$abbrev$_mul(v, w)
$abbrev$object *v;
$abbrev$object *w;
{
/* XXXX Multiply them */
- err_setstr(SystemError, "not implemented");
- return NULL;
}
-static object *
+static PyObject *
$abbrev$_div(x, y)
$abbrev$object *x;
$abbrev$object *y;
{
/* XXXX Divide them */
- err_setstr(SystemError, "not implemented");
- return NULL;
}
-static object *
+static PyObject *
$abbrev$_mod(x, y)
$abbrev$object *x;
$abbrev$object *y;
{
/* XXXX Modulo them */
- err_setstr(SystemError, "not implemented");
- return NULL;
}
-static object *
+static PyObject *
$abbrev$_divmod(x, y)
$abbrev$object *x;
$abbrev$object *y;
{
/* XXXX Return 2-tuple with div and mod */
- err_setstr(SystemError, "not implemented");
- return NULL;
}
-static object *
+static PyObject *
$abbrev$_pow(v, w, z)
$abbrev$object *v;
$abbrev$object *w;
$abbrev$object *z;
{
/* XXXX */
- err_setstr(SystemError, "not implemented");
- return NULL;
}
-static object *
+static PyObject *
$abbrev$_neg(v)
$abbrev$object *v;
{
/* XXXX */
- err_setstr(SystemError, "not implemented");
- return NULL;
}
-static object *
+static PyObject *
$abbrev$_pos(v)
$abbrev$object *v;
{
/* XXXX */
- err_setstr(SystemError, "not implemented");
- return NULL;
}
-static object *
+static PyObject *
$abbrev$_abs(v)
$abbrev$object *v;
{
/* XXXX */
- err_setstr(SystemError, "not implemented");
- return NULL;
}
static int
$abbrev$object *v;
{
/* XXXX Return 1 if non-zero */
- err_setstr(SystemError, "not implemented");
- return -1;
}
-static object *
+static PyObject *
$abbrev$_invert(v)
$abbrev$object *v;
{
/* XXXX */
- err_setstr(SystemError, "not implemented");
- return NULL;
}
-static object *
+static PyObject *
$abbrev$_lshift(v, w)
$abbrev$object *v;
$abbrev$object *w;
{
/* XXXX */
- err_setstr(SystemError, "not implemented");
- return NULL;
}
-static object *
+static PyObject *
$abbrev$_rshift(v, w)
$abbrev$object *v;
$abbrev$object *w;
{
/* XXXX */
- err_setstr(SystemError, "not implemented");
- return NULL;
}
-static object *
+static PyObject *
$abbrev$_and(v, w)
$abbrev$object *v;
$abbrev$object *w;
{
/* XXXX */
- err_setstr(SystemError, "not implemented");
- return NULL;
}
-static object *
+static PyObject *
$abbrev$_xor(v, w)
$abbrev$object *v;
$abbrev$object *w;
{
/* XXXX */
- err_setstr(SystemError, "not implemented");
- return NULL;
}
-static object *
+static PyObject *
$abbrev$_or(v, w)
$abbrev$object *v;
$abbrev$object *w;
{
/* XXXX */
- err_setstr(SystemError, "not implemented");
- return NULL;
}
static int
$abbrev$_coerce(pv, pw)
- object **pv;
- object **pw;
+ PyObject **pv;
+ PyObject **pw;
{
/* XXXX I haven't a clue... */
return 1;
}
-static object *
+static PyObject *
$abbrev$_int(v)
$abbrev$object *v;
{
/* XXXX */
- err_setstr(SystemError, "not implemented");
- return NULL;
}
-static object *
+static PyObject *
$abbrev$_long(v)
$abbrev$object *v;
{
/* XXXX */
- err_setstr(SystemError, "not implemented");
- return NULL;
}
-static object *
+static PyObject *
$abbrev$_float(v)
$abbrev$object *v;
{
/* XXXX */
- err_setstr(SystemError, "not implemented");
- return NULL;
}
-static object *
+static PyObject *
$abbrev$_oct(v)
$abbrev$object *v;
{
/* XXXX Return object as octal stringobject */
- err_setstr(SystemError, "not implemented");
- return NULL;
}
-static object *
+static PyObject *
$abbrev$_hex(v)
$abbrev$object *v;
{
/* XXXX Return object as hex stringobject */
- err_setstr(SystemError, "not implemented");
- return NULL;
}
-static number_methods $abbrev$_as_number = {
+static PyNumberMethods $abbrev$_as_number = {
(binaryfunc)$abbrev$_add, /*nb_add*/
(binaryfunc)$abbrev$_sub, /*nb_subtract*/
(binaryfunc)$abbrev$_mul, /*nb_multiply*/
/* XXXX Return the size of the object */
}
-static object *
+static PyObject *
$abbrev$_concat(self, bb)
$abbrev$object *self;
- object *bb;
+ PyObject *bb;
{
/* XXXX Return the concatenation of self and bb */
}
-static object *
+static PyObject *
$abbrev$_repeat(self, n)
$abbrev$object *self;
int n;
/* XXXX Return a new object that is n times self */
}
-static object *
+static PyObject *
$abbrev$_item(self, i)
$abbrev$object *self;
int i;
/* XXXX Return the i-th object of self */
}
-static object *
+static PyObject *
$abbrev$_slice(self, ilow, ihigh)
$abbrev$object *self;
int ilow, ihigh;
$abbrev$_ass_item(self, i, v)
$abbrev$object *self;
int i;
- object *v;
+ PyObject *v;
{
/* XXXX Assign to the i-th element of self */
return 0;
static int
$abbrev$_ass_slice(self, ilow, ihigh, v)
- listobject *self;
+ PyListObject *self;
int ilow, ihigh;
- object *v;
+ PyObject *v;
{
/* XXXX Replace ilow..ihigh slice of self with v */
return 0;
}
-static sequence_methods $abbrev$_as_sequence = {
+static PySequenceMethods $abbrev$_as_sequence = {
(inquiry)$abbrev$_length, /*sq_length*/
(binaryfunc)$abbrev$_concat, /*sq_concat*/
(intargfunc)$abbrev$_repeat, /*sq_repeat*/
$abbrev$object *self;
{
/* XXXX Add your own cleanup code here */
- DEL(self);
+ PyMem_DEL(self);
}
-static object *
+static PyObject *
$abbrev$_getattr(self, name)
$abbrev$object *self;
char *name;
{
/* XXXX Add your own getattr code here */
- return findmethod($abbrev$_methods, (object *)self, name);
+ return Py_FindMethod($abbrev$_methods, (PyObject *)self, name);
}
-static object *
+static PyObject *
$abbrev$_repr(self)
$abbrev$object *self;
{
- object *s;
+ PyObject *s;
/* XXXX Add code here to put self into s */
return s;
$abbrev$_setattr(self, name, v)
$abbrev$object *self;
char *name;
- object *v;
+ PyObject *v;
{
/* XXXX Add your own setattr code here */
return -1;