[Python-checkins] CVS: python/dist/src/Tools/scripts h2py.py,1.12,1.13

Martin v. L?wis loewis@users.sourceforge.net
Thu, 09 Aug 2001 05:24:40 -0700


Update of /cvsroot/python/python/dist/src/Tools/scripts
In directory usw-pr-cvs1:/tmp/cvs-serv6829

Modified Files:
	h2py.py 
Log Message:
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. 


Index: h2py.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Tools/scripts/h2py.py,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** h2py.py	2001/01/17 08:48:39	1.12
--- h2py.py	2001/08/09 12:24:38	1.13
***************
*** 22,53 ****
  # - what to do about macros with multiple parameters?
  
! import sys, regex, regsub, string, getopt, os
  
! p_define = regex.compile('^[\t ]*#[\t ]*define[\t ]+\([a-zA-Z0-9_]+\)[\t ]+')
  
! p_macro = regex.compile(
    '^[\t ]*#[\t ]*define[\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_comment = regex.compile('/\*\([^*]+\|\*+[^/]\)*\(\*+/\)?')
! p_cpp_comment = regex.compile('//.*')
  
  ignores = [p_comment, p_cpp_comment]
  
! p_char = regex.compile("'\(\\\\.[^\\\\]*\|[^\\\\]\)'")
  
  filedict = {}
  
  try:
!     searchdirs=string.splitfields(os.environ['include'],';')
  except KeyError:
      try:
!         searchdirs=string.splitfields(os.environ['INCLUDE'],';')
      except KeyError:
          try:
!             if string.find( sys.platform, "beos" ) == 0:
!                 searchdirs=string.splitfields(os.environ['BEINCLUDES'],';')
              else:
                  raise KeyError
--- 22,54 ----
  # - what to do about macros with multiple parameters?
  
! import sys, re, getopt, os
  
! p_define = re.compile('^[\t ]*#[\t ]*define[\t ]+([a-zA-Z0-9_]+)[\t ]+')
  
! p_macro = re.compile(
    '^[\t ]*#[\t ]*define[\t ]+'
!   '([a-zA-Z0-9_]+)\(([_a-zA-Z][_a-zA-Z0-9]*)\)[\t ]+')
  
! p_include = re.compile('^[\t ]*#[\t ]*include[\t ]+<([a-zA-Z0-9_/\.]+)')
  
! p_comment = re.compile(r'/\*([^*]+|\*+[^/])*(\*+/)?')
! p_cpp_comment = re.compile('//.*')
  
  ignores = [p_comment, p_cpp_comment]
  
! p_char = re.compile(r"'(\\.[^\\]*|[^\\])'")
  
  filedict = {}
+ importable = {}
  
  try:
!     searchdirs=os.environ['include'].splitfields(';')
  except KeyError:
      try:
!         searchdirs=os.environ['INCLUDE'].splitfields(';')
      except KeyError:
          try:
!             if  sys.platform.find("beos") == 0:
!                 searchdirs=os.environ['BEINCLUDES'].splitfields(';')
              else:
                  raise KeyError
***************
*** 60,64 ****
      for o, a in opts:
          if o == '-i':
!             ignores.append(regex.compile(a))
      if not args:
          args = ['-']
--- 61,65 ----
      for o, a in opts:
          if o == '-i':
!             ignores.append(re.compile(a))
      if not args:
          args = ['-']
***************
*** 70,77 ****
              fp = open(filename, 'r')
              outfile = os.path.basename(filename)
!             i = string.rfind(outfile, '.')
              if i > 0: outfile = outfile[:i]
!             outfile = string.upper(outfile)
!             outfile = outfile + '.py'
              outfp = open(outfile, 'w')
              outfp.write('# Generated by h2py from %s\n' % filename)
--- 71,78 ----
              fp = open(filename, 'r')
              outfile = os.path.basename(filename)
!             i = outfile.rfind('.')
              if i > 0: outfile = outfile[:i]
!             modname = outfile.upper()
!             outfile = modname + '.py'
              outfp = open(outfile, 'w')
              outfp.write('# Generated by h2py from %s\n' % filename)
***************
*** 80,83 ****
--- 81,85 ----
                  if filename[:len(dir)] == dir:
                      filedict[filename[len(dir)+1:]] = None  # no '/' trailing
+                     importable[filename[len(dir)+1:]] = modname
                      break
              process(fp, outfp)
***************
*** 91,96 ****
          if not line: break
          lineno = lineno + 1
!         n = p_define.match(line)
!         if n >= 0:
              # gobble up continuation lines
              while line[-2:] == '\\\n':
--- 93,98 ----
          if not line: break
          lineno = lineno + 1
!         match = p_define.match(line)
!         if match:
              # gobble up continuation lines
              while line[-2:] == '\\\n':
***************
*** 99,110 ****
                  lineno = lineno + 1
                  line = line + nextline
!             name = p_define.group(1)
!             body = line[n:]
              # replace ignored patterns by spaces
              for p in ignores:
!                 body = regsub.gsub(p, ' ', body)
              # replace char literals by ord(...)
!             body = regsub.gsub(p_char, 'ord(\\0)', body)
!             stmt = '%s = %s\n' % (name, string.strip(body))
              ok = 0
              try:
--- 101,112 ----
                  lineno = lineno + 1
                  line = line + nextline
!             name = match.group(1)
!             body = line[match.end():]
              # replace ignored patterns by spaces
              for p in ignores:
!                 body = p.sub(' ', body)
              # replace char literals by ord(...)
!             body = p_char.sub('ord(\\0)', body)
!             stmt = '%s = %s\n' % (name, body.strip())
              ok = 0
              try:
***************
*** 114,124 ****
              else:
                  outfp.write(stmt)
!         n =p_macro.match(line)
!         if n >= 0:
!             macro, arg = p_macro.group(1, 2)
!             body = line[n:]
              for p in ignores:
!                 body = regsub.gsub(p, ' ', body)
!             body = regsub.gsub(p_char, 'ord(\\0)', body)
              stmt = 'def %s(%s): return %s\n' % (macro, arg, body)
              try:
--- 116,126 ----
              else:
                  outfp.write(stmt)
!         match = p_macro.match(line)
!         if match:
!             macro, arg = match.group(1, 2)
!             body = line[match.end():]
              for p in ignores:
!                 body = p.sub(' ', body)
!             body = p_char.sub('ord(\\0)', body)
              stmt = 'def %s(%s): return %s\n' % (macro, arg, body)
              try:
***************
*** 128,141 ****
              else:
                  outfp.write(stmt)
!         if p_include.match(line) >= 0:
!             regs = p_include.regs
              a, b = regs[1]
              filename = line[a:b]
!             if not filedict.has_key(filename):
                  filedict[filename] = None
                  inclfp = None
                  for dir in searchdirs:
                      try:
!                         inclfp = open(dir + '/' + filename, 'r')
                          break
                      except IOError:
--- 130,146 ----
              else:
                  outfp.write(stmt)
!         match = p_include.match(line)
!         if match:
!             regs = match.regs
              a, b = regs[1]
              filename = line[a:b]
!             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)
                          break
                      except IOError: