annotate Tools/scripts/h2py.py @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents 5726be8ee59f
children 73758db2f464
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1269
28948181fb2f *** empty log message ***
Guido van Rossum <guido@python.org>
parents: 898
diff changeset
1 #! /usr/local/bin/python
687
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
3 # Read #define's and translate to Python code.
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
4 # Handle #include statements.
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
5 # Handle #define macros with one argument.
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
6 # Anything that isn't recognized or doesn't translate into valid
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
7 # Python is ignored.
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
8
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
9 # Without filename arguments, acts as a filter.
1602
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
10 # If one or more filenames are given, output is written to corresponding
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
11 # filenames in the local directory, translated to all uppercase, with
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
12 # the extension replaced by ".py".
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
13
1639
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
14 # By passing one or more options of the form "-i regular_expression"
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
15 # you can specify additional strings to be ignored. This is useful
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
16 # e.g. to ignore casts to u_long: simply specify "-i '(u_long)'".
687
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 # XXX To do:
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 # - turn trailing C comments into Python comments
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # - turn C Boolean operators "&& || !" into Python "and or not"
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 # - what to do about #if(def)?
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
22 # - what to do about macros with multiple parameters?
687
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
24 import sys, regex, regsub, string, getopt, os
687
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
4112
5726be8ee59f Allow whitespace before '#' in preprocessor directives.
Guido van Rossum <guido@python.org>
parents: 2169
diff changeset
26 p_define = regex.compile('^[\t ]*#[\t ]*define[\t ]+\([a-zA-Z0-9_]+\)[\t ]+')
687
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
28 p_macro = regex.compile(
4112
5726be8ee59f Allow whitespace before '#' in preprocessor directives.
Guido van Rossum <guido@python.org>
parents: 2169
diff changeset
29 '^[\t ]*#[\t ]*define[\t ]+'
5726be8ee59f Allow whitespace before '#' in preprocessor directives.
Guido van Rossum <guido@python.org>
parents: 2169
diff changeset
30 '\([a-zA-Z0-9_]+\)(\([_a-zA-Z][_a-zA-Z0-9]*\))[\t ]+')
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
31
4112
5726be8ee59f Allow whitespace before '#' in preprocessor directives.
Guido van Rossum <guido@python.org>
parents: 2169
diff changeset
32 p_include = regex.compile('^[\t ]*#[\t ]*include[\t ]+<\([a-zA-Z0-9_/\.]+\)')
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
33
898
ef86502508e2 Fix regexp recognizing comments to cope with unterminated comments.
Guido van Rossum <guido@python.org>
parents: 687
diff changeset
34 p_comment = regex.compile('/\*\([^*]+\|\*+[^/]\)*\(\*+/\)?')
687
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
1639
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
36 ignores = [p_comment]
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
37
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
38 p_char = regex.compile("'\(\\\\.[^\\\\]*\|[^\\\\]\)'")
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
39
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
40 filedict = {}
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
41
2169
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
42 try:
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
43 searchdirs=string.splitfields(os.environ['include'],';')
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
44 except KeyError:
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
45 try:
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
46 searchdirs=string.splitfields(os.environ['INCLUDE'],';')
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
47 except KeyError:
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
48 searchdirs=['/usr/include']
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
49
687
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 def main():
1639
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
51 opts, args = getopt.getopt(sys.argv[1:], 'i:')
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
52 for o, a in opts:
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
53 if o == '-i':
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
54 ignores.append(regex.compile(a))
1602
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
55 if not args:
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
56 args = ['-']
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
57 for filename in args:
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
58 if filename == '-':
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
59 sys.stdout.write('# Generated by h2py from stdin\n')
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
60 process(sys.stdin, sys.stdout)
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
61 else:
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
62 fp = open(filename, 'r')
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
63 outfile = os.path.basename(filename)
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
64 i = string.rfind(outfile, '.')
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
65 if i > 0: outfile = outfile[:i]
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
66 outfile = string.upper(outfile)
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
67 outfile = outfile + '.py'
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
68 outfp = open(outfile, 'w')
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
69 outfp.write('# Generated by h2py from %s\n' % filename)
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
70 filedict = {}
2169
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
71 for dir in searchdirs:
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
72 if filename[:len(dir)] == dir:
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
73 filedict[filename[len(dir)+1:]] = None # no '/' trailing
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
74 break
1602
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
75 process(fp, outfp)
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
76 outfp.close()
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
77 fp.close()
687
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
79 def process(fp, outfp, env = {}):
687
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 lineno = 0
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 while 1:
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 line = fp.readline()
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 if not line: break
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 lineno = lineno + 1
1639
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
85 n = p_define.match(line)
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
86 if n >= 0:
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
87 # gobble up continuation lines
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
88 while line[-2:] == '\\\n':
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
89 nextline = fp.readline()
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
90 if not nextline: break
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
91 lineno = lineno + 1
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
92 line = line + nextline
1639
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
93 name = p_define.group(1)
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
94 body = line[n:]
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
95 # replace ignored patterns by spaces
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
96 for p in ignores:
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
97 body = regsub.gsub(p, ' ', body)
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
98 # replace char literals by ord(...)
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
99 body = regsub.gsub(p_char, 'ord(\\0)', body)
1602
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
100 stmt = '%s = %s\n' % (name, string.strip(body))
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
101 ok = 0
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
102 try:
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
103 exec stmt in env
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
104 except:
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
105 sys.stderr.write('Skipping: %s' % stmt)
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
106 else:
1602
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
107 outfp.write(stmt)
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
108 n =p_macro.match(line)
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
109 if n >= 0:
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
110 macro, arg = p_macro.group(1, 2)
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
111 body = line[n:]
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
112 for p in ignores:
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
113 body = regsub.gsub(p, ' ', body)
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
114 body = regsub.gsub(p_char, 'ord(\\0)', body)
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
115 stmt = 'def %s(%s): return %s\n' % (macro, arg, body)
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
116 try:
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
117 exec stmt in env
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
118 except:
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
119 sys.stderr.write('Skipping: %s' % stmt)
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
120 else:
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
121 outfp.write(stmt)
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
122 if p_include.match(line) >= 0:
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
123 regs = p_include.regs
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
124 a, b = regs[1]
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
125 filename = line[a:b]
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
126 if not filedict.has_key(filename):
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
127 filedict[filename] = None
2169
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
128 inclfp = None
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
129 for dir in searchdirs:
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
130 try:
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
131 inclfp = open(dir + '/' + filename, 'r')
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
132 break
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
133 except IOError:
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
134 pass
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
135 if inclfp:
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
136 outfp.write(
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
137 '\n# Included from %s\n' % filename)
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
138 process(inclfp, outfp, env)
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
139 else:
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
140 sys.stderr.write('Warning - could not find file %s' % filename)
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
141
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
142 main()