From e06ec8c423328ba943d6b21e0f06f9b8afba9195 Mon Sep 17 00:00:00 2001 From: Giacomo Travaglini Date: Mon, 2 Mar 2020 15:16:09 +0000 Subject: [PATCH] misc: Text vs Byte string in python3 Python 3 uses the concepts of two different types: text and binary strings. Those cannot be implicilty combined (as it was happening in python2) and in order to be used together one of them must be converted to the other type: * Text can be encoded into Bytes via the encode() method * Bytes can be decoded to Text using the decode() method By default encode/decode will assume UTF-8 format JIRA: https://gem5.atlassian.net/browse/GEM5-345 Change-Id: I1bdf7db17b49cc109239fd5f44791769370853f8 Signed-off-by: Giacomo Travaglini Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/26250 Tested-by: kokoro Reviewed-by: Jason Lowe-Power --- SConstruct | 2 +- src/SConscript | 6 +++--- src/python/m5/util/__init__.py | 6 ++++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/SConstruct b/SConstruct index 932eb819d..b93a00026 100755 --- a/SConstruct +++ b/SConstruct @@ -1013,7 +1013,7 @@ export_vars += ['USE_FENV', 'TARGET_ISA', 'TARGET_GPU_ISA', 'CP_ANNOTATE', # operands are the name of the variable and a Value node containing the # value of the variable. def build_config_file(target, source, env): - (variable, value) = [s.get_contents() for s in source] + (variable, value) = [s.get_contents().decode('utf-8') for s in source] with open(str(target[0]), 'w') as f: print('#define', variable, value, file=f) return None diff --git a/src/SConscript b/src/SConscript index e8b90acf5..552235a66 100644 --- a/src/SConscript +++ b/src/SConscript @@ -621,7 +621,7 @@ for opt in export_vars: env.ConfigFile(opt) def makeTheISA(source, target, env): - isas = [ src.get_contents() for src in source ] + isas = [ src.get_contents().decode('utf-8') for src in source ] target_isa = env['TARGET_ISA'] def define(isa): return isa.upper() + '_ISA' @@ -823,7 +823,7 @@ depends.sort(key = lambda x: x.name) # Generate Python file containing a dict specifying the current # buildEnv flags. def makeDefinesPyFile(target, source, env): - build_env = source[0].get_contents() + build_env = source[0].get_contents().decode('utf-8') code = code_formatter() code(""" @@ -882,7 +882,7 @@ def createSimObjectCxxConfig(is_header): def body(target, source, env): assert len(target) == 1 and len(source) == 1 - name = str(source[0].get_contents()) + name = source[0].get_contents().decode('utf-8') obj = sim_objects[name] code = code_formatter() diff --git a/src/python/m5/util/__init__.py b/src/python/m5/util/__init__.py index 2fdd5a19b..fd1ea9125 100644 --- a/src/python/m5/util/__init__.py +++ b/src/python/m5/util/__init__.py @@ -43,6 +43,8 @@ import os import re import sys +from six import string_types + from . import convert from . import jobfile @@ -122,7 +124,7 @@ def compareVersions(v1, v2): def make_version_list(v): if isinstance(v, (list,tuple)): return v - elif isinstance(v, str): + elif isinstance(v, string_types): return map(lambda x: int(re.match('\d+', x).group()), v.split('.')) else: raise TypeError() @@ -196,7 +198,7 @@ def readCommand(cmd, **kwargs): return exception raise - return subp.communicate()[0] + return subp.communicate()[0].decode('utf-8') def makeDir(path): """Make a directory if it doesn't exist. If the path does exist, -- 2.30.2