You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/01/15 20:45:16 UTC

[GitHub] piiswrong closed pull request #9346: amalgamation library fixes for python 2.7 and 3.6 compatibility

piiswrong closed pull request #9346: amalgamation library fixes for python  2.7 and 3.6 compatibility
URL: https://github.com/apache/incubator-mxnet/pull/9346
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/amalgamation/amalgamation.py b/amalgamation/amalgamation.py
index f1e1e02f54..1a82413575 100644
--- a/amalgamation/amalgamation.py
+++ b/amalgamation/amalgamation.py
@@ -15,8 +15,11 @@
 # specific language governing permissions and limitations
 # under the License.
 
+from __future__ import print_function
+
 import sys
-import os.path, re, StringIO
+import os.path, re
+from io import BytesIO, StringIO
 import platform
 
 blacklist = [
@@ -39,15 +42,12 @@
     blacklist.append('linalg.h')
 
 if platform.system() != 'Darwin':
-  blacklist.append('TargetConditionals.h')
+    blacklist.append('TargetConditionals.h')
 
 if platform.system() != 'Windows':
-  blacklist.append('windows.h')
-  blacklist.append('process.h')
+    blacklist.append('windows.h')
+    blacklist.append('process.h')
 
-def pprint(lst):
-    for item in lst:
-        print item
 
 def get_sources(def_file):
     sources = []
@@ -67,6 +67,7 @@ def get_sources(def_file):
             visited.add(fn)
     return sources
 
+
 sources = get_sources(sys.argv[1])
 
 
@@ -93,9 +94,7 @@ def find_source(name, start, stage):
 
 sysheaders = []
 history = set([])
-out = StringIO.StringIO()
-
-
+out = BytesIO()
 
 
 def expand(x, pending, stage):
@@ -103,38 +102,44 @@ def expand(x, pending, stage):
         return
 
     if x in pending:
-        #print 'loop found: %s in ' % x, pending
+        #print('loop found: {} in {}'.format(x, pending))
         return
 
-    whtspace = '  '*expand.treeDepth
-    expand.fileCount+=1
-    print >>out, "//=====[%3d] STAGE:%4s %sEXPANDING: %s =====\n" % (expand.fileCount, stage, whtspace, x)
-    print        "//=====[%3d] STAGE:%4s %sEXPANDING: %s        " % (expand.fileCount, stage, whtspace, x)
-    for line in open(x):
-        if line.find('#include') < 0:
-            out.write(line)
-            continue
-        if line.strip().find('#include') > 0:
-            print line
-            continue
-        m = re1.search(line)
-        if not m: m = re2.search(line)
-        if not m:
-            print line + ' not found'
-            continue
-        h = m.groups()[0].strip('./')
-        source = find_source(h, x, stage)
-        if not source:
-            if (h not in blacklist and
-                h not in sysheaders and
-                'mkl' not in h and
-                'nnpack' not in h and
-                not h.endswith('.cuh')): sysheaders.append(h)
-        else:
-            expand.treeDepth+=1
-            expand(source, pending + [x], stage)
-            expand.treeDepth-=1
-    print >>out, "//===== EXPANDED  : %s =====\n" %x
+    whtspace = '  ' * expand.treeDepth
+    expand.fileCount += 1
+    comment = u"//=====[{:3d}] STAGE:{:>4} {}EXPANDING: {} =====\n\n".format(expand.fileCount, stage, whtspace, x)
+    out.write(comment.encode('ascii'))
+    print(comment)
+
+    with open(x, 'rb') as x_h:
+        for line in x_h.readlines():
+            uline = line.decode('utf-8')
+            if uline.find('#include') < 0:
+                out.write(line)
+                continue
+            if uline.strip().find('#include') > 0:
+                print(uline)
+                continue
+            m = re1.search(uline)
+            if not m:
+                m = re2.search(uline)
+            if not m:
+                print(uline + ' not found')
+                continue
+            h = m.groups()[0].strip('./')
+            source = find_source(h, x, stage)
+            if not source:
+                if (h not in blacklist and
+                    h not in sysheaders and
+                    'mkl' not in h and
+                    'nnpack' not in h and
+                    not h.endswith('.cuh')): sysheaders.append(h)
+            else:
+                expand.treeDepth += 1
+                expand(source, pending + [x], stage)
+                expand.treeDepth -= 1
+
+    out.write(u"//===== EXPANDED  : {} =====\n\n".format(x).encode('ascii'))
     history.add(x)
 
 
@@ -149,15 +154,16 @@ def expand(x, pending, stage):
 expand(sys.argv[4], [], "src")
 
 # Write to amalgamation file
-f = open(sys.argv[5], 'wb')
+with open(sys.argv[5], 'wb') as f:
 
-if minimum != 0:
-    sysheaders.remove('cblas.h')
-    print >>f, "#define MSHADOW_STAND_ALONE 1"
-    print >>f, "#define MSHADOW_USE_SSE 0"
-    print >>f, "#define MSHADOW_USE_CBLAS 0"
+    if minimum != 0:
+        sysheaders.remove('cblas.h')
+        f.write(b"#define MSHADOW_STAND_ALONE 1\n")
+        f.write(b"#define MSHADOW_USE_SSE 0\n")
+        f.write(b"#define MSHADOW_USE_CBLAS 0\n")
 
-print >>f, '''
+    f.write(
+        b"""
 #if defined(__MACH__)
 #include <mach/clock.h>
 #include <mach/mach.h>
@@ -172,19 +178,21 @@ def expand(x, pending, stage):
 #endif
 
 #endif
-'''
+\n"""
+    )
 
-if minimum != 0 and android != 0 and 'complex.h' not in sysheaders:
-    sysheaders.append('complex.h')
+    if minimum != 0 and android != 0 and 'complex.h' not in sysheaders:
+        sysheaders.append('complex.h')
 
-for k in sorted(sysheaders):
-    print >>f, "#include <%s>" % k
+    for k in sorted(sysheaders):
+        f.write("#include <{}>\n".format(k).encode('ascii'))
 
-print >>f, ''
-print >>f, out.getvalue()
+    f.write(b'\n')
+    f.write(out.getvalue())
+    f.write(b'\n')
 
-for x in sources:
-    if x not in history and not x.endswith('.o'):
-        print 'Not processed:', x
+for src in sources:
+    if src not in history and not src.endswith('.o'):
+        print('Not processed:', src)
 
 
diff --git a/amalgamation/python/mxnet_predict.py b/amalgamation/python/mxnet_predict.py
index 3dd6b38793..627f375e14 100644
--- a/amalgamation/python/mxnet_predict.py
+++ b/amalgamation/python/mxnet_predict.py
@@ -35,14 +35,19 @@
 else:
     py_str = lambda x: x
 
+
 def c_str(string):
     """"Convert a python string to C string."""
+    if not isinstance(string, str):
+        string = string.decode('ascii')
     return ctypes.c_char_p(string.encode('utf-8'))
 
+
 def c_array(ctype, values):
     """Create ctypes array from a python array."""
     return (ctype * len(values))(*values)
 
+
 def _find_lib_path():
     """Find mxnet library."""
     curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__)))


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services