buildtools/wafsamba: Avoid decode when using python2
authorNoel Power <noel.power@suse.com>
Wed, 6 Feb 2019 15:27:41 +0000 (15:27 +0000)
committerAndrew Bartlett <abartlet@samba.org>
Mon, 11 Feb 2019 06:43:31 +0000 (07:43 +0100)
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 <noel.power@suse.com>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
buildtools/wafsamba/samba_abi.py
buildtools/wafsamba/samba_conftests.py
buildtools/wafsamba/samba_cross.py
buildtools/wafsamba/samba_dist.py
buildtools/wafsamba/samba_perl.py
buildtools/wafsamba/samba_utils.py
buildtools/wafsamba/samba_version.py

index 80db7f87be5dcecfda623fffa86d069328101132..5e7686da3d68b1ebcd842b8a319a5997fa9cf600 100644 (file)
@@ -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
index c0b9ae4929649692c9870edf28e8e0aa94fca113..7d9b531690236cd482efb984ea1387180c3ea7f1 100644 (file)
@@ -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' ]
index f9c4b10e82bdf7172bf6dfffd1a683704a2430c3..8863c2c53e7d7dd9317c9233f0085ffd0eea6b2f 100644 (file)
@@ -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
index c3144e9adf74a3b0af538895accaefae61a0ad2e..6af7bb4eaff7cca61e933a7cff6f0518005c9181 100644 (file)
@@ -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=''):
index 3d4fe29027f7d6706f810fc303b2e4c6f521aa34..e019acb0fa17d35775b3583eb3e5f121e287e9b6 100644 (file)
@@ -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):
index 2a7f62f03bdaee73a6d2d6fddbb9ae0a12cd2d5d..bc36d1f194d979825feb052a335ad4f3da6163e9 100644 (file)
@@ -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)
index 670001e753a52158a1f4ae486fa21df2555034a0..f0e7b4d0caf0e2ae8ad01c7e5d00e18ff14bcc52 100644 (file)
@@ -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: