Commit d88a413c authored by Ralf Schmitt's avatar Ralf Schmitt

make util/cythonpp.py work with python 3

parent 9de962b3
...@@ -10,6 +10,12 @@ import pipes ...@@ -10,6 +10,12 @@ import pipes
import difflib import difflib
from hashlib import md5 from hashlib import md5
if sys.version_info >= (3, 0):
exec("def do_exec(co, loc): exec(co, loc)\n")
else:
exec("def do_exec(co, loc): exec co in loc\n")
_ex = lambda: sys.exc_info()[1]
# TODO: avoid using itertools.product which is not available on Python2.5 # TODO: avoid using itertools.product which is not available on Python2.5
...@@ -66,7 +72,7 @@ def process_filename(filename, output_filename=None): ...@@ -66,7 +72,7 @@ def process_filename(filename, output_filename=None):
for configuration, lines in preprocessed.items(): for configuration, lines in preprocessed.items():
counter += 1 counter += 1
value = ''.join(lines) value = ''.join(lines)
sourcehash = md5(value).hexdigest() sourcehash = md5(value.encode("utf-8")).hexdigest()
comment = convert_key_to_ifdef(configuration, short=True) comment = convert_key_to_ifdef(configuration, short=True)
atomic_write(pyx_filename, py_banner + value) atomic_write(pyx_filename, py_banner + value)
if WRITE_OUTPUT: if WRITE_OUTPUT:
...@@ -165,7 +171,8 @@ def preprocess_filename(filename, config): ...@@ -165,7 +171,8 @@ def preprocess_filename(filename, config):
lines = [x + '\n' for x in lines] lines = [x + '\n' for x in lines]
lines = [Str_sourceline(x, linecount - 1) for x in lines] lines = [Str_sourceline(x, linecount - 1) for x in lines]
result.extend(lines) result.extend(lines)
except BaseException, ex: except BaseException:
ex = _ex()
log('%s:%s: %s', filename, linecount, ex) log('%s:%s: %s', filename, linecount, ex)
if type(ex) is SyntaxError: if type(ex) is SyntaxError:
sys.exit(1) sys.exit(1)
...@@ -214,12 +221,13 @@ def expand_to_match(items): ...@@ -214,12 +221,13 @@ def expand_to_match(items):
for configuration, lines in items: for configuration, lines in items:
cfg2newlines[configuration] = [] cfg2newlines[configuration] = []
maxguard = 2**30
while True: while True:
minimalsourceline = sys.maxint minimalsourceline = maxguard
for configuration, lines in items: for configuration, lines in items:
if lines: if lines:
minimalsourceline = min(minimalsourceline, lines[0].sourceline) minimalsourceline = min(minimalsourceline, lines[0].sourceline)
if minimalsourceline == sys.maxint: if minimalsourceline == maxguard:
break break
for configuration, lines in items: for configuration, lines in items:
...@@ -324,8 +332,8 @@ class Str(str): ...@@ -324,8 +332,8 @@ class Str(str):
'join', 'replace', 'upper', 'lower'] 'join', 'replace', 'upper', 'lower']
for method in methods: for method in methods:
exec '''def %s(self, *args): do_exec('''def %s(self, *args):
return self.__class__(str.%s(self, *args), self.tag)''' % (method, method) return self.__class__(str.%s(self, *args), self.tag)''' % (method, method), locals())
def simplify_tag(tag): def simplify_tag(tag):
...@@ -336,7 +344,7 @@ def simplify_tag(tag): ...@@ -336,7 +344,7 @@ def simplify_tag(tag):
conditions = {} conditions = {}
for condition, flag in tag: for condition, flag in tag:
conditions.setdefault(condition, set()).add(flag) conditions.setdefault(condition, set()).add(flag)
for condition, flags in conditions.items(): for condition, flags in list(conditions.items()):
if flags == set([True, False]): if flags == set([True, False]):
conditions.pop(condition) conditions.pop(condition)
return set(((condition, flags.pop()) for (condition, flags) in conditions.items())) return set(((condition, flags.pop()) for (condition, flags) in conditions.items()))
...@@ -366,7 +374,7 @@ def parse_parameter_values(x): ...@@ -366,7 +374,7 @@ def parse_parameter_values(x):
def expand_definitions(code, definitions): def expand_definitions(code, definitions):
if not definitions: if not definitions:
return code return code
keys = definitions.keys() keys = list(definitions.keys())
keys.sort(key=lambda x: (-len(x), x)) keys.sort(key=lambda x: (-len(x), x))
keys = '|'.join(keys) keys = '|'.join(keys)
...@@ -405,7 +413,7 @@ def expand_definitions(code, definitions): ...@@ -405,7 +413,7 @@ def expand_definitions(code, definitions):
dbg('Replace %r with %r', m.group(0), result) dbg('Replace %r with %r', m.group(0), result)
return result return result
for _ in xrange(20000): for _ in range(20000):
newcode, count = re_macro.subn(repl, code, count=1) newcode, count = re_macro.subn(repl, code, count=1)
if code == newcode: if code == newcode:
if count > 0: if count > 0:
...@@ -545,7 +553,8 @@ def get_conditions(filename): ...@@ -545,7 +553,8 @@ def get_conditions(filename):
raise AssertionError('Internal error') raise AssertionError('Internal error')
else: else:
conditions.add(tuple(condition_stack)) conditions.add(tuple(condition_stack))
except BaseException, ex: except BaseException:
ex = _ex()
log('%s:%s: %s', filename, linecount, ex) log('%s:%s: %s', filename, linecount, ex)
if type(ex) is SyntaxError: if type(ex) is SyntaxError:
sys.exit(1) sys.exit(1)
...@@ -657,12 +666,12 @@ if __name__ == '__main__': ...@@ -657,12 +666,12 @@ if __name__ == '__main__':
if options.list_cond: if options.list_cond:
run = False run = False
for x in get_conditions(filename): for x in get_conditions(filename):
print '* %s' % (x, ) sys.stdout.write('* %s\n' % (x, ))
if options.list: if options.list:
run = False run = False
for x in get_configurations(filename): for x in get_configurations(filename):
print '* %s' % (x, ) sys.stdout.write('* %s\n' % (x, ))
if options.ignore_cond: if options.ignore_cond:
run = False run = False
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment