Commit 8c8f4681 authored by Jack Jansen's avatar Jack Jansen

Patches by Jens B. Jorgensen with small mods by me:

- Converted the templates to use ANSI C prototypes (finally!)
- Use re in stead of deprecated regex
parent d06d120b
...@@ -4,9 +4,7 @@ static char $abbrev$_$method$__doc__[] = ...@@ -4,9 +4,7 @@ static char $abbrev$_$method$__doc__[] =
; ;
static PyObject * static PyObject *
$abbrev$_$method$(self, args) $abbrev$_$method$(PyObject *self /* Not used */, PyObject *args)
PyObject *self; /* Not used */
PyObject *args;
{ {
if (!PyArg_ParseTuple(args, "")) if (!PyArg_ParseTuple(args, ""))
......
...@@ -4,9 +4,7 @@ static char $abbrev$_$method$__doc__[] = ...@@ -4,9 +4,7 @@ static char $abbrev$_$method$__doc__[] =
; ;
static PyObject * static PyObject *
$abbrev$_$method$(self, args) $abbrev$_$method$($abbrev$object *self, PyObject *args)
$abbrev$object *self;
PyObject *args;
{ {
if (!PyArg_ParseTuple(args, "")) if (!PyArg_ParseTuple(args, ""))
return NULL; return NULL;
......
...@@ -12,9 +12,7 @@ static struct memberlist $abbrev$_memberlist[] = { ...@@ -12,9 +12,7 @@ static struct memberlist $abbrev$_memberlist[] = {
}; };
static PyObject * static PyObject *
$abbrev$_getattr(self, name) $abbrev$_getattr($abbrev$object *self, char *name)
$abbrev$object *self;
char *name;
{ {
PyObject *rv; PyObject *rv;
...@@ -28,10 +26,7 @@ $abbrev$_getattr(self, name) ...@@ -28,10 +26,7 @@ $abbrev$_getattr(self, name)
static int static int
$abbrev$_setattr(self, name, v) $abbrev$_setattr($abbrev$object *self, char *name, PyObject *v)
$abbrev$object *self;
char *name;
PyObject *v;
{ {
/* XXXX Add your own setattr code here */ /* XXXX Add your own setattr code here */
if ( v == NULL ) { if ( v == NULL ) {
......
...@@ -2,24 +2,19 @@ ...@@ -2,24 +2,19 @@
/* Code to access $name$ objects as mappings */ /* Code to access $name$ objects as mappings */
static int static int
$abbrev$_length(self) $abbrev$_length($abbrev$object *self)
$abbrev$object *self;
{ {
/* XXXX Return the size of the mapping */ /* XXXX Return the size of the mapping */
} }
static PyObject * static PyObject *
$abbrev$_subscript(self, key) $abbrev$_subscript($abbrev$object *self, PyObject *key)
$abbrev$object *self;
PyObject *key;
{ {
/* XXXX Return the item of self indexed by key */ /* XXXX Return the item of self indexed by key */
} }
static int static int
$abbrev$_ass_sub(self, v, w) $abbrev$_ass_sub($abbrev$object *self, PyObject *v, PyObject *w)
$abbrev$object *self;
PyObject *v, *w;
{ {
/* XXXX Put w in self under key v */ /* XXXX Put w in self under key v */
return 0; return 0;
......
...@@ -2,177 +2,140 @@ ...@@ -2,177 +2,140 @@
/* Code to access $name$ objects as numbers */ /* Code to access $name$ objects as numbers */
static PyObject * static PyObject *
$abbrev$_add(v, w) $abbrev$_add($abbrev$object *v, $abbrev$object *w)
$abbrev$object *v;
$abbrev$object *w;
{ {
/* XXXX Add them */ /* XXXX Add them */
} }
static PyObject * static PyObject *
$abbrev$_sub(v, w) $abbrev$_sub($abbrev$object *v, $abbrev$object *w)
$abbrev$object *v;
$abbrev$object *w;
{ {
/* XXXX Subtract them */ /* XXXX Subtract them */
} }
static PyObject * static PyObject *
$abbrev$_mul(v, w) $abbrev$_mul($abbrev$object *v, $abbrev$object *w)
$abbrev$object *v;
$abbrev$object *w;
{ {
/* XXXX Multiply them */ /* XXXX Multiply them */
} }
static PyObject * static PyObject *
$abbrev$_div(x, y) $abbrev$_div($abbrev$object *x, $abbrev$object *y)
$abbrev$object *x;
$abbrev$object *y;
{ {
/* XXXX Divide them */ /* XXXX Divide them */
} }
static PyObject * static PyObject *
$abbrev$_mod(x, y) $abbrev$_mod($abbrev$object *x, $abbrev$object *y)
$abbrev$object *x;
$abbrev$object *y;
{ {
/* XXXX Modulo them */ /* XXXX Modulo them */
} }
static PyObject * static PyObject *
$abbrev$_divmod(x, y) $abbrev$_divmod($abbrev$object *x, $abbrev$object *y)
$abbrev$object *x;
$abbrev$object *y;
{ {
/* XXXX Return 2-tuple with div and mod */ /* XXXX Return 2-tuple with div and mod */
} }
static PyObject * static PyObject *
$abbrev$_pow(v, w, z) $abbrev$_pow($abbrev$object *v, $abbrev$object *w, $abbrev$object *z)
$abbrev$object *v;
$abbrev$object *w;
$abbrev$object *z;
{ {
/* XXXX */ /* XXXX */
} }
static PyObject * static PyObject *
$abbrev$_neg(v) $abbrev$_neg($abbrev$object *v)
$abbrev$object *v;
{ {
/* XXXX */ /* XXXX */
} }
static PyObject * static PyObject *
$abbrev$_pos(v) $abbrev$_pos($abbrev$object *v)
$abbrev$object *v;
{ {
/* XXXX */ /* XXXX */
} }
static PyObject * static PyObject *
$abbrev$_abs(v) $abbrev$_abs($abbrev$object *v)
$abbrev$object *v;
{ {
/* XXXX */ /* XXXX */
} }
static int static int
$abbrev$_nonzero(v) $abbrev$_nonzero($abbrev$object *v)
$abbrev$object *v;
{ {
/* XXXX Return 1 if non-zero */ /* XXXX Return 1 if non-zero */
} }
static PyObject * static PyObject *
$abbrev$_invert(v) $abbrev$_invert($abbrev$object *v)
$abbrev$object *v;
{ {
/* XXXX */ /* XXXX */
} }
static PyObject * static PyObject *
$abbrev$_lshift(v, w) $abbrev$_lshift($abbrev$object *v, $abbrev$object *w)
$abbrev$object *v;
$abbrev$object *w;
{ {
/* XXXX */ /* XXXX */
} }
static PyObject * static PyObject *
$abbrev$_rshift(v, w) $abbrev$_rshift($abbrev$object *v, $abbrev$object *w)
$abbrev$object *v;
$abbrev$object *w;
{ {
/* XXXX */ /* XXXX */
} }
static PyObject * static PyObject *
$abbrev$_and(v, w) $abbrev$_and($abbrev$object *v, $abbrev$object *w)
$abbrev$object *v;
$abbrev$object *w;
{ {
/* XXXX */ /* XXXX */
} }
static PyObject * static PyObject *
$abbrev$_xor(v, w) $abbrev$_xor($abbrev$object *v, $abbrev$object *w)
$abbrev$object *v;
$abbrev$object *w;
{ {
/* XXXX */ /* XXXX */
} }
static PyObject * static PyObject *
$abbrev$_or(v, w) $abbrev$_or($abbrev$object *v, $abbrev$object *w)
$abbrev$object *v;
$abbrev$object *w;
{ {
/* XXXX */ /* XXXX */
} }
static int static int
$abbrev$_coerce(pv, pw) $abbrev$_coerce(PyObject **pv, PyObject **pw)
PyObject **pv;
PyObject **pw;
{ {
/* XXXX I haven't a clue... */ /* XXXX I haven't a clue... */
return 1; return 1;
} }
static PyObject * static PyObject *
$abbrev$_int(v) $abbrev$_int($abbrev$object *v)
$abbrev$object *v;
{ {
/* XXXX */ /* XXXX */
} }
static PyObject * static PyObject *
$abbrev$_long(v) $abbrev$_long($abbrev$object *v)
$abbrev$object *v;
{ {
/* XXXX */ /* XXXX */
} }
static PyObject * static PyObject *
$abbrev$_float(v) $abbrev$_float($abbrev$object *v)
$abbrev$object *v;
{ {
/* XXXX */ /* XXXX */
} }
static PyObject * static PyObject *
$abbrev$_oct(v) $abbrev$_oct($abbrev$object *v)
$abbrev$object *v;
{ {
/* XXXX Return object as octal stringobject */ /* XXXX Return object as octal stringobject */
} }
static PyObject * static PyObject *
$abbrev$_hex(v) $abbrev$_hex($abbrev$object *v)
$abbrev$object *v;
{ {
/* XXXX Return object as hex stringobject */ /* XXXX Return object as hex stringobject */
} }
......
...@@ -2,59 +2,44 @@ ...@@ -2,59 +2,44 @@
/* Code to handle accessing $name$ objects as sequence objects */ /* Code to handle accessing $name$ objects as sequence objects */
static int static int
$abbrev$_length(self) $abbrev$_length($abbrev$object *self)
$abbrev$object *self;
{ {
/* XXXX Return the size of the object */ /* XXXX Return the size of the object */
} }
static PyObject * static PyObject *
$abbrev$_concat(self, bb) $abbrev$_concat($abbrev$object *self, PyObject *bb)
$abbrev$object *self;
PyObject *bb;
{ {
/* XXXX Return the concatenation of self and bb */ /* XXXX Return the concatenation of self and bb */
} }
static PyObject * static PyObject *
$abbrev$_repeat(self, n) $abbrev$_repeat($abbrev$object *self, int n)
$abbrev$object *self;
int n;
{ {
/* XXXX Return a new object that is n times self */ /* XXXX Return a new object that is n times self */
} }
static PyObject * static PyObject *
$abbrev$_item(self, i) $abbrev$_item($abbrev$object *self, int i)
$abbrev$object *self;
int i;
{ {
/* XXXX Return the i-th object of self */ /* XXXX Return the i-th object of self */
} }
static PyObject * static PyObject *
$abbrev$_slice(self, ilow, ihigh) $abbrev$_slice($abbrev$object *self, int ilow, int ihigh)
$abbrev$object *self;
int ilow, ihigh;
{ {
/* XXXX Return the ilow..ihigh slice of self in a new object */ /* XXXX Return the ilow..ihigh slice of self in a new object */
} }
static int static int
$abbrev$_ass_item(self, i, v) $abbrev$_ass_item($abbrev$object *self, int i, PyObject *v)
$abbrev$object *self;
int i;
PyObject *v;
{ {
/* XXXX Assign to the i-th element of self */ /* XXXX Assign to the i-th element of self */
return 0; return 0;
} }
static int static int
$abbrev$_ass_slice(self, ilow, ihigh, v) $abbrev$_ass_slice(PyListObject *self, int ilow, int ihigh, PyObject *v)
PyListObject *self;
int ilow, ihigh;
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 PyObject * static PyObject *
$abbrev$_call(self, args, kwargs) $abbrev$_call($abbrev$object *self, PyObject *args, PyObject *kwargs)
$abbrev$object *self;
PyObject *args;
PyObject *kwargs;
{ {
/* XXXX Return the result of calling self with argument args */ /* XXXX Return the result of calling self with argument args */
} }
......
static int static int
$abbrev$_compare(v, w) $abbrev$_compare($abbrev$object *v, $abbrev$object *w)
$abbrev$object *v, *w;
{ {
/* XXXX Compare objects and return -1, 0 or 1 */ /* XXXX Compare objects and return -1, 0 or 1 */
} }
static void static void
$abbrev$_dealloc(self) $abbrev$_dealloc($abbrev$object *self)
$abbrev$object *self;
{ {
/* XXXX Add your own cleanup code here */ /* XXXX Add your own cleanup code here */
PyMem_DEL(self); PyMem_DEL(self);
......
static PyObject * static PyObject *
$abbrev$_getattr(self, name) $abbrev$_getattr($abbrev$object *self, char *name)
$abbrev$object *self;
char *name;
{ {
/* XXXX Add your own getattr code here */ /* XXXX Add your own getattr code here */
return Py_FindMethod($abbrev$_methods, (PyObject *)self, name); return Py_FindMethod($abbrev$_methods, (PyObject *)self, name);
......
static long static long
$abbrev$_hash(self) $abbrev$_hash($abbrev$object *self)
$abbrev$object *self;
{ {
/* XXXX Return a hash of self (or -1) */ /* XXXX Return a hash of self (or -1) */
} }
static int static int
$abbrev$_print(self, fp, flags) $abbrev$_print($abbrev$object *self, FILE *fp, int flags)
$abbrev$object *self;
FILE *fp;
int flags;
{ {
/* XXXX Add code here to print self to fp */ /* XXXX Add code here to print self to fp */
return 0; return 0;
......
static PyObject * static PyObject *
$abbrev$_repr(self) $abbrev$_repr($abbrev$object *self)
$abbrev$object *self;
{ {
PyObject *s; PyObject *s;
......
static int static int
$abbrev$_setattr(self, name, v) $abbrev$_setattr($abbrev$object *self, char *name, PyObject *v)
$abbrev$object *self;
char *name;
PyObject *v;
{ {
/* Set attribute 'name' to value 'v'. v==NULL means delete */ /* Set attribute 'name' to value 'v'. v==NULL means delete */
......
static PyObject * static PyObject *
$abbrev$_str(self) $abbrev$_str($abbrev$object *self)
$abbrev$object *self;
{ {
PyObject *s; PyObject *s;
......
...@@ -30,8 +30,8 @@ import string ...@@ -30,8 +30,8 @@ import string
oops = 'oops' oops = 'oops'
IDENTSTARTCHARS = string.ascii_letters + '_' IDENTSTARTCHARS = string.letters + '_'
IDENTCHARS = string.ascii_letters + string.digits + '_' IDENTCHARS = string.letters + string.digits + '_'
# Check that string is a legal C identifier # Check that string is a legal C identifier
def checkid(str): def checkid(str):
......
...@@ -2,37 +2,33 @@ ...@@ -2,37 +2,33 @@
# Variable substitution. Variables are $delimited$ # Variable substitution. Variables are $delimited$
# #
import string import string
import regex import re
import regsub
error = 'varsubst.error' error = 'varsubst.error'
class Varsubst: class Varsubst:
def __init__(self, dict): def __init__(self, dict):
self.dict = dict self.dict = dict
self.prog = regex.compile('\$[a-zA-Z0-9_]*\$') self.prog = re.compile('\$([a-zA-Z0-9_]*)\$')
self.do_useindent = 0 self.do_useindent = 0
def useindent(self, onoff): def useindent(self, onoff):
self.do_useindent = onoff self.do_useindent = onoff
def subst(self, str): def subst(self, s):
rv = '' rv = ''
while 1: while 1:
pos = self.prog.search(str) m = self.prog.search(s)
if pos < 0: if not m:
return rv + str return rv + s
if pos: rv = rv + s[:m.start()]
rv = rv + str[:pos] s = s[m.end():]
str = str[pos:] if m.end() - m.start() == 2:
len = self.prog.match(str)
if len == 2:
# Escaped dollar # Escaped dollar
rv = rv + '$' rv = rv + '$'
str = str[2:] s = s[2:]
continue continue
name = str[1:len-1] name = m.group(1)
str = str[len:]
if not self.dict.has_key(name): if not self.dict.has_key(name):
raise error, 'No such variable: '+name raise error, 'No such variable: '+name
value = self.dict[name] value = self.dict[name]
...@@ -44,7 +40,7 @@ class Varsubst: ...@@ -44,7 +40,7 @@ class Varsubst:
lastnl = string.rfind(old, '\n', 0) + 1 lastnl = string.rfind(old, '\n', 0) + 1
lastnl = len(old) - lastnl lastnl = len(old) - lastnl
sub = '\n' + (' '*lastnl) sub = '\n' + (' '*lastnl)
return regsub.gsub('\n', sub, value) return re.sub('\n', sub, value)
def _test(): def _test():
import sys import sys
......
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