summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2002-11-23 12:08:10 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2002-11-23 12:08:10 (GMT)
commitdab3bc05f365996984542229cad017ca40422563 (patch)
tree9e2b03d2755e7fe6d3f6155547e6b7acbf260e43
parentb2c7affbaab984915b9401105334afffeedf706d (diff)
downloadcpython-dab3bc05f365996984542229cad017ca40422563.zip
cpython-dab3bc05f365996984542229cad017ca40422563.tar.gz
cpython-dab3bc05f365996984542229cad017ca40422563.tar.bz2
Expand negative hexadecimal constants.
-rwxr-xr-xTools/scripts/h2py.py34
1 files changed, 25 insertions, 9 deletions
diff --git a/Tools/scripts/h2py.py b/Tools/scripts/h2py.py
index 393a4ad..e2f6054 100755
--- a/Tools/scripts/h2py.py
+++ b/Tools/scripts/h2py.py
@@ -38,6 +38,8 @@ ignores = [p_comment, p_cpp_comment]
p_char = re.compile(r"'(\\.[^\\]*|[^\\])'")
+p_hex = re.compile(r"0x([0-9a-fA-F]+)L?")
+
filedict = {}
importable = {}
@@ -88,6 +90,26 @@ def main():
outfp.close()
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
while 1:
@@ -104,13 +126,9 @@ def process(fp, outfp, env = {}):
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())
+ body = pytify(body)
ok = 0
+ stmt = '%s = %s\n' % (name, body.strip())
try:
exec stmt in env
except:
@@ -121,9 +139,7 @@ def process(fp, outfp, env = {}):
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)
+ body = pytify(body)
stmt = 'def %s(%s): return %s\n' % (macro, arg, body)
try:
exec stmt in env