From 2d4a9c278b255f315c4d704572b109a1cc55b7fd Mon Sep 17 00:00:00 2001 From: Tom Henderson Date: Wed, 9 Sep 2015 15:14:27 -0700 Subject: [PATCH] additional Python 2/3 compatibility --- bindings/python/wscript | 17 +++++++++++++++-- src/wscript | 2 +- wscript | 16 +++++++++++++++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/bindings/python/wscript b/bindings/python/wscript index 1ee461b23..8d4526365 100644 --- a/bindings/python/wscript +++ b/bindings/python/wscript @@ -19,6 +19,19 @@ REQUIRED_PYGCCXML_VERSION = (0, 9, 5) RUN_ME=-3 +# return types of some APIs differ in Python 2/3 (type string vs class bytes) +# This method will decode('utf-8') a byte object in Python 3, +# and do nothing in Python 2 +def maybe_decode(input): + if sys.version_info < (3,): + return input + else: + try: + return input.decode('utf-8') + except: + sys.exc_clear() + return input + def add_to_python_path(path): if os.environ.get('PYTHONPATH', ''): os.environ['PYTHONPATH'] = path + os.pathsep + os.environ.get('PYTHONPATH') @@ -168,7 +181,7 @@ def configure(conf): "import pybindgen.version; " "print(pybindgen.__version__)"], stdout=subprocess.PIPE).communicate()[0] - pybindgen_version = out.strip() + pybindgen_version = maybe_decode(out.strip()) conf.msg('Checking for pybindgen version', pybindgen_version) if not (pybindgen_version == REQUIRED_PYBINDGEN_VERSION): Logs.warn("pybindgen (found %r), (need %r)" % @@ -415,7 +428,7 @@ class gen_ns3_compat_pymod_task(Task.Task): def run(self): assert len(self.outputs) == 1 - outfile = file(self.outputs[0].abspath(), "w") + outfile = open(self.outputs[0].abspath(), "w") print("import warnings", file=outfile) print('warnings.warn("the ns3 module is a compatibility layer '\ 'and should not be used in newly written code", DeprecationWarning, stacklevel=2)', file=outfile) diff --git a/src/wscript b/src/wscript index 932a2e1cc..b3dc96e74 100644 --- a/src/wscript +++ b/src/wscript @@ -721,7 +721,7 @@ def apply_ns3moduleheader(self): if ns3headers.module != self.module: continue found_the_module = True - for source in ns3headers.headers: + for source in sorted(ns3headers.headers): source = os.path.basename(source) node = ns3_dir_node.find_or_declare(os.path.basename(source)) if node is None: diff --git a/wscript b/wscript index efb27c4ee..4ec058907 100644 --- a/wscript +++ b/wscript @@ -116,6 +116,19 @@ def print_module_names(names): if i != 1: print() +# return types of some APIs differ in Python 2/3 (type string vs class bytes) +# This method will decode('utf-8') a byte object in Python 3, +# and do nothing in Python 2 +def maybe_decode(input): + if sys.version_info < (3,): + return input + else: + try: + return input.decode('utf-8') + except: + sys.exc_clear() + return input + def options(opt): # options provided by the modules opt.load('compiler_c') @@ -351,7 +364,8 @@ def configure(conf): cxx = env['CXX'] cxx_check_libstdcxx = cxx + ['-print-file-name=libstdc++.so'] p = subprocess.Popen(cxx_check_libstdcxx, stdout=subprocess.PIPE) - libstdcxx_location = os.path.dirname(p.stdout.read().strip()) + libstdcxx_output = maybe_decode(p.stdout.read().strip()) + libstdcxx_location = os.path.dirname(libstdcxx_output) p.wait() if libstdcxx_location: conf.env.append_value('NS3_MODULE_PATH', libstdcxx_location)