From 06c857cb86586755ab5f01f1a8fecc614f23dffa Mon Sep 17 00:00:00 2001
From: Masood Malekghassemi <atash@google.com>
Date: Mon, 13 Jun 2016 20:53:02 -0700
Subject: [PATCH] Patch monkeypatch link function to work in Python3

The modified link command was originally taken from a Python 2.x distutils.
---
 setup.py                                             |  4 ++--
 .../grpcio/{build.py => _unixccompiler_patch.py}     | 12 ++++++++----
 2 files changed, 10 insertions(+), 6 deletions(-)
 rename src/python/grpcio/{build.py => _unixccompiler_patch.py} (91%)

diff --git a/setup.py b/setup.py
index c6d3da7299..700515b894 100644
--- a/setup.py
+++ b/setup.py
@@ -57,13 +57,13 @@ os.chdir(os.path.dirname(os.path.abspath(__file__)))
 sys.path.insert(0, os.path.abspath(PYTHON_STEM))
 
 # Break import-style to ensure we can actually find our in-repo dependencies.
-import build
+import _unixccompiler_patch
 import commands
 import grpc_core_dependencies
 import grpc_version
 
 # TODO(atash) make this conditional on being on a mingw32 build
-build.monkeypatch_unix_compiler()
+_unixccompiler_patch.monkeypatch_unix_compiler()
 
 
 LICENSE = '3-clause BSD'
diff --git a/src/python/grpcio/build.py b/src/python/grpcio/_unixccompiler_patch.py
similarity index 91%
rename from src/python/grpcio/build.py
rename to src/python/grpcio/_unixccompiler_patch.py
index df5b54cf69..9a697989b3 100644
--- a/src/python/grpcio/build.py
+++ b/src/python/grpcio/_unixccompiler_patch.py
@@ -61,8 +61,9 @@ def _unix_piecemeal_link(
                           if not dir in ('/lib', '/lib64', '/usr/lib', '/usr/lib64')]
   lib_opts = ccompiler.gen_lib_options(self, library_dirs, runtime_library_dirs,
                              libraries)
-  if not isinstance(output_dir, basestring) and output_dir is not None:
-    raise TypeError, "'output_dir' must be a string or None"
+  if (not (isinstance(output_dir, str) or isinstance(output_dir, bytes))
+      and output_dir is not None):
+    raise TypeError("'output_dir' must be a string or None")
   if output_dir is not None:
     output_filename = os.path.join(output_dir, output_filename)
 
@@ -106,11 +107,14 @@ def _unix_piecemeal_link(
         escaped_ld_args = [arg.replace('\\', '\\\\') for arg in ld_args]
         command_file.write(' '.join(escaped_ld_args))
       self.spawn(linker + ['@{}'.format(command_filename)])
-    except errors.DistutilsExecError, msg:
-      raise ccompiler.LinkError, msg
+    except errors.DistutilsExecError:
+      raise ccompiler.LinkError
   else:
     log.debug("skipping %s (up-to-date)", output_filename)
 
+# TODO(atash) try replacing this monkeypatch of the compiler harness' link
+# operation with a monkeypatch of the distutils `spawn` that applies
+# command-argument-file hacks where it can. Might be cleaner.
 def monkeypatch_unix_compiler():
   """Monkeypatching is dumb, but it's either that or we become maintainers of
      something much, much bigger."""
-- 
GitLab