Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4f85bf3

Browse files
committed
Patch #437683: Use re instead of regex.
If multiple header files are processed simultaneously which include each other, the corresponding modules mport each other. Specifically, if h2py is invoked with sys/types.h first, later header files won't contain the complete contents of TYPES.py.
1 parent a2ac602 commit 4f85bf3

1 file changed

Lines changed: 38 additions & 33 deletions

File tree

Tools/scripts/h2py.py

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,35 @@
2121
# - what to do about #if(def)?
2222
# - what to do about macros with multiple parameters?
2323

24-
import sys, regex, regsub, string, getopt, os
24+
import sys, re, getopt, os
2525

26-
p_define = regex.compile('^[\t ]*#[\t ]*define[\t ]+\([a-zA-Z0-9_]+\)[\t ]+')
26+
p_define = re.compile('^[\t ]*#[\t ]*define[\t ]+([a-zA-Z0-9_]+)[\t ]+')
2727

28-
p_macro = regex.compile(
28+
p_macro = re.compile(
2929
'^[\t ]*#[\t ]*define[\t ]+'
30-
'\([a-zA-Z0-9_]+\)(\([_a-zA-Z][_a-zA-Z0-9]*\))[\t ]+')
30+
'([a-zA-Z0-9_]+)\(([_a-zA-Z][_a-zA-Z0-9]*)\)[\t ]+')
3131

32-
p_include = regex.compile('^[\t ]*#[\t ]*include[\t ]+<\([a-zA-Z0-9_/\.]+\)')
32+
p_include = re.compile('^[\t ]*#[\t ]*include[\t ]+<([a-zA-Z0-9_/\.]+)')
3333

34-
p_comment = regex.compile('/\*\([^*]+\|\*+[^/]\)*\(\*+/\)?')
35-
p_cpp_comment = regex.compile('//.*')
34+
p_comment = re.compile(r'/\*([^*]+|\*+[^/])*(\*+/)?')
35+
p_cpp_comment = re.compile('//.*')
3636

3737
ignores = [p_comment, p_cpp_comment]
3838

39-
p_char = regex.compile("'\(\\\\.[^\\\\]*\|[^\\\\]\)'")
39+
p_char = re.compile(r"'(\\.[^\\]*|[^\\])'")
4040

4141
filedict = {}
42+
importable = {}
4243

4344
try:
44-
searchdirs=string.splitfields(os.environ['include'],';')
45+
searchdirs=os.environ['include'].splitfields(';')
4546
except KeyError:
4647
try:
47-
searchdirs=string.splitfields(os.environ['INCLUDE'],';')
48+
searchdirs=os.environ['INCLUDE'].splitfields(';')
4849
except KeyError:
4950
try:
50-
if string.find( sys.platform, "beos" ) == 0:
51-
searchdirs=string.splitfields(os.environ['BEINCLUDES'],';')
51+
if sys.platform.find("beos") == 0:
52+
searchdirs=os.environ['BEINCLUDES'].splitfields(';')
5253
else:
5354
raise KeyError
5455
except KeyError:
@@ -59,7 +60,7 @@ def main():
5960
opts, args = getopt.getopt(sys.argv[1:], 'i:')
6061
for o, a in opts:
6162
if o == '-i':
62-
ignores.append(regex.compile(a))
63+
ignores.append(re.compile(a))
6364
if not args:
6465
args = ['-']
6566
for filename in args:
@@ -69,16 +70,17 @@ def main():
6970
else:
7071
fp = open(filename, 'r')
7172
outfile = os.path.basename(filename)
72-
i = string.rfind(outfile, '.')
73+
i = outfile.rfind('.')
7374
if i > 0: outfile = outfile[:i]
74-
outfile = string.upper(outfile)
75-
outfile = outfile + '.py'
75+
modname = outfile.upper()
76+
outfile = modname + '.py'
7677
outfp = open(outfile, 'w')
7778
outfp.write('# Generated by h2py from %s\n' % filename)
7879
filedict = {}
7980
for dir in searchdirs:
8081
if filename[:len(dir)] == dir:
8182
filedict[filename[len(dir)+1:]] = None # no '/' trailing
83+
importable[filename[len(dir)+1:]] = modname
8284
break
8385
process(fp, outfp)
8486
outfp.close()
@@ -90,53 +92,56 @@ def process(fp, outfp, env = {}):
9092
line = fp.readline()
9193
if not line: break
9294
lineno = lineno + 1
93-
n = p_define.match(line)
94-
if n >= 0:
95+
match = p_define.match(line)
96+
if match:
9597
# gobble up continuation lines
9698
while line[-2:] == '\\\n':
9799
nextline = fp.readline()
98100
if not nextline: break
99101
lineno = lineno + 1
100102
line = line + nextline
101-
name = p_define.group(1)
102-
body = line[n:]
103+
name = match.group(1)
104+
body = line[match.end():]
103105
# replace ignored patterns by spaces
104106
for p in ignores:
105-
body = regsub.gsub(p, ' ', body)
107+
body = p.sub(' ', body)
106108
# replace char literals by ord(...)
107-
body = regsub.gsub(p_char, 'ord(\\0)', body)
108-
stmt = '%s = %s\n' % (name, string.strip(body))
109+
body = p_char.sub('ord(\\0)', body)
110+
stmt = '%s = %s\n' % (name, body.strip())
109111
ok = 0
110112
try:
111113
exec stmt in env
112114
except:
113115
sys.stderr.write('Skipping: %s' % stmt)
114116
else:
115117
outfp.write(stmt)
116-
n =p_macro.match(line)
117-
if n >= 0:
118-
macro, arg = p_macro.group(1, 2)
119-
body = line[n:]
118+
match = p_macro.match(line)
119+
if match:
120+
macro, arg = match.group(1, 2)
121+
body = line[match.end():]
120122
for p in ignores:
121-
body = regsub.gsub(p, ' ', body)
122-
body = regsub.gsub(p_char, 'ord(\\0)', body)
123+
body = p.sub(' ', body)
124+
body = p_char.sub('ord(\\0)', body)
123125
stmt = 'def %s(%s): return %s\n' % (macro, arg, body)
124126
try:
125127
exec stmt in env
126128
except:
127129
sys.stderr.write('Skipping: %s' % stmt)
128130
else:
129131
outfp.write(stmt)
130-
if p_include.match(line) >= 0:
131-
regs = p_include.regs
132+
match = p_include.match(line)
133+
if match:
134+
regs = match.regs
132135
a, b = regs[1]
133136
filename = line[a:b]
134-
if not filedict.has_key(filename):
137+
if importable.has_key(filename):
138+
outfp.write('import %s\n' % importable[filename])
139+
elif not filedict.has_key(filename):
135140
filedict[filename] = None
136141
inclfp = None
137142
for dir in searchdirs:
138143
try:
139-
inclfp = open(dir + '/' + filename, 'r')
144+
inclfp = open(dir + '/' + filename)
140145
break
141146
except IOError:
142147
pass

0 commit comments

Comments
 (0)