annotate Tools/modulator/genmodule.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents 9e113aa3758d
children 8a7a2f1f88f0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2466
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 #
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 # Genmodule - A python program to help you build (template) modules.
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 #
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 # Usage:
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 #
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 # o = genmodule.object()
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 # o.name = 'dwarve object'
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 # o.abbrev = 'dw'
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 # o.funclist = ['new', 'dealloc', 'getattr', 'setattr']
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 # o.methodlist = ['dig']
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 #
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 # m = genmodule.module()
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 # m.name = 'beings'
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 # m.abbrev = 'be'
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 # m.methodlist = ['newdwarve']
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 # m.objects = [o]
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 #
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 # genmodule.write(sys.stdout, m)
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 #
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 import sys
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 import os
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 import varsubst
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 import string
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 error = 'genmodule.error'
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 #
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # Names of functions in the object-description struct.
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 #
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 FUNCLIST = ['new', 'tp_dealloc', 'tp_print', 'tp_getattr', 'tp_setattr',
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
31 'tp_compare', 'tp_repr', 'tp_hash', 'tp_call', 'tp_str']
2466
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 TYPELIST = ['tp_as_number', 'tp_as_sequence', 'tp_as_mapping', 'structure']
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 #
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 # writer is a base class for the object and module classes
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 # it contains code common to both.
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 #
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 class writer:
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 def __init__(self):
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
40 self._subst = None
2466
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 def makesubst(self):
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
43 if not self._subst:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
44 if not self.__dict__.has_key('abbrev'):
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
45 self.abbrev = self.name
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
46 self.Abbrev = string.upper(self.abbrev[0])+self.abbrev[1:]
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
47 subst = varsubst.Varsubst(self.__dict__)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
48 subst.useindent(1)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
49 self._subst = subst.subst
2466
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 def addcode(self, name, fp):
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
52 ifp = self.opentemplate(name)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
53 self.makesubst()
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
54 d = ifp.read()
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
55 d = self._subst(d)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
56 fp.write(d)
2466
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 def opentemplate(self, name):
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
59 for p in sys.path:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
60 fn = os.path.join(p, name)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
61 if os.path.exists(fn):
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
62 return open(fn, 'r')
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
63 fn = os.path.join(p, 'Templates')
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
64 fn = os.path.join(fn, name)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
65 if os.path.exists(fn):
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
66 return open(fn, 'r')
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
67 raise error, 'Template '+name+' not found for '+self._type+' '+ \
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
68 self.name
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
69
2466
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 class module(writer):
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 _type = 'module'
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 def writecode(self, fp):
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
74 self.addcode('copyright', fp)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
75 self.addcode('module_head', fp)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
76 for o in self.objects:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
77 o.writehead(fp)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
78 for o in self.objects:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
79 o.writebody(fp)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
80 new_ml = ''
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
81 for fn in self.methodlist:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
82 self.method = fn
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
83 self.addcode('module_method', fp)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
84 new_ml = new_ml + (
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
85 '{"%s",\t(PyCFunction)%s_%s,\tMETH_VARARGS,\t%s_%s__doc__},\n'
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
86 %(fn, self.abbrev, fn, self.abbrev, fn))
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
87 self.methodlist = new_ml
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
88 self.addcode('module_tail', fp)
2466
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
89
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 class object(writer):
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 _type = 'object'
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 def __init__(self):
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
93 self.typelist = []
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
94 self.methodlist = []
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
95 self.funclist = ['new']
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
96 writer.__init__(self)
2466
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 def writecode(self, fp):
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
99 self.addcode('copyright', fp)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
100 self.writehead(fp)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
101 self.writebody(fp)
2466
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
102
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 def writehead(self, fp):
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
104 self.addcode('object_head', fp)
2466
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
105
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 def writebody(self, fp):
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
107 new_ml = ''
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
108 for fn in self.methodlist:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
109 self.method = fn
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
110 self.addcode('object_method', fp)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
111 new_ml = new_ml + (
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
112 '{"%s",\t(PyCFunction)%s_%s,\tMETH_VARARGS,\t%s_%s__doc__},\n'
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
113 %(fn, self.abbrev, fn, self.abbrev, fn))
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
114 self.methodlist = new_ml
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
115 self.addcode('object_mlist', fp)
2466
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
116
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
117 # Add getattr if we have methods
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
118 if self.methodlist and not 'tp_getattr' in self.funclist:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
119 self.funclist.insert(0, 'tp_getattr')
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
120
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
121 for fn in FUNCLIST:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
122 setattr(self, fn, '0')
2466
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
123
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
124 #
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
125 # Special case for structure-access objects: put getattr in the
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
126 # list of functions but don't generate code for it directly,
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
127 # the code is obtained from the object_structure template.
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
128 # The same goes for setattr.
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
129 #
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
130 if 'structure' in self.typelist:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
131 if 'tp_getattr' in self.funclist:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
132 self.funclist.remove('tp_getattr')
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
133 if 'tp_setattr' in self.funclist:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
134 self.funclist.remove('tp_setattr')
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
135 self.tp_getattr = self.abbrev + '_getattr'
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
136 self.tp_setattr = self.abbrev + '_setattr'
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
137 for fn in self.funclist:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
138 self.addcode('object_'+fn, fp)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
139 setattr(self, fn, '%s_%s'%(self.abbrev, fn[3:]))
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
140 for tn in TYPELIST:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
141 setattr(self, tn, '0')
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
142 for tn in self.typelist:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
143 self.addcode('object_'+tn, fp)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
144 setattr(self, tn, '&%s_%s'%(self.abbrev, tn[3:]))
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
145 self.addcode('object_tail', fp)
2466
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
146
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 def write(fp, obj):
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 obj.writecode(fp)
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 if __name__ == '__main__':
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 o = object()
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 o.name = 'dwarve object'
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 o.abbrev = 'dw'
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 o.funclist = ['new', 'tp_dealloc']
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 o.methodlist = ['dig']
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 m = module()
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 m.name = 'beings'
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 m.abbrev = 'be'
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 m.methodlist = ['newdwarve']
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 m.objects = [o]
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 write(sys.stdout, m)