From 12bb2bb277734adee28090ae26dbe0c9ee199c29 Mon Sep 17 00:00:00 2001
From: John Sirois <john.sirois@gmail.com>
Date: Tue, 2 Oct 2018 15:45:53 -0400
Subject: [PATCH] Fix #511 to fix #523 test on 3.7 machine with access to 2.7
 bad interp.

---
 pex/interpreter.py |  6 ++++--
 pex/resolver.py    | 44 +++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/pex/interpreter.py b/pex/interpreter.py
index 6da98e8bc..4b3226b57 100644
--- a/pex/interpreter.py
+++ b/pex/interpreter.py
@@ -261,8 +261,10 @@ def __str__(self):
     )
 
   def __repr__(self):
-    return 'PythonIdentity(%r, %s, %s, %s)' % (
-      self._interpreter,
+    return 'PythonIdentity(%r, %r, %r, %r, %r, %r)' % (
+      self.abbr_impl,
+      self.abi_tag,
+      self.impl_ver,
       self._version[0],
       self._version[1],
       self._version[2]
diff --git a/pex/resolver.py b/pex/resolver.py
index 03c9ad868..c2ac8aee4 100644
--- a/pex/resolver.py
+++ b/pex/resolver.py
@@ -164,10 +164,52 @@ class Resolver(object):
 
   class Error(Exception): pass
 
+  @staticmethod
+  def _expand_and_maybe_adjust_platform(interpreter, platform=None):
+    # Adjusts `platform` if it is 'current' and does not match the given `interpreter` platform.
+    cur_plat = Platform.current()
+
+    given_platform = Platform.create(platform or 'current')
+    if cur_plat.platform != given_platform.platform:
+      # IE: Say we're on OSX and platform was 'linux-x86_64' or 'linux_x86_64-cp-27-cp27mu'.
+      return given_platform
+
+    ii = interpreter.identity
+    if (ii.abbr_impl, ii.impl_ver, ii.abi_tag) == (cur_plat.impl, cur_plat.version, cur_plat.abi):
+      # IE: Say we're on Linux and platform was 'current' or 'linux-x86_64' or
+      # 'linux_x86_64-cp-27-cp27mu'and the current extended platform info matches the given
+      # interpreter exactly.
+      return cur_plat
+
+    # Otherwise we need to adjust the platform to match a local interpreter different from the
+    # currently executing interpreter.
+    adjusted_platform = Platform(platform=cur_plat.platform,
+                                 impl=ii.abbr_impl,
+                                 version=ii.impl_ver,
+                                 abi=ii.abi_tag)
+
+    TRACER.log("""
+  Modifying given platform of {given_platform!r}:
+  Using the current platform of {current_platform!r}
+  Under current interpreter {current_interpreter!r}
+
+  To match given interpreter {given_interpreter!r}.
+
+  Calculated platform: {calculated_platform!r}""".format(
+      given_platform=given_platform,
+      current_platform=cur_plat,
+      current_interpreter=PythonInterpreter.get(),
+      given_interpreter=interpreter,
+      calculated_platform=adjusted_platform),
+      V=9
+    )
+
+    return adjusted_platform
+
   def __init__(self, allow_prereleases=None, interpreter=None, platform=None,
                pkg_blacklist=None, use_manylinux=None):
     self._interpreter = interpreter or PythonInterpreter.get()
-    self._platform = Platform.create(platform) if platform else Platform.current()
+    self._platform = self._expand_and_maybe_adjust_platform(self._interpreter, platform)
     self._allow_prereleases = allow_prereleases
     self._blacklist = pkg_blacklist.copy() if pkg_blacklist else {}
     self._supported_tags = self._platform.supported_tags(