[pypy-svn] r75196 - in pypy/trunk/lib-python/modified-2.5.2/distutils: . tests

afa at codespeak.net afa at codespeak.net
Tue Jun 8 16:31:44 CEST 2010


Author: afa
Date: Tue Jun  8 16:31:42 2010
New Revision: 75196

Added:
   pypy/trunk/lib-python/modified-2.5.2/distutils/tests/test_msvccompiler.py   (contents, props changed)
Modified:
   pypy/trunk/lib-python/modified-2.5.2/distutils/msvccompiler.py
Log:
On Windows, correctly retrieve the version of the compiler used to compile the interpreter.

Don't rely on sys.version (pypy does not indicates the compiler version),
directly fetch the manifest embedded in the program.


Modified: pypy/trunk/lib-python/modified-2.5.2/distutils/msvccompiler.py
==============================================================================
--- pypy/trunk/lib-python/modified-2.5.2/distutils/msvccompiler.py	(original)
+++ pypy/trunk/lib-python/modified-2.5.2/distutils/msvccompiler.py	Tue Jun  8 16:31:42 2010
@@ -159,14 +159,60 @@
             s = s.replace(k, v)
         return s
 
+def get_manifests():
+    """Retrieves the manifest(s) embedded in the current executable"""
+    import ctypes.wintypes
+    EnumResourceNames = ctypes.windll.kernel32.EnumResourceNamesA
+    EnumResourceNameCallback = ctypes.WINFUNCTYPE(
+        ctypes.wintypes.BOOL,
+        ctypes.wintypes.HMODULE, ctypes.wintypes.LONG,
+        ctypes.wintypes.LONG, ctypes.wintypes.LONG)
+    FindResource = ctypes.windll.kernel32.FindResourceA
+    LoadResource = ctypes.windll.kernel32.LoadResource
+    FreeResource = ctypes.windll.kernel32.FreeResource
+    SizeofResource = ctypes.windll.kernel32.SizeofResource
+    LockResource = ctypes.windll.kernel32.LockResource
+    UnlockResource = lambda x: None # hehe
+
+    manifests = []
+
+    def callback(hModule, lpType, lpName, lParam):
+        hResource = FindResource(hModule, lpName, lpType)
+        size = SizeofResource(hModule, hResource)
+        hData = LoadResource(hModule, hResource)
+        try:
+            ptr = LockResource(hData)
+            try:
+                manifests.append(ctypes.string_at(ptr, size))
+            finally:
+                UnlockResource(hData)
+        finally:
+            FreeResource(hData)
+        return True
+
+    hModule = None       # main executable
+    RT_MANIFEST = 24     # from WinUser.h
+    EnumResourceNames(hModule, RT_MANIFEST,
+                      EnumResourceNameCallback(callback), None)
+    return manifests
+
 def get_build_version():
     """Return the version of MSVC that was used to build Python.
-
-    For Python 2.3 and up, the version number is included in
-    sys.version.  For earlier versions, assume the compiler is MSVC 6.
     """
+    try:
+        manifests = get_manifests()
+        for manifest in manifests:
+            match = re.search('"Microsoft.VC([0-9]+).CRT"', manifest)
+            if match:
+                return int(match.group(1)) / 10.0
+    except BaseException:
+        pass
+    # No manifest embedded, use default compiler version
     return 9.0
 
+def get_build_architecture():
+    return 'Intel'
+
 def normalize_and_reduce_paths(paths):
     """Return a list of normalized paths with duplicates removed.
 

Added: pypy/trunk/lib-python/modified-2.5.2/distutils/tests/test_msvccompiler.py
==============================================================================
--- (empty file)
+++ pypy/trunk/lib-python/modified-2.5.2/distutils/tests/test_msvccompiler.py	Tue Jun  8 16:31:42 2010
@@ -0,0 +1,18 @@
+import unittest
+from distutils.msvccompiler import get_manifests
+
+class MsvcCompilerTestCase(unittest.TestCase):
+    def test_get_manifests(self):
+        manifests = get_manifests()
+        self.assert_(manifests)
+        for manifest in manifests:
+            if '"Microsoft.VC' in manifest:
+                break
+        else:
+            self.fail("could not find a suitable manifest")
+
+def test_suite():
+    return unittest.makeSuite(MsvcCompilerTestCase)
+
+if __name__ == "__main__":
+    unittest.main(defaultTest="test_suite")



More information about the Pypy-commit mailing list