From 9aaee933da8ce390c8c34fc227054b2651264368 Mon Sep 17 00:00:00 2001 From: Jack Jansen Date: Thu, 27 Dec 2001 23:35:43 +0000 Subject: 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 --- Tools/modulator/Templates/module_method | 4 +- Tools/modulator/Templates/object_method | 4 +- Tools/modulator/Templates/object_structure | 9 +-- Tools/modulator/Templates/object_tp_as_mapping | 11 +--- Tools/modulator/Templates/object_tp_as_number | 83 +++++++------------------ Tools/modulator/Templates/object_tp_as_sequence | 29 +++------ Tools/modulator/Templates/object_tp_call | 5 +- Tools/modulator/Templates/object_tp_compare | 3 +- Tools/modulator/Templates/object_tp_dealloc | 3 +- Tools/modulator/Templates/object_tp_getattr | 4 +- Tools/modulator/Templates/object_tp_hash | 3 +- Tools/modulator/Templates/object_tp_print | 5 +- Tools/modulator/Templates/object_tp_repr | 3 +- Tools/modulator/Templates/object_tp_setattr | 5 +- Tools/modulator/Templates/object_tp_str | 3 +- Tools/modulator/modulator.py | 4 +- Tools/modulator/varsubst.py | 28 ++++----- 17 files changed, 60 insertions(+), 146 deletions(-) diff --git a/Tools/modulator/Templates/module_method b/Tools/modulator/Templates/module_method index 9e6c0d2..3048b1f 100644 --- a/Tools/modulator/Templates/module_method +++ b/Tools/modulator/Templates/module_method @@ -4,9 +4,7 @@ static char $abbrev$_$method$__doc__[] = ; static PyObject * -$abbrev$_$method$(self, args) - PyObject *self; /* Not used */ - PyObject *args; +$abbrev$_$method$(PyObject *self /* Not used */, PyObject *args) { if (!PyArg_ParseTuple(args, "")) diff --git a/Tools/modulator/Templates/object_method b/Tools/modulator/Templates/object_method index 9541494..b15162c 100644 --- a/Tools/modulator/Templates/object_method +++ b/Tools/modulator/Templates/object_method @@ -4,9 +4,7 @@ static char $abbrev$_$method$__doc__[] = ; static PyObject * -$abbrev$_$method$(self, args) - $abbrev$object *self; - PyObject *args; +$abbrev$_$method$($abbrev$object *self, PyObject *args) { if (!PyArg_ParseTuple(args, "")) return NULL; diff --git a/Tools/modulator/Templates/object_structure b/Tools/modulator/Templates/object_structure index 4bb92ef..573ac8d 100644 --- a/Tools/modulator/Templates/object_structure +++ b/Tools/modulator/Templates/object_structure @@ -12,9 +12,7 @@ static struct memberlist $abbrev$_memberlist[] = { }; static PyObject * -$abbrev$_getattr(self, name) - $abbrev$object *self; - char *name; +$abbrev$_getattr($abbrev$object *self, char *name) { PyObject *rv; @@ -28,10 +26,7 @@ $abbrev$_getattr(self, name) static int -$abbrev$_setattr(self, name, v) - $abbrev$object *self; - char *name; - PyObject *v; +$abbrev$_setattr($abbrev$object *self, char *name, PyObject *v) { /* XXXX Add your own setattr code here */ if ( v == NULL ) { diff --git a/Tools/modulator/Templates/object_tp_as_mapping b/Tools/modulator/Templates/object_tp_as_mapping index 440904f..f9213b7 100644 --- a/Tools/modulator/Templates/object_tp_as_mapping +++ b/Tools/modulator/Templates/object_tp_as_mapping @@ -2,24 +2,19 @@ /* Code to access $name$ objects as mappings */ static int -$abbrev$_length(self) - $abbrev$object *self; +$abbrev$_length($abbrev$object *self) { /* XXXX Return the size of the mapping */ } static PyObject * -$abbrev$_subscript(self, key) - $abbrev$object *self; - PyObject *key; +$abbrev$_subscript($abbrev$object *self, PyObject *key) { /* XXXX Return the item of self indexed by key */ } static int -$abbrev$_ass_sub(self, v, w) - $abbrev$object *self; - PyObject *v, *w; +$abbrev$_ass_sub($abbrev$object *self, PyObject *v, PyObject *w) { /* XXXX Put w in self under key v */ return 0; diff --git a/Tools/modulator/Templates/object_tp_as_number b/Tools/modulator/Templates/object_tp_as_number index 2f90edc..e69aa9a 100644 --- a/Tools/modulator/Templates/object_tp_as_number +++ b/Tools/modulator/Templates/object_tp_as_number @@ -2,177 +2,140 @@ /* Code to access $name$ objects as numbers */ static PyObject * -$abbrev$_add(v, w) - $abbrev$object *v; - $abbrev$object *w; +$abbrev$_add($abbrev$object *v, $abbrev$object *w) { /* XXXX Add them */ } static PyObject * -$abbrev$_sub(v, w) - $abbrev$object *v; - $abbrev$object *w; +$abbrev$_sub($abbrev$object *v, $abbrev$object *w) { /* XXXX Subtract them */ } static PyObject * -$abbrev$_mul(v, w) - $abbrev$object *v; - $abbrev$object *w; +$abbrev$_mul($abbrev$object *v, $abbrev$object *w) { /* XXXX Multiply them */ } static PyObject * -$abbrev$_div(x, y) - $abbrev$object *x; - $abbrev$object *y; +$abbrev$_div($abbrev$object *x, $abbrev$object *y) { /* XXXX Divide them */ } static PyObject * -$abbrev$_mod(x, y) - $abbrev$object *x; - $abbrev$object *y; +$abbrev$_mod($abbrev$object *x, $abbrev$object *y) { /* XXXX Modulo them */ } static PyObject * -$abbrev$_divmod(x, y) - $abbrev$object *x; - $abbrev$object *y; +$abbrev$_divmod($abbrev$object *x, $abbrev$object *y) { /* XXXX Return 2-tuple with div and mod */ } static PyObject * -$abbrev$_pow(v, w, z) - $abbrev$object *v; - $abbrev$object *w; - $abbrev$object *z; +$abbrev$_pow($abbrev$object *v, $abbrev$object *w, $abbrev$object *z) { /* XXXX */ } static PyObject * -$abbrev$_neg(v) - $abbrev$object *v; +$abbrev$_neg($abbrev$object *v) { /* XXXX */ } static PyObject * -$abbrev$_pos(v) - $abbrev$object *v; +$abbrev$_pos($abbrev$object *v) { /* XXXX */ } static PyObject * -$abbrev$_abs(v) - $abbrev$object *v; +$abbrev$_abs($abbrev$object *v) { /* XXXX */ } static int -$abbrev$_nonzero(v) - $abbrev$object *v; +$abbrev$_nonzero($abbrev$object *v) { /* XXXX Return 1 if non-zero */ } static PyObject * -$abbrev$_invert(v) - $abbrev$object *v; +$abbrev$_invert($abbrev$object *v) { /* XXXX */ } static PyObject * -$abbrev$_lshift(v, w) - $abbrev$object *v; - $abbrev$object *w; +$abbrev$_lshift($abbrev$object *v, $abbrev$object *w) { /* XXXX */ } static PyObject * -$abbrev$_rshift(v, w) - $abbrev$object *v; - $abbrev$object *w; +$abbrev$_rshift($abbrev$object *v, $abbrev$object *w) { /* XXXX */ } static PyObject * -$abbrev$_and(v, w) - $abbrev$object *v; - $abbrev$object *w; +$abbrev$_and($abbrev$object *v, $abbrev$object *w) { /* XXXX */ } static PyObject * -$abbrev$_xor(v, w) - $abbrev$object *v; - $abbrev$object *w; +$abbrev$_xor($abbrev$object *v, $abbrev$object *w) { /* XXXX */ } static PyObject * -$abbrev$_or(v, w) - $abbrev$object *v; - $abbrev$object *w; +$abbrev$_or($abbrev$object *v, $abbrev$object *w) { /* XXXX */ } static int -$abbrev$_coerce(pv, pw) - PyObject **pv; - PyObject **pw; +$abbrev$_coerce(PyObject **pv, PyObject **pw) { /* XXXX I haven't a clue... */ return 1; } static PyObject * -$abbrev$_int(v) - $abbrev$object *v; +$abbrev$_int($abbrev$object *v) { /* XXXX */ } static PyObject * -$abbrev$_long(v) - $abbrev$object *v; +$abbrev$_long($abbrev$object *v) { /* XXXX */ } static PyObject * -$abbrev$_float(v) - $abbrev$object *v; +$abbrev$_float($abbrev$object *v) { /* XXXX */ } static PyObject * -$abbrev$_oct(v) - $abbrev$object *v; +$abbrev$_oct($abbrev$object *v) { /* XXXX Return object as octal stringobject */ } static PyObject * -$abbrev$_hex(v) - $abbrev$object *v; +$abbrev$_hex($abbrev$object *v) { /* XXXX Return object as hex stringobject */ } diff --git a/Tools/modulator/Templates/object_tp_as_sequence b/Tools/modulator/Templates/object_tp_as_sequence index bc0f470..54c0b92 100644 --- a/Tools/modulator/Templates/object_tp_as_sequence +++ b/Tools/modulator/Templates/object_tp_as_sequence @@ -2,59 +2,44 @@ /* Code to handle accessing $name$ objects as sequence objects */ static int -$abbrev$_length(self) - $abbrev$object *self; +$abbrev$_length($abbrev$object *self) { /* XXXX Return the size of the object */ } static PyObject * -$abbrev$_concat(self, bb) - $abbrev$object *self; - PyObject *bb; +$abbrev$_concat($abbrev$object *self, PyObject *bb) { /* XXXX Return the concatenation of self and bb */ } static PyObject * -$abbrev$_repeat(self, n) - $abbrev$object *self; - int n; +$abbrev$_repeat($abbrev$object *self, int n) { /* XXXX Return a new object that is n times self */ } static PyObject * -$abbrev$_item(self, i) - $abbrev$object *self; - int i; +$abbrev$_item($abbrev$object *self, int i) { /* XXXX Return the i-th object of self */ } static PyObject * -$abbrev$_slice(self, ilow, ihigh) - $abbrev$object *self; - int ilow, ihigh; +$abbrev$_slice($abbrev$object *self, int ilow, int ihigh) { /* XXXX Return the ilow..ihigh slice of self in a new object */ } static int -$abbrev$_ass_item(self, i, v) - $abbrev$object *self; - int i; - PyObject *v; +$abbrev$_ass_item($abbrev$object *self, int i, PyObject *v) { /* XXXX Assign to the i-th element of self */ return 0; } static int -$abbrev$_ass_slice(self, ilow, ihigh, v) - PyListObject *self; - int ilow, ihigh; - PyObject *v; +$abbrev$_ass_slice(PyListObject *self, int ilow, int ihigh, PyObject *v) { /* XXXX Replace ilow..ihigh slice of self with v */ return 0; diff --git a/Tools/modulator/Templates/object_tp_call b/Tools/modulator/Templates/object_tp_call index be4cc4b..a93f17f 100644 --- a/Tools/modulator/Templates/object_tp_call +++ b/Tools/modulator/Templates/object_tp_call @@ -1,9 +1,6 @@ static PyObject * -$abbrev$_call(self, args, kwargs) - $abbrev$object *self; - PyObject *args; - PyObject *kwargs; +$abbrev$_call($abbrev$object *self, PyObject *args, PyObject *kwargs) { /* XXXX Return the result of calling self with argument args */ } diff --git a/Tools/modulator/Templates/object_tp_compare b/Tools/modulator/Templates/object_tp_compare index a2e2e9d..153bae0 100644 --- a/Tools/modulator/Templates/object_tp_compare +++ b/Tools/modulator/Templates/object_tp_compare @@ -1,7 +1,6 @@ static int -$abbrev$_compare(v, w) - $abbrev$object *v, *w; +$abbrev$_compare($abbrev$object *v, $abbrev$object *w) { /* XXXX Compare objects and return -1, 0 or 1 */ } diff --git a/Tools/modulator/Templates/object_tp_dealloc b/Tools/modulator/Templates/object_tp_dealloc index ca15c03..440419a 100644 --- a/Tools/modulator/Templates/object_tp_dealloc +++ b/Tools/modulator/Templates/object_tp_dealloc @@ -1,7 +1,6 @@ static void -$abbrev$_dealloc(self) - $abbrev$object *self; +$abbrev$_dealloc($abbrev$object *self) { /* XXXX Add your own cleanup code here */ PyMem_DEL(self); diff --git a/Tools/modulator/Templates/object_tp_getattr b/Tools/modulator/Templates/object_tp_getattr index 8e42aea..6a1b2e8 100644 --- a/Tools/modulator/Templates/object_tp_getattr +++ b/Tools/modulator/Templates/object_tp_getattr @@ -1,8 +1,6 @@ static PyObject * -$abbrev$_getattr(self, name) - $abbrev$object *self; - char *name; +$abbrev$_getattr($abbrev$object *self, char *name) { /* XXXX Add your own getattr code here */ return Py_FindMethod($abbrev$_methods, (PyObject *)self, name); diff --git a/Tools/modulator/Templates/object_tp_hash b/Tools/modulator/Templates/object_tp_hash index 1681b4a..2d63f6a 100644 --- a/Tools/modulator/Templates/object_tp_hash +++ b/Tools/modulator/Templates/object_tp_hash @@ -1,7 +1,6 @@ static long -$abbrev$_hash(self) - $abbrev$object *self; +$abbrev$_hash($abbrev$object *self) { /* XXXX Return a hash of self (or -1) */ } diff --git a/Tools/modulator/Templates/object_tp_print b/Tools/modulator/Templates/object_tp_print index 017712e..76408d2 100644 --- a/Tools/modulator/Templates/object_tp_print +++ b/Tools/modulator/Templates/object_tp_print @@ -1,9 +1,6 @@ static int -$abbrev$_print(self, fp, flags) - $abbrev$object *self; - FILE *fp; - int flags; +$abbrev$_print($abbrev$object *self, FILE *fp, int flags) { /* XXXX Add code here to print self to fp */ return 0; diff --git a/Tools/modulator/Templates/object_tp_repr b/Tools/modulator/Templates/object_tp_repr index 16aebc7..f122225 100644 --- a/Tools/modulator/Templates/object_tp_repr +++ b/Tools/modulator/Templates/object_tp_repr @@ -1,7 +1,6 @@ static PyObject * -$abbrev$_repr(self) - $abbrev$object *self; +$abbrev$_repr($abbrev$object *self) { PyObject *s; diff --git a/Tools/modulator/Templates/object_tp_setattr b/Tools/modulator/Templates/object_tp_setattr index 15701b6..dfe4bc8 100644 --- a/Tools/modulator/Templates/object_tp_setattr +++ b/Tools/modulator/Templates/object_tp_setattr @@ -1,9 +1,6 @@ static int -$abbrev$_setattr(self, name, v) - $abbrev$object *self; - char *name; - PyObject *v; +$abbrev$_setattr($abbrev$object *self, char *name, PyObject *v) { /* Set attribute 'name' to value 'v'. v==NULL means delete */ diff --git a/Tools/modulator/Templates/object_tp_str b/Tools/modulator/Templates/object_tp_str index bed15df..2e3648e 100644 --- a/Tools/modulator/Templates/object_tp_str +++ b/Tools/modulator/Templates/object_tp_str @@ -1,7 +1,6 @@ static PyObject * -$abbrev$_str(self) - $abbrev$object *self; +$abbrev$_str($abbrev$object *self) { PyObject *s; diff --git a/Tools/modulator/modulator.py b/Tools/modulator/modulator.py index add8b6a..cdf6afe 100755 --- a/Tools/modulator/modulator.py +++ b/Tools/modulator/modulator.py @@ -30,8 +30,8 @@ import string oops = 'oops' -IDENTSTARTCHARS = string.ascii_letters + '_' -IDENTCHARS = string.ascii_letters + string.digits + '_' +IDENTSTARTCHARS = string.letters + '_' +IDENTCHARS = string.letters + string.digits + '_' # Check that string is a legal C identifier def checkid(str): diff --git a/Tools/modulator/varsubst.py b/Tools/modulator/varsubst.py index 06e9683..71a2b26 100644 --- a/Tools/modulator/varsubst.py +++ b/Tools/modulator/varsubst.py @@ -2,37 +2,33 @@ # Variable substitution. Variables are $delimited$ # import string -import regex -import regsub +import re error = 'varsubst.error' class Varsubst: def __init__(self, dict): self.dict = dict - self.prog = regex.compile('\$[a-zA-Z0-9_]*\$') + self.prog = re.compile('\$([a-zA-Z0-9_]*)\$') self.do_useindent = 0 def useindent(self, onoff): self.do_useindent = onoff - def subst(self, str): + def subst(self, s): rv = '' while 1: - pos = self.prog.search(str) - if pos < 0: - return rv + str - if pos: - rv = rv + str[:pos] - str = str[pos:] - len = self.prog.match(str) - if len == 2: + m = self.prog.search(s) + if not m: + return rv + s + rv = rv + s[:m.start()] + s = s[m.end():] + if m.end() - m.start() == 2: # Escaped dollar rv = rv + '$' - str = str[2:] + s = s[2:] continue - name = str[1:len-1] - str = str[len:] + name = m.group(1) if not self.dict.has_key(name): raise error, 'No such variable: '+name value = self.dict[name] @@ -44,7 +40,7 @@ class Varsubst: lastnl = string.rfind(old, '\n', 0) + 1 lastnl = len(old) - lastnl sub = '\n' + (' '*lastnl) - return regsub.gsub('\n', sub, value) + return re.sub('\n', sub, value) def _test(): import sys -- cgit v0.12