From b246c95266465d43d2fcbfdddf91a9eda2d11102 Mon Sep 17 00:00:00 2001 From: Noel Power Date: Wed, 6 Feb 2019 15:27:41 +0000 Subject: [PATCH] buildtools/wafsamba: Avoid decode when using python2 To avoid problematic type checking for 'str' types which fail when result from str.decode is used. Bug: https://bugzilla.samba.org/show_bug.cgi?id=13777 Signed-off-by: Noel Power --- buildtools/wafsamba/samba_abi.py | 2 +- buildtools/wafsamba/samba_conftests.py | 6 +++--- buildtools/wafsamba/samba_cross.py | 3 ++- buildtools/wafsamba/samba_dist.py | 4 ++-- buildtools/wafsamba/samba_perl.py | 4 ++-- buildtools/wafsamba/samba_utils.py | 32 ++++++++++++++++++++++++++++++++ buildtools/wafsamba/samba_version.py | 2 +- 7 files changed, 43 insertions(+), 10 deletions(-) diff --git a/buildtools/wafsamba/samba_abi.py b/buildtools/wafsamba/samba_abi.py index 80db7f87be5..5e7686da3d6 100644 --- a/buildtools/wafsamba/samba_abi.py +++ b/buildtools/wafsamba/samba_abi.py @@ -85,7 +85,7 @@ def abi_check_task(self): libpath = self.inputs[0].abspath(self.env) libname = os.path.basename(libpath) - sigs = Utils.cmd_output([abi_gen, libpath]).decode('utf8') + sigs = samba_utils.get_string(Utils.cmd_output([abi_gen, libpath])) parsed_sigs = parse_sigs(sigs, self.ABI_MATCH) sig_file = self.ABI_FILE diff --git a/buildtools/wafsamba/samba_conftests.py b/buildtools/wafsamba/samba_conftests.py index c0b9ae49296..7d9b5316902 100644 --- a/buildtools/wafsamba/samba_conftests.py +++ b/buildtools/wafsamba/samba_conftests.py @@ -4,7 +4,7 @@ import os, shutil, re from waflib import Build, Configure, Utils, Options, Logs, Errors from waflib.Configure import conf -from samba_utils import TO_LIST, ADD_LD_LIBRARY_PATH +from samba_utils import TO_LIST, ADD_LD_LIBRARY_PATH, get_string def add_option(self, *k, **kw): @@ -418,7 +418,7 @@ def CHECK_COMMAND(conf, cmd, msg=None, define=None, on_target=True, boolean=Fals if on_target: cmd.extend(conf.SAMBA_CROSS_ARGS(msg=msg)) try: - ret = Utils.cmd_output(cmd).decode('utf8') + ret = get_string(Utils.cmd_output(cmd)) except: conf.COMPOUND_END(False) return False @@ -508,7 +508,7 @@ def CHECK_STANDARD_LIBPATH(conf): # at least gcc and clang support this: try: cmd = conf.env.CC + ['-print-search-dirs'] - out = Utils.cmd_output(cmd).decode('utf8').split('\n') + out = get_string(Utils.cmd_output(cmd)).split('\n') except ValueError: # option not supported by compiler - use a standard list of directories dirlist = [ '/usr/lib', '/usr/lib64' ] diff --git a/buildtools/wafsamba/samba_cross.py b/buildtools/wafsamba/samba_cross.py index f9c4b10e82b..8863c2c53e7 100644 --- a/buildtools/wafsamba/samba_cross.py +++ b/buildtools/wafsamba/samba_cross.py @@ -3,6 +3,7 @@ import os, sys, re, shlex from waflib import Utils, Logs, Options, Errors, Context from waflib.Configure import conf +from wafsamba import samba_utils real_Popen = None @@ -121,7 +122,7 @@ class cross_Popen(Utils.subprocess.Popen): stdout=Utils.subprocess.PIPE, stderr=Utils.subprocess.PIPE) ce_out, ce_err = p.communicate() - ans = (p.returncode, ce_out.decode('utf8')) + ans = (p.returncode, samba_utils.get_string(ce_out)) add_answer(ca_file, msg, ans) else: args = newargs diff --git a/buildtools/wafsamba/samba_dist.py b/buildtools/wafsamba/samba_dist.py index c3144e9adf7..6af7bb4eaff 100644 --- a/buildtools/wafsamba/samba_dist.py +++ b/buildtools/wafsamba/samba_dist.py @@ -4,7 +4,7 @@ import os, sys, tarfile from waflib import Utils, Scripting, Logs, Options from waflib.Configure import conf -from samba_utils import os_path_relpath +from samba_utils import os_path_relpath, get_string from waflib import Context dist_dirs = None @@ -119,7 +119,7 @@ def vcs_dir_contents(path): repo = os.path.dirname(repo) if repo == "/": raise Exception("unsupported or no vcs for %s" % path) - return Utils.cmd_output(ls_files_cmd, cwd=cwd, env=env).decode('utf8').split('\n') + return get_string(Utils.cmd_output(ls_files_cmd, cwd=cwd, env=env)).split('\n') def dist(appname='', version=''): diff --git a/buildtools/wafsamba/samba_perl.py b/buildtools/wafsamba/samba_perl.py index 3d4fe29027f..e019acb0fa1 100644 --- a/buildtools/wafsamba/samba_perl.py +++ b/buildtools/wafsamba/samba_perl.py @@ -1,6 +1,6 @@ from waflib import Utils from waflib.Configure import conf - +from samba_utils import get_string done = {} @conf @@ -17,7 +17,7 @@ def SAMBA_CHECK_PERL(conf, mandatory=True, version=(5,0,0)): def read_perl_config_var(cmd): output = Utils.cmd_output([conf.env.get_flat('PERL'), '-MConfig', '-e', cmd]) if not isinstance(output, str): - output = output.decode('utf8') + output = get_string(output) return Utils.to_list(output) def check_perl_config_var(var): diff --git a/buildtools/wafsamba/samba_utils.py b/buildtools/wafsamba/samba_utils.py index 2a7f62f03bd..bc36d1f194d 100644 --- a/buildtools/wafsamba/samba_utils.py +++ b/buildtools/wafsamba/samba_utils.py @@ -15,6 +15,38 @@ from waflib.Build import CACHE_SUFFIX LIB_PATH="shared" +PY3 = sys.version_info[0] == 3 + +if PY3: + + # helper function to get a string from a variable that maybe 'str' or + # 'bytes' if 'bytes' then it is decoded using 'utf8'. If 'str' is passed + # it is returned unchanged + # Using this function is PY2/PY3 code should ensure in most cases + # the PY2 code runs unchanged in PY2 whereas the code in PY3 possibly + # decodes the variable (see PY2 implementation of this function below) + def get_string(bytesorstring): + tmp = bytesorstring + if isinstance(bytesorstring, bytes): + tmp = bytesorstring.decode('utf8') + elif not isinstance(bytesorstring, str): + raise ValueError('Expected byte of string for %s:%s' % (type(bytesorstring), bytesorstring)) + return tmp + +else: + + # Helper function to return string. + # if 'str' or 'unicode' passed in they are returned unchanged + # otherwise an exception is generated + # Using this function is PY2/PY3 code should ensure in most cases + # the PY2 code runs unchanged in PY2 whereas the code in PY3 possibly + # decodes the variable (see PY3 implementation of this function above) + def get_string(bytesorstring): + tmp = bytesorstring + if not(isinstance(bytesorstring, str) or isinstance(bytesorstring, unicode)): + raise ValueError('Expected str or unicode for %s:%s' % (type(bytesorstring), bytesorstring)) + return tmp + # sigh, python octal constants are a mess MODE_644 = int('644', 8) MODE_744 = int('744', 8) diff --git a/buildtools/wafsamba/samba_version.py b/buildtools/wafsamba/samba_version.py index 670001e753a..f0e7b4d0caf 100644 --- a/buildtools/wafsamba/samba_version.py +++ b/buildtools/wafsamba/samba_version.py @@ -14,7 +14,7 @@ def git_version_summary(path, env=None): environ = dict(os.environ) environ["GIT_DIR"] = '%s/.git' % path environ["GIT_WORK_TREE"] = path - git = Utils.cmd_output(env.GIT + ' show --pretty=format:"%h%n%ct%n%H%n%cd" --stat HEAD', silent=True, env=environ).decode('utf8') + git = samba_utils.get_string(Utils.cmd_output(env.GIT + ' show --pretty=format:"%h%n%ct%n%H%n%cd" --stat HEAD', silent=True, env=environ)) lines = git.splitlines() if not lines or len(lines) < 4: -- 2.16.4