additional Python 2/3 compatibility
authorTom Henderson <tomh@tomh.org>
Wed, 09 Sep 2015 15:14:27 -0700
changeset 11661 68c0e7f87bdf
parent 11660 47325a00bda8
child 11662 886aad6f6702
additional Python 2/3 compatibility
bindings/python/wscript
src/wscript
wscript
--- a/bindings/python/wscript	Wed Sep 09 09:47:36 2015 -0700
+++ b/bindings/python/wscript	Wed Sep 09 15:14:27 2015 -0700
@@ -19,6 +19,19 @@
 
 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 @@
                                 "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 @@
 
     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)
--- a/src/wscript	Wed Sep 09 09:47:36 2015 -0700
+++ b/src/wscript	Wed Sep 09 15:14:27 2015 -0700
@@ -721,7 +721,7 @@
             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:
--- a/wscript	Wed Sep 09 09:47:36 2015 -0700
+++ b/wscript	Wed Sep 09 15:14:27 2015 -0700
@@ -116,6 +116,19 @@
     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 @@
         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)