diff options
author | Eric Fiselier <eric@efcs.ca> | 2017-01-16 19:52:58 +0000 |
---|---|---|
committer | Eric Fiselier <eric@efcs.ca> | 2017-01-16 19:52:58 +0000 |
commit | a0aa6c3c80ce6050b954726fa6552e75c6b75eaa (patch) | |
tree | 18a07fb2faaa06f313cf5bee3c52146920110a0c | |
parent | ab8b32de7199703905f2d3a5f891bfa163bba2d0 (diff) | |
download | bcm5719-llvm-a0aa6c3c80ce6050b954726fa6552e75c6b75eaa.tar.gz bcm5719-llvm-a0aa6c3c80ce6050b954726fa6552e75c6b75eaa.zip |
Make sym_check python 3 compatible
llvm-svn: 292152
-rw-r--r-- | libcxx/utils/sym_check/sym_check/util.py | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/libcxx/utils/sym_check/sym_check/util.py b/libcxx/utils/sym_check/sym_check/util.py index 9543984673c..32055d84c89 100644 --- a/libcxx/utils/sym_check/sym_check/util.py +++ b/libcxx/utils/sym_check/sym_check/util.py @@ -14,6 +14,25 @@ import subprocess import sys import re +def to_bytes(str): + # Encode to UTF-8 to get binary data. + if isinstance(str, bytes): + return str + return str.encode('utf-8') + +def to_string(bytes): + if isinstance(bytes, str): + return bytes + return to_bytes(bytes) + +def convert_string(bytes): + try: + return to_string(bytes.decode('utf-8')) + except AttributeError: # 'str' object has no attribute 'decode'. + return str(bytes) + except UnicodeError: + return str(bytes) + def execute_command(cmd, input_str=None): """ Execute a command, capture and return its output. @@ -28,6 +47,8 @@ def execute_command(cmd, input_str=None): exitCode = p.wait() if exitCode == -signal.SIGINT: raise KeyboardInterrupt + out = convert_string(out) + err = convert_string(err) return out, err, exitCode @@ -105,13 +126,13 @@ def demangle_symbol(symbol): def is_elf(filename): - with open(filename, 'r') as f: + with open(filename, 'rb') as f: magic_bytes = f.read(4) - return magic_bytes == '\x7fELF' + return magic_bytes == b'\x7fELF' def is_mach_o(filename): - with open(filename, 'r') as f: + with open(filename, 'rb') as f: magic_bytes = f.read(4) return magic_bytes in [ '\xfe\xed\xfa\xce', # MH_MAGIC |