[Python-checkins] python/dist/src/Tools/scripts h2py.py,1.17,1.18

loewis@users.sourceforge.net loewis@users.sourceforge.net
Sat, 23 Nov 2002 04:08:12 -0800


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

Modified Files:
	h2py.py 
Log Message:
Expand negative hexadecimal constants.


Index: h2py.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Tools/scripts/h2py.py,v
retrieving revision 1.17
retrieving revision 1.18
diff -C2 -d -r1.17 -r1.18
*** h2py.py	11 Jun 2002 06:22:31 -0000	1.17
--- h2py.py	23 Nov 2002 12:08:10 -0000	1.18
***************
*** 39,42 ****
--- 39,44 ----
  p_char = re.compile(r"'(\\.[^\\]*|[^\\])'")
  
+ p_hex = re.compile(r"0x([0-9a-fA-F]+)L?")
+ 
  filedict = {}
  importable = {}
***************
*** 89,92 ****
--- 91,114 ----
              fp.close()
  
+ def pytify(body):
+     # 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)
+     # Compute negative hexadecimal constants
+     start = 0
+     UMAX = 2*(sys.maxint+1)
+     while 1:
+         m = p_hex.search(body, start)
+         if not m: break
+         s,e = m.span()
+         val = long(body[slice(*m.span(1))], 16)
+         if val > sys.maxint:
+             val -= UMAX
+             body = body[:s] + "(" + str(val) + ")" + body[e:]
+         start = s + 1
+     return body
+ 
  def process(fp, outfp, env = {}):
      lineno = 0
***************
*** 105,115 ****
              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:
                  exec stmt in env
--- 127,133 ----
              name = match.group(1)
              body = line[match.end():]
!             body = pytify(body)
              ok = 0
+             stmt = '%s = %s\n' % (name, body.strip())
              try:
                  exec stmt in env
***************
*** 122,128 ****
              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:
--- 140,144 ----
              macro, arg = match.group(1, 2)
              body = line[match.end():]
!             body = pytify(body)
              stmt = 'def %s(%s): return %s\n' % (macro, arg, body)
              try: