Commit ebed7511 authored by Jack Jansen's avatar Jack Jansen

Templates converted to new naming conventions (thanks to Chak Tan)

parent 52e02998
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. 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 the forms specifying all the objects and methods, tell modulator
whether objects should also be accessible as sequences, etc and press whether objects should also be accessible as sequences, etc and press
'generate code'. It will write a complete skeleton module for you. 'generate code'. It will write a complete skeleton module for you.
......
#include "allobjects.h" #include "Python.h"
#include "modsupport.h" /* For getargs() etc. */ /* #include "modsupport.h" /* For getargs() etc. */
static object *ErrorObject; static PyObject *ErrorObject;
/* ----------------------------------------------------- */ /* ----------------------------------------------------- */
static object * static PyObject *
$abbrev$_$method$(self, args) $abbrev$_$method$(self, args)
object *self; /* Not used */ PyObject *self; /* Not used */
object *args; PyObject *args;
{ {
if (!newgetargs(args, "")) if (!PyArg_ParseTuple(args, ""))
return NULL; return NULL;
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
/* List of methods defined in the module */ /* List of methods defined in the module */
static struct methodlist $abbrev$_methods[] = { static struct PyMethodDef $abbrev$_methods[] = {
$methodlist$ $methodlist$
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
...@@ -12,19 +12,20 @@ static struct methodlist $abbrev$_methods[] = { ...@@ -12,19 +12,20 @@ static struct methodlist $abbrev$_methods[] = {
void void
init$name$() init$name$()
{ {
object *m, *d; PyObject *m, *d;
/* Create the module and add the functions */ /* 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 */ /* Add some symbolic constants to the module */
d = getmoduledict(m); d = PyModule_GetDict(m);
ErrorObject = newstringobject("$name$.error"); ErrorObject = PyString_FromString("$name$.error");
dictinsert(d, "error", ErrorObject); PyDict_SetItemString(d, "error", ErrorObject);
/* XXXX Add constants here */ /* XXXX Add constants here */
/* Check for errors */ /* Check for errors */
if (err_occurred()) if (PyErr_Occurred())
fatal("can't initialize module $name$"); Py_FatalError("can't initialize module $name$");
} }
/* Declarations for objects of type $name$ */ /* Declarations for objects of type $name$ */
typedef struct { typedef struct {
OB_HEAD PyObject_HEAD
/* XXXX Add your own stuff here */ /* XXXX Add your own stuff here */
} $abbrev$object; } $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$_$method$(self, args)
$abbrev$object *self; $abbrev$object *self;
object *args; PyObject *args;
{ {
if (!newgetargs(args, "")) if (!PyArg_ParseTuple(args, ""))
return NULL; return NULL;
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
static struct methodlist $abbrev$_methods[] = { static struct PyMethodDef $abbrev$_methods[] = {
$methodlist$ $methodlist$
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
/* ---------- */ /* ---------- */
...@@ -4,9 +4,10 @@ new$abbrev$object() ...@@ -4,9 +4,10 @@ new$abbrev$object()
{ {
$abbrev$object *self; $abbrev$object *self;
self = NEWOBJ($abbrev$object, &$Abbrev$type); self = PyObject_NEW($abbrev$object, &$Abbrev$type);
if (self == NULL) if (self == NULL)
return NULL; return NULL;
/* XXXX Add your own initializers here */ /* XXXX Add your own initializers here */
return self; return self;
} }
/* Code to access structure members by accessing attributes */ /* Code to access structure members by accessing attributes */
#include "structmember.h" #include "structmember.h"
...@@ -6,22 +7,23 @@ ...@@ -6,22 +7,23 @@
static struct memberlist $abbrev$_memberlist[] = { static struct memberlist $abbrev$_memberlist[] = {
/* XXXX Add lines like { "foo", T_INT, OFF(foo), RO } */ /* XXXX Add lines like { "foo", T_INT, OFF(foo), RO } */
{NULL} /* Sentinel */ {NULL} /* Sentinel */
}; };
static object * static PyObject *
$abbrev$_getattr(self, name) $abbrev$_getattr(self, name)
$abbrev$object *self; $abbrev$object *self;
char *name; char *name;
{ {
object *rv; PyObject *rv;
/* XXXX Add your own getattr code here */ /* 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) if (rv)
return rv; return rv;
err_clear(); PyErr_Clear();
return findmethod($abbrev$_methods, (object *)self, name); return Py_FindMethod($abbrev$_methods, (PyObject *)self, name);
} }
...@@ -29,13 +31,12 @@ static int ...@@ -29,13 +31,12 @@ static int
$abbrev$_setattr(self, name, v) $abbrev$_setattr(self, name, v)
$abbrev$object *self; $abbrev$object *self;
char *name; char *name;
object *v; PyObject *v;
{ {
/* XXXX Add your own setattr code here */ /* XXXX Add your own setattr code here */
if ( v == NULL ) { if ( v == NULL ) {
err_setstr(AttributeError, "Cannot delete attribute"); PyErr_SetString(PyExc_AttributeError, "Cannot delete attribute");
return -1; 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 = { static PyTypeObject $Abbrev$type = {
OB_HEAD_INIT(&Typetype) PyObject_HEAD_INIT(&PyType_Type)
0, /*ob_size*/ 0, /*ob_size*/
"$name$", /*tp_name*/ "$name$", /*tp_name*/
sizeof($abbrev$object), /*tp_basicsize*/ sizeof($abbrev$object), /*tp_basicsize*/
...@@ -20,3 +20,4 @@ static typeobject $Abbrev$type = { ...@@ -20,3 +20,4 @@ static typeobject $Abbrev$type = {
/* End of code for $name$ objects */ /* End of code for $name$ objects */
/* -------------------------------------------------------- */ /* -------------------------------------------------------- */
/* Code to access $name$ objects as mappings */ /* Code to access $name$ objects as mappings */
static int static int
...@@ -7,10 +8,10 @@ $abbrev$_length(self) ...@@ -7,10 +8,10 @@ $abbrev$_length(self)
/* XXXX Return the size of the mapping */ /* XXXX Return the size of the mapping */
} }
static object * static PyObject *
$abbrev$_subscript(self, key) $abbrev$_subscript(self, key)
$abbrev$object *self; $abbrev$object *self;
object *key; PyObject *key;
{ {
/* XXXX Return the item of self indexed by key */ /* XXXX Return the item of self indexed by key */
} }
...@@ -18,13 +19,13 @@ $abbrev$_subscript(self, key) ...@@ -18,13 +19,13 @@ $abbrev$_subscript(self, key)
static int static int
$abbrev$_ass_sub(self, v, w) $abbrev$_ass_sub(self, v, w)
$abbrev$object *self; $abbrev$object *self;
object *v, *w; PyObject *v, *w;
{ {
/* XXXX Put w in self under key v */ /* XXXX Put w in self under key v */
return 0; return 0;
} }
static mapping_methods $abbrev$_as_mapping = { static PyMappingMethods $abbrev$_as_mapping = {
(inquiry)$abbrev$_length, /*mp_length*/ (inquiry)$abbrev$_length, /*mp_length*/
(binaryfunc)$abbrev$_subscript, /*mp_subscript*/ (binaryfunc)$abbrev$_subscript, /*mp_subscript*/
(objobjargproc)$abbrev$_ass_sub, /*mp_ass_subscript*/ (objobjargproc)$abbrev$_ass_sub, /*mp_ass_subscript*/
......
/* Code to access $name$ objects as numbers */ /* Code to access $name$ objects as numbers */
static object * static PyObject *
$abbrev$_add(v, w) $abbrev$_add(v, w)
$abbrev$object *v; $abbrev$object *v;
$abbrev$object *w; $abbrev$object *w;
{ {
/* XXXX Add them */ /* XXXX Add them */
err_setstr(SystemError, "not implemented");
return NULL;
} }
static object * static PyObject *
$abbrev$_sub(v, w) $abbrev$_sub(v, w)
$abbrev$object *v; $abbrev$object *v;
$abbrev$object *w; $abbrev$object *w;
{ {
/* XXXX Subtract them */ /* XXXX Subtract them */
err_setstr(SystemError, "not implemented");
return NULL;
} }
static object * static PyObject *
$abbrev$_mul(v, w) $abbrev$_mul(v, w)
$abbrev$object *v; $abbrev$object *v;
$abbrev$object *w; $abbrev$object *w;
{ {
/* XXXX Multiply them */ /* XXXX Multiply them */
err_setstr(SystemError, "not implemented");
return NULL;
} }
static object * static PyObject *
$abbrev$_div(x, y) $abbrev$_div(x, y)
$abbrev$object *x; $abbrev$object *x;
$abbrev$object *y; $abbrev$object *y;
{ {
/* XXXX Divide them */ /* XXXX Divide them */
err_setstr(SystemError, "not implemented");
return NULL;
} }
static object * static PyObject *
$abbrev$_mod(x, y) $abbrev$_mod(x, y)
$abbrev$object *x; $abbrev$object *x;
$abbrev$object *y; $abbrev$object *y;
{ {
/* XXXX Modulo them */ /* XXXX Modulo them */
err_setstr(SystemError, "not implemented");
return NULL;
} }
static object * static PyObject *
$abbrev$_divmod(x, y) $abbrev$_divmod(x, y)
$abbrev$object *x; $abbrev$object *x;
$abbrev$object *y; $abbrev$object *y;
{ {
/* XXXX Return 2-tuple with div and mod */ /* 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$_pow(v, w, z)
$abbrev$object *v; $abbrev$object *v;
$abbrev$object *w; $abbrev$object *w;
$abbrev$object *z; $abbrev$object *z;
{ {
/* XXXX */ /* XXXX */
err_setstr(SystemError, "not implemented");
return NULL;
} }
static object * static PyObject *
$abbrev$_neg(v) $abbrev$_neg(v)
$abbrev$object *v; $abbrev$object *v;
{ {
/* XXXX */ /* XXXX */
err_setstr(SystemError, "not implemented");
return NULL;
} }
static object * static PyObject *
$abbrev$_pos(v) $abbrev$_pos(v)
$abbrev$object *v; $abbrev$object *v;
{ {
/* XXXX */ /* XXXX */
err_setstr(SystemError, "not implemented");
return NULL;
} }
static object * static PyObject *
$abbrev$_abs(v) $abbrev$_abs(v)
$abbrev$object *v; $abbrev$object *v;
{ {
/* XXXX */ /* XXXX */
err_setstr(SystemError, "not implemented");
return NULL;
} }
static int static int
...@@ -103,124 +84,100 @@ $abbrev$_nonzero(v) ...@@ -103,124 +84,100 @@ $abbrev$_nonzero(v)
$abbrev$object *v; $abbrev$object *v;
{ {
/* XXXX Return 1 if non-zero */ /* XXXX Return 1 if non-zero */
err_setstr(SystemError, "not implemented");
return -1;
} }
static object * static PyObject *
$abbrev$_invert(v) $abbrev$_invert(v)
$abbrev$object *v; $abbrev$object *v;
{ {
/* XXXX */ /* XXXX */
err_setstr(SystemError, "not implemented");
return NULL;
} }
static object * static PyObject *
$abbrev$_lshift(v, w) $abbrev$_lshift(v, w)
$abbrev$object *v; $abbrev$object *v;
$abbrev$object *w; $abbrev$object *w;
{ {
/* XXXX */ /* XXXX */
err_setstr(SystemError, "not implemented");
return NULL;
} }
static object * static PyObject *
$abbrev$_rshift(v, w) $abbrev$_rshift(v, w)
$abbrev$object *v; $abbrev$object *v;
$abbrev$object *w; $abbrev$object *w;
{ {
/* XXXX */ /* XXXX */
err_setstr(SystemError, "not implemented");
return NULL;
} }
static object * static PyObject *
$abbrev$_and(v, w) $abbrev$_and(v, w)
$abbrev$object *v; $abbrev$object *v;
$abbrev$object *w; $abbrev$object *w;
{ {
/* XXXX */ /* XXXX */
err_setstr(SystemError, "not implemented");
return NULL;
} }
static object * static PyObject *
$abbrev$_xor(v, w) $abbrev$_xor(v, w)
$abbrev$object *v; $abbrev$object *v;
$abbrev$object *w; $abbrev$object *w;
{ {
/* XXXX */ /* XXXX */
err_setstr(SystemError, "not implemented");
return NULL;
} }
static object * static PyObject *
$abbrev$_or(v, w) $abbrev$_or(v, w)
$abbrev$object *v; $abbrev$object *v;
$abbrev$object *w; $abbrev$object *w;
{ {
/* XXXX */ /* XXXX */
err_setstr(SystemError, "not implemented");
return NULL;
} }
static int static int
$abbrev$_coerce(pv, pw) $abbrev$_coerce(pv, pw)
object **pv; PyObject **pv;
object **pw; PyObject **pw;
{ {
/* XXXX I haven't a clue... */ /* XXXX I haven't a clue... */
return 1; return 1;
} }
static object * static PyObject *
$abbrev$_int(v) $abbrev$_int(v)
$abbrev$object *v; $abbrev$object *v;
{ {
/* XXXX */ /* XXXX */
err_setstr(SystemError, "not implemented");
return NULL;
} }
static object * static PyObject *
$abbrev$_long(v) $abbrev$_long(v)
$abbrev$object *v; $abbrev$object *v;
{ {
/* XXXX */ /* XXXX */
err_setstr(SystemError, "not implemented");
return NULL;
} }
static object * static PyObject *
$abbrev$_float(v) $abbrev$_float(v)
$abbrev$object *v; $abbrev$object *v;
{ {
/* XXXX */ /* XXXX */
err_setstr(SystemError, "not implemented");
return NULL;
} }
static object * static PyObject *
$abbrev$_oct(v) $abbrev$_oct(v)
$abbrev$object *v; $abbrev$object *v;
{ {
/* XXXX Return object as octal stringobject */ /* XXXX Return object as octal stringobject */
err_setstr(SystemError, "not implemented");
return NULL;
} }
static object * static PyObject *
$abbrev$_hex(v) $abbrev$_hex(v)
$abbrev$object *v; $abbrev$object *v;
{ {
/* XXXX Return object as hex stringobject */ /* 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$_add, /*nb_add*/
(binaryfunc)$abbrev$_sub, /*nb_subtract*/ (binaryfunc)$abbrev$_sub, /*nb_subtract*/
(binaryfunc)$abbrev$_mul, /*nb_multiply*/ (binaryfunc)$abbrev$_mul, /*nb_multiply*/
......
...@@ -8,15 +8,15 @@ $abbrev$_length(self) ...@@ -8,15 +8,15 @@ $abbrev$_length(self)
/* XXXX Return the size of the object */ /* XXXX Return the size of the object */
} }
static object * static PyObject *
$abbrev$_concat(self, bb) $abbrev$_concat(self, bb)
$abbrev$object *self; $abbrev$object *self;
object *bb; PyObject *bb;
{ {
/* XXXX Return the concatenation of self and bb */ /* XXXX Return the concatenation of self and bb */
} }
static object * static PyObject *
$abbrev$_repeat(self, n) $abbrev$_repeat(self, n)
$abbrev$object *self; $abbrev$object *self;
int n; int n;
...@@ -24,7 +24,7 @@ $abbrev$_repeat(self, n) ...@@ -24,7 +24,7 @@ $abbrev$_repeat(self, n)
/* XXXX Return a new object that is n times self */ /* XXXX Return a new object that is n times self */
} }
static object * static PyObject *
$abbrev$_item(self, i) $abbrev$_item(self, i)
$abbrev$object *self; $abbrev$object *self;
int i; int i;
...@@ -32,7 +32,7 @@ $abbrev$_item(self, i) ...@@ -32,7 +32,7 @@ $abbrev$_item(self, i)
/* XXXX Return the i-th object of self */ /* XXXX Return the i-th object of self */
} }
static object * static PyObject *
$abbrev$_slice(self, ilow, ihigh) $abbrev$_slice(self, ilow, ihigh)
$abbrev$object *self; $abbrev$object *self;
int ilow, ihigh; int ilow, ihigh;
...@@ -44,7 +44,7 @@ static int ...@@ -44,7 +44,7 @@ static int
$abbrev$_ass_item(self, i, v) $abbrev$_ass_item(self, i, v)
$abbrev$object *self; $abbrev$object *self;
int i; int i;
object *v; PyObject *v;
{ {
/* XXXX Assign to the i-th element of self */ /* XXXX Assign to the i-th element of self */
return 0; return 0;
...@@ -52,15 +52,15 @@ $abbrev$_ass_item(self, i, v) ...@@ -52,15 +52,15 @@ $abbrev$_ass_item(self, i, v)
static int static int
$abbrev$_ass_slice(self, ilow, ihigh, v) $abbrev$_ass_slice(self, ilow, ihigh, v)
listobject *self; PyListObject *self;
int ilow, ihigh; int ilow, ihigh;
object *v; PyObject *v;
{ {
/* XXXX Replace ilow..ihigh slice of self with v */ /* XXXX Replace ilow..ihigh slice of self with v */
return 0; return 0;
} }
static sequence_methods $abbrev$_as_sequence = { static PySequenceMethods $abbrev$_as_sequence = {
(inquiry)$abbrev$_length, /*sq_length*/ (inquiry)$abbrev$_length, /*sq_length*/
(binaryfunc)$abbrev$_concat, /*sq_concat*/ (binaryfunc)$abbrev$_concat, /*sq_concat*/
(intargfunc)$abbrev$_repeat, /*sq_repeat*/ (intargfunc)$abbrev$_repeat, /*sq_repeat*/
......
...@@ -4,5 +4,5 @@ $abbrev$_dealloc(self) ...@@ -4,5 +4,5 @@ $abbrev$_dealloc(self)
$abbrev$object *self; $abbrev$object *self;
{ {
/* XXXX Add your own cleanup code here */ /* XXXX Add your own cleanup code here */
DEL(self); PyMem_DEL(self);
} }
static object * static PyObject *
$abbrev$_getattr(self, name) $abbrev$_getattr(self, name)
$abbrev$object *self; $abbrev$object *self;
char *name; char *name;
{ {
/* XXXX Add your own getattr code here */ /* 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$_repr(self)
$abbrev$object *self; $abbrev$object *self;
{ {
object *s; PyObject *s;
/* XXXX Add code here to put self into s */ /* XXXX Add code here to put self into s */
return s; return s;
......
...@@ -3,7 +3,7 @@ static int ...@@ -3,7 +3,7 @@ static int
$abbrev$_setattr(self, name, v) $abbrev$_setattr(self, name, v)
$abbrev$object *self; $abbrev$object *self;
char *name; char *name;
object *v; PyObject *v;
{ {
/* XXXX Add your own setattr code here */ /* XXXX Add your own setattr code here */
return -1; return -1;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment