annotate Tools/modulator/genmodule.py @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents c3a096bfbd6b
children e4ade16a694f
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
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 error = 'genmodule.error'
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
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 # Names of functions in the object-description struct.
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 #
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 FUNCLIST = ['new', 'tp_dealloc', 'tp_print', 'tp_getattr', 'tp_setattr',
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
30 '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
31 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
32
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 # 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
35 # it contains code common to both.
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 #
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 class writer:
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 def __init__(self):
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
39 self._subst = None
2466
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 def makesubst(self):
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
42 if not self._subst:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
43 if not self.__dict__.has_key('abbrev'):
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
44 self.abbrev = self.name
25219
8a7a2f1f88f0 Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents: 7958
diff changeset
45 self.Abbrev = self.abbrev[0].upper()+self.abbrev[1:]
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
46 subst = varsubst.Varsubst(self.__dict__)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
47 subst.useindent(1)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
48 self._subst = subst.subst
2466
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 def addcode(self, name, fp):
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
51 ifp = self.opentemplate(name)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
52 self.makesubst()
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
53 d = ifp.read()
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
54 d = self._subst(d)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
55 fp.write(d)
2466
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
56
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 def opentemplate(self, name):
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
58 for p in sys.path:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
59 fn = os.path.join(p, name)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
60 if os.path.exists(fn):
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
61 return open(fn, 'r')
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
62 fn = os.path.join(p, 'Templates')
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
63 fn = os.path.join(fn, name)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
64 if os.path.exists(fn):
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
65 return open(fn, 'r')
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
66 raise error, 'Template '+name+' not found for '+self._type+' '+ \
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
67 self.name
32736
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 25219
diff changeset
68
2466
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 class module(writer):
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 _type = 'module'
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
71
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 def writecode(self, fp):
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
73 self.addcode('copyright', fp)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
74 self.addcode('module_head', fp)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
75 for o in self.objects:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
76 o.writehead(fp)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
77 for o in self.objects:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
78 o.writebody(fp)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
79 new_ml = ''
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
80 for fn in self.methodlist:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
81 self.method = fn
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
82 self.addcode('module_method', fp)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
83 new_ml = new_ml + (
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
84 '{"%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
85 %(fn, self.abbrev, fn, self.abbrev, fn))
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
86 self.methodlist = new_ml
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
87 self.addcode('module_tail', fp)
2466
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
88
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 class object(writer):
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 _type = 'object'
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 def __init__(self):
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
92 self.typelist = []
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
93 self.methodlist = []
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
94 self.funclist = ['new']
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
95 writer.__init__(self)
2466
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
96
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 def writecode(self, fp):
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
98 self.addcode('copyright', fp)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
99 self.writehead(fp)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
100 self.writebody(fp)
2466
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
101
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 def writehead(self, fp):
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
103 self.addcode('object_head', fp)
2466
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
104
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 def writebody(self, fp):
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
106 new_ml = ''
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
107 for fn in self.methodlist:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
108 self.method = fn
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
109 self.addcode('object_method', fp)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
110 new_ml = new_ml + (
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
111 '{"%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
112 %(fn, self.abbrev, fn, self.abbrev, fn))
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
113 self.methodlist = new_ml
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
114 self.addcode('object_mlist', fp)
2466
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
115
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
116 # Add getattr if we have methods
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
117 if self.methodlist and not 'tp_getattr' in self.funclist:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
118 self.funclist.insert(0, 'tp_getattr')
32736
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 25219
diff changeset
119
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
120 for fn in FUNCLIST:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
121 setattr(self, fn, '0')
2466
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
122
7958
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
123 #
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
124 # Special case for structure-access objects: put getattr in the
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
125 # list of functions but don't generate code for it directly,
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
126 # the code is obtained from the object_structure template.
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
127 # The same goes for setattr.
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
128 #
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
129 if 'structure' in self.typelist:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
130 if 'tp_getattr' in self.funclist:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
131 self.funclist.remove('tp_getattr')
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
132 if 'tp_setattr' in self.funclist:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
133 self.funclist.remove('tp_setattr')
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
134 self.tp_getattr = self.abbrev + '_getattr'
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
135 self.tp_setattr = self.abbrev + '_setattr'
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
136 for fn in self.funclist:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
137 self.addcode('object_'+fn, fp)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
138 setattr(self, fn, '%s_%s'%(self.abbrev, fn[3:]))
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
139 for tn in TYPELIST:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
140 setattr(self, tn, '0')
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
141 for tn in self.typelist:
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
142 self.addcode('object_'+tn, fp)
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
143 setattr(self, tn, '&%s_%s'%(self.abbrev, tn[3:]))
9e113aa3758d tabnanny stuff
Guido van Rossum <guido@python.org>
parents: 3443
diff changeset
144 self.addcode('object_tail', fp)
2466
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
145
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 def write(fp, obj):
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 obj.writecode(fp)
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
148
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 if __name__ == '__main__':
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 o = object()
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 o.name = 'dwarve object'
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 o.abbrev = 'dw'
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 o.funclist = ['new', 'tp_dealloc']
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 o.methodlist = ['dig']
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 m = module()
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 m.name = 'beings'
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 m.abbrev = 'be'
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 m.methodlist = ['newdwarve']
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 m.objects = [o]
322fc596f1c4 checkin of Jack's original version
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 write(sys.stdout, m)