From d93210ad5af7657e0190df4ec4d99d405be004c5 Mon Sep 17 00:00:00 2001 From: Nguyen Anh Quynh Date: Sun, 4 Oct 2015 10:55:29 +0800 Subject: [PATCH] python: better support for Python3 (adapted from Capstone code) --- bindings/const_generator.py | 14 +++++++------- bindings/python/unicorn/unicorn.py | 3 +-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/bindings/const_generator.py b/bindings/const_generator.py index 1ce1b4fe..70814ec9 100644 --- a/bindings/const_generator.py +++ b/bindings/const_generator.py @@ -66,8 +66,8 @@ def gen(lang): templ = template[lang] for target in include: prefix = templ[target] - outfile = open(templ['out_file'] %(prefix), 'w') - outfile.write(templ['header'] % (prefix)) + outfile = open(templ['out_file'] %(prefix), 'wb') # open as binary prevents windows newlines + outfile.write((templ['header'] % (prefix)).encode("utf-8")) if target == 'unicorn.h': prefix = '' lines = open(INCL_DIR + target).readlines() @@ -78,8 +78,8 @@ def gen(lang): line = line.strip() if line.startswith(MARKUP): # markup for comments - outfile.write("\n%s%s%s\n" %(templ['comment_open'], \ - line.replace(MARKUP, ''), templ['comment_close'])) + outfile.write(("\n%s%s%s\n" %(templ['comment_open'], \ + line.replace(MARKUP, ''), templ['comment_close'])).encode("utf-8")) continue if line == '' or line.startswith('//'): @@ -126,12 +126,12 @@ def gen(lang): lhs_strip = re.sub(r'^UC_', '', lhs) count = int(rhs) + 1 if (count == 1): - outfile.write("\n") + outfile.write(("\n").encode("utf-8")) - outfile.write(templ['line_format'] % (lhs_strip, rhs)) + outfile.write((templ['line_format'] % (lhs_strip, rhs)).encode("utf-8")) previous[lhs] = str(rhs) - outfile.write(templ['footer']) + outfile.write((templ['footer']).encode("utf-8")) outfile.close() def main(): diff --git a/bindings/python/unicorn/unicorn.py b/bindings/python/unicorn/unicorn.py index d8fc62b0..05a498e4 100644 --- a/bindings/python/unicorn/unicorn.py +++ b/bindings/python/unicorn/unicorn.py @@ -323,8 +323,7 @@ def debug(): all_archs = "" keys = archs.keys() - keys.sort() - for k in keys: + for k in sorted(keys): if uc_arch_supported(archs[k]): all_archs += "-%s" % k