From 4236bf19c57e29a262c6f03cc69d6a01d6c47b1e Mon Sep 17 00:00:00 2001
From: Andreas Kloeckner <inform@tiker.net>
Date: Fri, 26 Jun 2015 14:52:05 -0500
Subject: [PATCH] Py3 fixes

---
 gen_wrap.py       | 14 +++++++++++++-
 islpy/__init__.py | 19 ++++++++++++++++---
 setup.py          |  9 +++++++--
 3 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/gen_wrap.py b/gen_wrap.py
index 16c02f7..07818bf 100644
--- a/gen_wrap.py
+++ b/gen_wrap.py
@@ -38,6 +38,8 @@ ISL_SEM_TO_SEM = {
     "__isl_null": SEM_NULL,
     }
 
+PY3 = sys.version_info >= (3,)
+
 NON_COPYABLE = ["ctx", "printer", "access_info"]
 NON_COPYABLE_WITH_ISL_PREFIX = ["isl_"+i for i in NON_COPYABLE]
 
@@ -1261,7 +1263,10 @@ def write_method_wrapper(gen, cls_name, meth):
         with Indentation(post_call):
             post_call("_str_ret = None")
 
-        ret_vals.insert(0, "_str_ret")
+        if PY3:
+            ret_vals.insert(0, "_str_ret.decode()")
+        else:
+            ret_vals.insert(0, "_str_ret")
 
         if meth.return_semantics is SEM_GIVE:
             post_call("libc.free(_result)")
@@ -1326,6 +1331,13 @@ def write_method_wrapper(gen, cls_name, meth):
                 method_val=method_val))
     gen("")
 
+    if meth.is_static:
+        gen("{py_cls}._{name}_is_static = True"
+                .format(
+                    py_cls=isl_class_to_py_class(meth.cls),
+                    name=meth.name))
+        gen("")
+
 # }}}
 
 
diff --git a/islpy/__init__.py b/islpy/__init__.py
index 4b2869a..f3a9c2a 100644
--- a/islpy/__init__.py
+++ b/islpy/__init__.py
@@ -831,19 +831,32 @@ def _add_functionality():
 
     def add_upcasts(basic_class, special_class, upcast_method):
         from functools import update_wrapper
-        from inspect import ismethod
+
+        def my_ismethod(class_, method_name):
+            if method_name.startswith("_"):
+                return False
+
+            method = getattr(class_, method_name)
+
+            if not callable(method):
+                return False
+
+            if hasattr(class_, "_%s_is_static" % method_name):
+                return False
+
+            return True
 
         for method_name in dir(special_class):
             special_method = getattr(special_class, method_name)
 
-            if not ismethod(special_method):
+            if not my_ismethod(special_class, method_name):
                 continue
 
             if hasattr(basic_class, method_name):
                 # method already exists in basic class
                 basic_method = getattr(basic_class, method_name)
 
-                if not ismethod(basic_method):
+                if not my_ismethod(basic_class, method_name):
                     continue
 
                 wrapper = make_existing_upcast_wrapper(
diff --git a/setup.py b/setup.py
index 4b57aaf..4b0e887 100644
--- a/setup.py
+++ b/setup.py
@@ -204,7 +204,9 @@ def main():
         LIBRARIES.extend(conf["GMP_LIBNAME"])
 
     init_filename = "islpy/version.py"
-    exec(compile(open(init_filename, "r").read(), init_filename, "exec"), conf)
+    with open(init_filename, "r") as version_f:
+        version_py = version_f.read()
+    exec(compile(version_py, init_filename, "exec"), conf)
 
     from gen_wrap import gen_wrapper
     headers = gen_wrapper(wrapper_dirs, include_barvinok=conf["USE_BARVINOK"],
@@ -221,10 +223,13 @@ def main():
             LDFLAGS=conf["LDFLAGS"]
             )
 
+    with open("README.rst", "rt") as readme_f:
+        readme = readme_f.read()
+
     setup(name="islpy",
           version=conf["VERSION_TEXT"],
           description="Wrapper around isl, an integer set library",
-          long_description=open("README.rst", "rt").read(),
+          long_description=readme,
           author="Andreas Kloeckner",
           author_email="inform@tiker.net",
           license="MIT",
-- 
GitLab