summaryrefslogtreecommitdiffstats
path: root/Tools/scripts
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2001-08-09 12:24:38 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2001-08-09 12:24:38 (GMT)
commit4f85bf3311e9ac09ec64e74f1ce9cf3e2d223543 (patch)
tree003028e10bac4fb57824171b0d63b18a699bccae /Tools/scripts
parenta2ac60277c8d4ae46ff71e110359a94f1726cc5e (diff)
downloadcpython-4f85bf3311e9ac09ec64e74f1ce9cf3e2d223543.zip
cpython-4f85bf3311e9ac09ec64e74f1ce9cf3e2d223543.tar.gz
cpython-4f85bf3311e9ac09ec64e74f1ce9cf3e2d223543.tar.bz2
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.
Diffstat (limited to 'Tools/scripts')
-rwxr-xr-xTools/scripts/h2py.py71
1 files changed, 38 insertions, 33 deletions
diff --git a/Tools/scripts/h2py.py b/Tools/scripts/h2py.py
index 92f64c0..4e81c86 100755
--- a/Tools/scripts/h2py.py
+++ b/Tools/scripts/h2py.py
@@ -21,34 +21,35 @@
# - what to do about #if(def)?
# - 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 ]+'
- '\([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_cpp_comment = regex.compile('//.*')
+p_comment = re.compile(r'/\*([^*]+|\*+[^/])*(\*+/)?')
+p_cpp_comment = re.compile('//.*')
ignores = [p_comment, p_cpp_comment]
-p_char = regex.compile("'\(\\\\.[^\\\\]*\|[^\\\\]\)'")
+p_char = re.compile(r"'(\\.[^\\]*|[^\\])'")
filedict = {}
+importable = {}
try:
- searchdirs=string.splitfields(os.environ['include'],';')
+ searchdirs=os.environ['include'].splitfields(';')
except KeyError:
try:
- searchdirs=string.splitfields(os.environ['INCLUDE'],';')
+ searchdirs=os.environ['INCLUDE'].splitfields(';')
except KeyError:
try:
- if string.find( sys.platform, "beos" ) == 0:
- searchdirs=string.splitfields(os.environ['BEINCLUDES'],';')
+ if sys.platform.find("beos") == 0:
+ searchdirs=os.environ['BEINCLUDES'].splitfields(';')
else:
raise KeyError
except KeyError:
@@ -59,7 +60,7 @@ def main():
opts, args = getopt.getopt(sys.argv[1:], 'i:')
for o, a in opts:
if o == '-i':
- ignores.append(regex.compile(a))
+ ignores.append(re.compile(a))
if not args:
args = ['-']
for filename in args:
@@ -69,16 +70,17 @@ def main():
else:
fp = open(filename, 'r')
outfile = os.path.basename(filename)
- i = string.rfind(outfile, '.')
+ i = outfile.rfind('.')
if i > 0: outfile = outfile[:i]
- outfile = string.upper(outfile)
- outfile = outfile + '.py'
+ modname = outfile.upper()
+ outfile = modname + '.py'
outfp = open(outfile, 'w')
outfp.write('# Generated by h2py from %s\n' % filename)
filedict = {}
for dir in searchdirs:
if filename[:len(dir)] == dir:
filedict[filename[len(dir)+1:]] = None # no '/' trailing
+ importable[filename[len(dir)+1:]] = modname
break
process(fp, outfp)
outfp.close()
@@ -90,22 +92,22 @@ def process(fp, outfp, env = {}):
line = fp.readline()
if not line: break
lineno = lineno + 1
- n = p_define.match(line)
- if n >= 0:
+ match = p_define.match(line)
+ if match:
# gobble up continuation lines
while line[-2:] == '\\\n':
nextline = fp.readline()
if not nextline: break
lineno = lineno + 1
line = line + nextline
- name = p_define.group(1)
- body = line[n:]
+ name = match.group(1)
+ body = line[match.end():]
# replace ignored patterns by spaces
for p in ignores:
- body = regsub.gsub(p, ' ', body)
+ body = p.sub(' ', body)
# replace char literals by ord(...)
- body = regsub.gsub(p_char, 'ord(\\0)', body)
- stmt = '%s = %s\n' % (name, string.strip(body))
+ body = p_char.sub('ord(\\0)', body)
+ stmt = '%s = %s\n' % (name, body.strip())
ok = 0
try:
exec stmt in env
@@ -113,13 +115,13 @@ def process(fp, outfp, env = {}):
sys.stderr.write('Skipping: %s' % stmt)
else:
outfp.write(stmt)
- n =p_macro.match(line)
- if n >= 0:
- macro, arg = p_macro.group(1, 2)
- body = line[n:]
+ match = p_macro.match(line)
+ if match:
+ macro, arg = match.group(1, 2)
+ body = line[match.end():]
for p in ignores:
- body = regsub.gsub(p, ' ', body)
- body = regsub.gsub(p_char, 'ord(\\0)', body)
+ body = p.sub(' ', body)
+ body = p_char.sub('ord(\\0)', body)
stmt = 'def %s(%s): return %s\n' % (macro, arg, body)
try:
exec stmt in env
@@ -127,16 +129,19 @@ def process(fp, outfp, env = {}):
sys.stderr.write('Skipping: %s' % stmt)
else:
outfp.write(stmt)
- if p_include.match(line) >= 0:
- regs = p_include.regs
+ match = p_include.match(line)
+ if match:
+ regs = match.regs
a, b = regs[1]
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
inclfp = None
for dir in searchdirs:
try:
- inclfp = open(dir + '/' + filename, 'r')
+ inclfp = open(dir + '/' + filename)
break
except IOError:
pass