Repository: enthought/okonomiyaki

Issue:
Fix platform guessing on 64 bits processes on 32 bits kernel
See #31 

Discussion:

Patch:
diff --git a/okonomiyaki/platforms/epd_platform.py b/okonomiyaki/platforms/epd_platform.py
index 441712a..d32d37a 100644
--- a/okonomiyaki/platforms/epd_platform.py
+++ b/okonomiyaki/platforms/epd_platform.py
@@ -172,20 +172,16 @@ def _guess_architecture():
     """
     Returns the architecture of the running python.
     """
-    x86 = "x86"
-    amd64 = "amd64"
-    bits = platform.architecture()[0]
     machine = platform.machine()
-    if machine in ("AMD64", "x86_64"):
-        if bits == "32bit":
-            return x86
-        elif bits == "64bit":
-            return amd64
-    elif machine in ("x86", "i386", "i686") and bits == "32bit":
-        return x86
+
+    if machine in ("AMD64", "x86_64", "x86", "i386", "i686"):
+        if sys.maxsize > 2 ** 32:
+            return "amd64"
+        else:
+            return "x86"
     else:
-        raise OkonomiyakiError("Unknown bits/machine combination {0}/{1}".
-                               format(bits, machine))
+        raise OkonomiyakiError("Unknown machine combination {0!r}".
+                               format(machine))
 
 
 def _guess_epd_platform(arch=None):
diff --git a/okonomiyaki/platforms/platform.py b/okonomiyaki/platforms/platform.py
index bb20a39..f5db84f 100644
--- a/okonomiyaki/platforms/platform.py
+++ b/okonomiyaki/platforms/platform.py
@@ -3,6 +3,8 @@ from __future__ import absolute_import
 import platform
 import sys
 
+from okonomiyaki.platforms import epd_platform
+
 from okonomiyaki.bundled.traitlets import HasTraits, Enum, Instance, Unicode
 from okonomiyaki.platforms.epd_platform import EPDPlatform
 from okonomiyaki.errors import OkonomiyakiError
@@ -200,18 +202,14 @@ def _guess_architecture():
     """
     Returns the architecture of the running python.
     """
-    bits = platform.architecture()[0]
-    machine = platform.machine()
-    if machine in ("AMD64", "x86_64"):
-        if bits == "32bit":
-            return Arch.from_name(X86)
-        elif bits == "64bit":
-            return Arch.from_name(X86_64)
-    elif machine in ("x86", "i386", "i686") and bits == "32bit":
+    epd_platform_arch = epd_platform._guess_architecture()
+    if epd_platform_arch == "x86":
         return Arch.from_name(X86)
+    elif epd_platform_arch == "amd64":
+        return Arch.from_name(X86_64)
     else:
-        raise OkonomiyakiError("Unknown bits/machine combination {0}/{1}".
-                               format(bits, machine))
+        raise OkonomiyakiError("Unknown architecture {0!r}".
+                               format(epd_platform_arch))
 
 
 def _guess_machine():


Test patch:
diff --git a/okonomiyaki/platforms/tests/common.py b/okonomiyaki/platforms/tests/common.py
index b7ff851..8eb942b 100644
--- a/okonomiyaki/platforms/tests/common.py
+++ b/okonomiyaki/platforms/tests/common.py
@@ -63,14 +63,12 @@ mock_osx_10_7 = MultiPatcher([
 # Architecture mocking
 mock_machine = lambda machine: Patcher(mock.patch("platform.machine",
                                                   lambda: machine))
-mock_architecture = lambda arch: Patcher(mock.patch("platform.architecture",
-                                                    lambda: arch))
 
 mock_machine_x86 = Patcher(mock_machine("x86"))
-mock_architecture_32bit = Patcher(mock_architecture(("32bit",)))
+mock_architecture_32bit = Patcher(mock.patch("sys.maxsize", 2**32-1))
 
 mock_machine_x86_64 = Patcher(mock_machine("x86_64"))
-mock_architecture_64bit = Patcher(mock_architecture(("64bit",)))
+mock_architecture_64bit = Patcher(mock.patch("sys.maxsize", 2**64-1))
 
 mock_x86 = MultiPatcher([mock_machine_x86, mock_architecture_32bit])
 mock_x86_64 = MultiPatcher([mock_machine_x86_64, mock_architecture_64bit])
diff --git a/okonomiyaki/platforms/tests/test_epd_platform.py b/okonomiyaki/platforms/tests/test_epd_platform.py
index edb27a8..9cf711f 100644
--- a/okonomiyaki/platforms/tests/test_epd_platform.py
+++ b/okonomiyaki/platforms/tests/test_epd_platform.py
@@ -8,9 +8,12 @@ from okonomiyaki.platforms.epd_platform import (_guess_architecture,
                                                 _guess_epd_platform, applies)
 from okonomiyaki.platforms.legacy import _SUBDIR
 
-from .common import (mock_centos_3_5, mock_centos_5_8, mock_centos_6_3,
-                     mock_darwin, mock_machine_armv71, mock_solaris,
-                     mock_ubuntu_raring, mock_windows, mock_x86, mock_x86_64)
+from .common import (mock_architecture_32bit, mock_architecture_64bit,
+                     mock_centos_3_5, mock_centos_5_8, mock_centos_6_3,
+                     mock_darwin, mock_machine_x86, mock_machine_x86_64,
+                     mock_machine_armv71, mock_solaris,
+                     mock_ubuntu_raring, mock_windows, mock_x86,
+                     mock_x86_64)
 
 
 class TestEPDPlatform(unittest.TestCase):
@@ -94,12 +97,52 @@ class TestGuessEPDPlatform(unittest.TestCase):
 
     @mock_darwin
     def test_guess_darwin_platform(self):
-        epd_platform = _guess_epd_platform("x86")
+        # When
+        with mock_machine_x86:
+            epd_platform = _guess_epd_platform("x86")
+
+        # Then
         self.assertEqual(epd_platform.short, "osx-32")
 
-        epd_platform = _guess_epd_platform("amd64")
+        # When
+        with mock_machine_x86:
+            epd_platform = _guess_epd_platform("amd64")
+
+        # Then
+        self.assertEqual(epd_platform.short, "osx-64")
+
+        # When
+        with mock_machine_x86:
+            with mock_architecture_32bit:
+                epd_platform = _guess_epd_platform()
+
+        # Then
+        self.assertEqual(epd_platform.short, "osx-32")
+
+        # When
+        with mock_machine_x86:
+            with mock_architecture_64bit:
+                epd_platform = _guess_epd_platform()
+
+        # Then
         self.assertEqual(epd_platform.short, "osx-64")
 
+        # When
+        with mock_machine_x86_64:
+            with mock_architecture_64bit:
+                epd_platform = _guess_epd_platform()
+
+        # Then
+        self.assertEqual(epd_platform.short, "osx-64")
+
+        # When
+        with mock_machine_x86_64:
+            with mock_architecture_32bit:
+                epd_platform = _guess_epd_platform()
+
+        # Then
+        self.assertEqual(epd_platform.short, "osx-32")
+
     def test_guess_linux2_platform(self):
         with mock_centos_5_8:
             epd_platform = _guess_epd_platform("x86")
@@ -109,27 +152,27 @@ class TestGuessEPDPlatform(unittest.TestCase):
             self.assertEqual(epd_platform.short, "rh5-64")
 
             with mock.patch("platform.machine", lambda: "x86"):
-                with mock.patch("platform.architecture", lambda: ("32bit",)):
+                with mock_architecture_32bit:
                     epd_platform = _guess_epd_platform()
                     self.assertEqual(epd_platform.short, "rh5-32")
 
             with mock.patch("platform.machine", lambda: "i386"):
-                with mock.patch("platform.architecture", lambda: ("32bit",)):
+                with mock_architecture_32bit:
                     epd_platform = _guess_epd_platform()
                     self.assertEqual(epd_platform.short, "rh5-32")
 
             with mock.patch("platform.machine", lambda: "i686"):
-                with mock.patch("platform.architecture", lambda: ("32bit",)):
+                with mock_architecture_32bit:
                     epd_platform = _guess_epd_platform()
                     self.assertEqual(epd_platform.short, "rh5-32")
 
             with mock.patch("platform.machine", lambda: "x86_64"):
-                with mock.patch("platform.architecture", lambda: ("32bit",)):
+                with mock_architecture_32bit:
                     epd_platform = _guess_epd_platform()
                     self.assertEqual(epd_platform.short, "rh5-32")
 
             with mock.patch("platform.machine", lambda: "x86_64"):
-                with mock.patch("platform.architecture", lambda: ("64bit",)):
+                with mock_architecture_64bit:
                     epd_platform = _guess_epd_platform()
                     self.assertEqual(epd_platform.short, "rh5-64")