Change to flush and close logic to fix #1760556.
[python.git] / Tools / freeze / makeconfig.py
blob7cd9b784788110a240f07aa4abf6483f103a1ae2
1 import re
4 # Write the config.c file
6 never = ['marshal', '__main__', '__builtin__', 'sys', 'exceptions']
8 def makeconfig(infp, outfp, modules, with_ifdef=0):
9 m1 = re.compile('-- ADDMODULE MARKER 1 --')
10 m2 = re.compile('-- ADDMODULE MARKER 2 --')
11 while 1:
12 line = infp.readline()
13 if not line: break
14 outfp.write(line)
15 if m1 and m1.search(line):
16 m1 = None
17 for mod in modules:
18 if mod in never:
19 continue
20 if with_ifdef:
21 outfp.write("#ifndef init%s\n"%mod)
22 outfp.write('extern void init%s(void);\n' % mod)
23 if with_ifdef:
24 outfp.write("#endif\n")
25 elif m2 and m2.search(line):
26 m2 = None
27 for mod in modules:
28 if mod in never:
29 continue
30 outfp.write('\t{"%s", init%s},\n' %
31 (mod, mod))
32 if m1:
33 sys.stderr.write('MARKER 1 never found\n')
34 elif m2:
35 sys.stderr.write('MARKER 2 never found\n')
38 # Test program.
40 def test():
41 import sys
42 if not sys.argv[3:]:
43 print 'usage: python makeconfig.py config.c.in outputfile',
44 print 'modulename ...'
45 sys.exit(2)
46 if sys.argv[1] == '-':
47 infp = sys.stdin
48 else:
49 infp = open(sys.argv[1])
50 if sys.argv[2] == '-':
51 outfp = sys.stdout
52 else:
53 outfp = open(sys.argv[2], 'w')
54 makeconfig(infp, outfp, sys.argv[3:])
55 if outfp != sys.stdout:
56 outfp.close()
57 if infp != sys.stdin:
58 infp.close()
60 if __name__ == '__main__':
61 test()