mirror of
https://github.com/python/cpython.git
synced 2025-10-17 12:18:23 +00:00
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.
This commit is contained in:
parent
a2ac60277c
commit
4f85bf3311
1 changed files with 38 additions and 33 deletions
|
@ -21,34 +21,35 @@
|
||||||
# - what to do about #if(def)?
|
# - what to do about #if(def)?
|
||||||
# - what to do about macros with multiple parameters?
|
# - what to do about macros with multiple parameters?
|
||||||
|
|
||||||
import sys, regex, regsub, string, getopt, os
|
import sys, re, getopt, os
|
||||||
|
|
||||||
p_define = regex.compile('^[\t ]*#[\t ]*define[\t ]+\([a-zA-Z0-9_]+\)[\t ]+')
|
p_define = re.compile('^[\t ]*#[\t ]*define[\t ]+([a-zA-Z0-9_]+)[\t ]+')
|
||||||
|
|
||||||
p_macro = regex.compile(
|
p_macro = re.compile(
|
||||||
'^[\t ]*#[\t ]*define[\t ]+'
|
'^[\t ]*#[\t ]*define[\t ]+'
|
||||||
'\([a-zA-Z0-9_]+\)(\([_a-zA-Z][_a-zA-Z0-9]*\))[\t ]+')
|
'([a-zA-Z0-9_]+)\(([_a-zA-Z][_a-zA-Z0-9]*)\)[\t ]+')
|
||||||
|
|
||||||
p_include = regex.compile('^[\t ]*#[\t ]*include[\t ]+<\([a-zA-Z0-9_/\.]+\)')
|
p_include = re.compile('^[\t ]*#[\t ]*include[\t ]+<([a-zA-Z0-9_/\.]+)')
|
||||||
|
|
||||||
p_comment = regex.compile('/\*\([^*]+\|\*+[^/]\)*\(\*+/\)?')
|
p_comment = re.compile(r'/\*([^*]+|\*+[^/])*(\*+/)?')
|
||||||
p_cpp_comment = regex.compile('//.*')
|
p_cpp_comment = re.compile('//.*')
|
||||||
|
|
||||||
ignores = [p_comment, p_cpp_comment]
|
ignores = [p_comment, p_cpp_comment]
|
||||||
|
|
||||||
p_char = regex.compile("'\(\\\\.[^\\\\]*\|[^\\\\]\)'")
|
p_char = re.compile(r"'(\\.[^\\]*|[^\\])'")
|
||||||
|
|
||||||
filedict = {}
|
filedict = {}
|
||||||
|
importable = {}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
searchdirs=string.splitfields(os.environ['include'],';')
|
searchdirs=os.environ['include'].splitfields(';')
|
||||||
except KeyError:
|
except KeyError:
|
||||||
try:
|
try:
|
||||||
searchdirs=string.splitfields(os.environ['INCLUDE'],';')
|
searchdirs=os.environ['INCLUDE'].splitfields(';')
|
||||||
except KeyError:
|
except KeyError:
|
||||||
try:
|
try:
|
||||||
if string.find( sys.platform, "beos" ) == 0:
|
if sys.platform.find("beos") == 0:
|
||||||
searchdirs=string.splitfields(os.environ['BEINCLUDES'],';')
|
searchdirs=os.environ['BEINCLUDES'].splitfields(';')
|
||||||
else:
|
else:
|
||||||
raise KeyError
|
raise KeyError
|
||||||
except KeyError:
|
except KeyError:
|
||||||
|
@ -59,7 +60,7 @@ def main():
|
||||||
opts, args = getopt.getopt(sys.argv[1:], 'i:')
|
opts, args = getopt.getopt(sys.argv[1:], 'i:')
|
||||||
for o, a in opts:
|
for o, a in opts:
|
||||||
if o == '-i':
|
if o == '-i':
|
||||||
ignores.append(regex.compile(a))
|
ignores.append(re.compile(a))
|
||||||
if not args:
|
if not args:
|
||||||
args = ['-']
|
args = ['-']
|
||||||
for filename in args:
|
for filename in args:
|
||||||
|
@ -69,16 +70,17 @@ def main():
|
||||||
else:
|
else:
|
||||||
fp = open(filename, 'r')
|
fp = open(filename, 'r')
|
||||||
outfile = os.path.basename(filename)
|
outfile = os.path.basename(filename)
|
||||||
i = string.rfind(outfile, '.')
|
i = outfile.rfind('.')
|
||||||
if i > 0: outfile = outfile[:i]
|
if i > 0: outfile = outfile[:i]
|
||||||
outfile = string.upper(outfile)
|
modname = outfile.upper()
|
||||||
outfile = outfile + '.py'
|
outfile = modname + '.py'
|
||||||
outfp = open(outfile, 'w')
|
outfp = open(outfile, 'w')
|
||||||
outfp.write('# Generated by h2py from %s\n' % filename)
|
outfp.write('# Generated by h2py from %s\n' % filename)
|
||||||
filedict = {}
|
filedict = {}
|
||||||
for dir in searchdirs:
|
for dir in searchdirs:
|
||||||
if filename[:len(dir)] == dir:
|
if filename[:len(dir)] == dir:
|
||||||
filedict[filename[len(dir)+1:]] = None # no '/' trailing
|
filedict[filename[len(dir)+1:]] = None # no '/' trailing
|
||||||
|
importable[filename[len(dir)+1:]] = modname
|
||||||
break
|
break
|
||||||
process(fp, outfp)
|
process(fp, outfp)
|
||||||
outfp.close()
|
outfp.close()
|
||||||
|
@ -90,22 +92,22 @@ def process(fp, outfp, env = {}):
|
||||||
line = fp.readline()
|
line = fp.readline()
|
||||||
if not line: break
|
if not line: break
|
||||||
lineno = lineno + 1
|
lineno = lineno + 1
|
||||||
n = p_define.match(line)
|
match = p_define.match(line)
|
||||||
if n >= 0:
|
if match:
|
||||||
# gobble up continuation lines
|
# gobble up continuation lines
|
||||||
while line[-2:] == '\\\n':
|
while line[-2:] == '\\\n':
|
||||||
nextline = fp.readline()
|
nextline = fp.readline()
|
||||||
if not nextline: break
|
if not nextline: break
|
||||||
lineno = lineno + 1
|
lineno = lineno + 1
|
||||||
line = line + nextline
|
line = line + nextline
|
||||||
name = p_define.group(1)
|
name = match.group(1)
|
||||||
body = line[n:]
|
body = line[match.end():]
|
||||||
# replace ignored patterns by spaces
|
# replace ignored patterns by spaces
|
||||||
for p in ignores:
|
for p in ignores:
|
||||||
body = regsub.gsub(p, ' ', body)
|
body = p.sub(' ', body)
|
||||||
# replace char literals by ord(...)
|
# replace char literals by ord(...)
|
||||||
body = regsub.gsub(p_char, 'ord(\\0)', body)
|
body = p_char.sub('ord(\\0)', body)
|
||||||
stmt = '%s = %s\n' % (name, string.strip(body))
|
stmt = '%s = %s\n' % (name, body.strip())
|
||||||
ok = 0
|
ok = 0
|
||||||
try:
|
try:
|
||||||
exec stmt in env
|
exec stmt in env
|
||||||
|
@ -113,13 +115,13 @@ def process(fp, outfp, env = {}):
|
||||||
sys.stderr.write('Skipping: %s' % stmt)
|
sys.stderr.write('Skipping: %s' % stmt)
|
||||||
else:
|
else:
|
||||||
outfp.write(stmt)
|
outfp.write(stmt)
|
||||||
n =p_macro.match(line)
|
match = p_macro.match(line)
|
||||||
if n >= 0:
|
if match:
|
||||||
macro, arg = p_macro.group(1, 2)
|
macro, arg = match.group(1, 2)
|
||||||
body = line[n:]
|
body = line[match.end():]
|
||||||
for p in ignores:
|
for p in ignores:
|
||||||
body = regsub.gsub(p, ' ', body)
|
body = p.sub(' ', body)
|
||||||
body = regsub.gsub(p_char, 'ord(\\0)', body)
|
body = p_char.sub('ord(\\0)', body)
|
||||||
stmt = 'def %s(%s): return %s\n' % (macro, arg, body)
|
stmt = 'def %s(%s): return %s\n' % (macro, arg, body)
|
||||||
try:
|
try:
|
||||||
exec stmt in env
|
exec stmt in env
|
||||||
|
@ -127,16 +129,19 @@ def process(fp, outfp, env = {}):
|
||||||
sys.stderr.write('Skipping: %s' % stmt)
|
sys.stderr.write('Skipping: %s' % stmt)
|
||||||
else:
|
else:
|
||||||
outfp.write(stmt)
|
outfp.write(stmt)
|
||||||
if p_include.match(line) >= 0:
|
match = p_include.match(line)
|
||||||
regs = p_include.regs
|
if match:
|
||||||
|
regs = match.regs
|
||||||
a, b = regs[1]
|
a, b = regs[1]
|
||||||
filename = line[a:b]
|
filename = line[a:b]
|
||||||
if not filedict.has_key(filename):
|
if importable.has_key(filename):
|
||||||
|
outfp.write('import %s\n' % importable[filename])
|
||||||
|
elif not filedict.has_key(filename):
|
||||||
filedict[filename] = None
|
filedict[filename] = None
|
||||||
inclfp = None
|
inclfp = None
|
||||||
for dir in searchdirs:
|
for dir in searchdirs:
|
||||||
try:
|
try:
|
||||||
inclfp = open(dir + '/' + filename, 'r')
|
inclfp = open(dir + '/' + filename)
|
||||||
break
|
break
|
||||||
except IOError:
|
except IOError:
|
||||||
pass
|
pass
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue