Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r78407:45fae072cb84
Date: 2015-07-02 12:34 +0200
http://bitbucket.org/pypy/pypy/changeset/45fae072cb84/

Log:    Tweak 'sys.version' to actually contain the compiler used to compile
        the final C sources, instead of whatever gcc was present when we
        produced the C sources. Useful e.g. with pypy-stm where we use a
        different compiler at the end (clang or gcc-seg-gs).

diff --git a/pypy/module/sys/__init__.py b/pypy/module/sys/__init__.py
--- a/pypy/module/sys/__init__.py
+++ b/pypy/module/sys/__init__.py
@@ -66,7 +66,7 @@
 
         'api_version'           : 'version.get_api_version(space)',
         'version_info'          : 'version.get_version_info(space)',
-        'version'               : 'version.get_version(space)',
+        #'version'              : set in startup()
         'pypy_version_info'     : 'version.get_pypy_version_info(space)',
         'subversion'            : 'version.get_subversion_info(space)',
         '_mercurial'            : 'version.get_repo_info(space)',
@@ -108,6 +108,9 @@
             assert self.filesystemencoding is None
 
         else:
+            from pypy.module.sys import version
+            space.setitem(self.w_dict, space.wrap("version"),
+                          space.wrap(version.get_version(space)))
             if _WIN:
                 from pypy.module.sys import vm
                 w_handle = vm.get_dllhandle(space)
diff --git a/pypy/module/sys/test/test_version.py 
b/pypy/module/sys/test/test_version.py
--- a/pypy/module/sys/test/test_version.py
+++ b/pypy/module/sys/test/test_version.py
@@ -2,13 +2,13 @@
     def test_compiler(self):
         import sys
         assert ("MSC v." in sys.version or
-                "GCC " in sys.version)
+                "GCC " in sys.version or
+                "(untranslated)" in sys.version)
 
-def test_get_version(space, monkeypatch):
+def test_get_version():
     from pypy.module.sys import version
-    monkeypatch.setattr(version, 'PYPY_VERSION', (2,5,0, "final", 1))
-    res = space.unwrap(version.get_version(space))
+    res = version._make_version_template(PYPY_VERSION=(2,5,0, "final", 1))
     assert "[PyPy 2.5.0" in res
-    monkeypatch.setattr(version, 'PYPY_VERSION', (2,6,3, "alpha", 5))
-    res = space.unwrap(version.get_version(space))
+    res = version._make_version_template(PYPY_VERSION=(2,6,3, "alpha", 5))
     assert "[PyPy 2.6.3-alpha5" in res
+    assert res.endswith(' with %s]')
diff --git a/pypy/module/sys/version.py b/pypy/module/sys/version.py
--- a/pypy/module/sys/version.py
+++ b/pypy/module/sys/version.py
@@ -2,7 +2,7 @@
 Version numbers exposed by PyPy through the 'sys' module.
 """
 import os
-from rpython.translator.platform import platform
+from rpython.rlib import compilerinfo
 from pypy.interpreter import gateway
 
 #XXX # the release serial 42 is not in range(16)
@@ -12,15 +12,6 @@
 
 PYPY_VERSION               = (2, 7, 0, "alpha", 0)    #XXX # sync patchlevel.h
 
-if platform.name == 'msvc':
-    COMPILER_INFO = 'MSC v.%d 32 bit' % (platform.version * 10 + 600)
-elif platform.cc is not None and \
-        os.path.basename(platform.cc).startswith(('gcc', 'clang')):
-    from rpython.rtyper.tool import rffi_platform
-    COMPILER_INFO = 'GCC ' + rffi_platform.getdefinedstring('__VERSION__', '')
-else:
-    COMPILER_INFO = ""
-
 
 import pypy
 pypydir = os.path.dirname(os.path.abspath(pypy.__file__))
@@ -57,19 +48,24 @@
     w_version_info = app.wget(space, "version_info")
     return space.call_function(w_version_info, space.wrap(CPYTHON_VERSION))
 
-def get_version(space):
+def _make_version_template(PYPY_VERSION=PYPY_VERSION):
     ver = "%d.%d.%d" % (PYPY_VERSION[0], PYPY_VERSION[1], PYPY_VERSION[2])
     if PYPY_VERSION[3] != "final":
         ver = ver + "-%s%d" %(PYPY_VERSION[3], PYPY_VERSION[4])
-    return space.wrap("%d.%d.%d (%s, %s, %s)\n[PyPy %s%s]" % (
+    template = "%d.%d.%d (%s, %s, %s)\n[PyPy %s with %%s]" % (
         CPYTHON_VERSION[0],
         CPYTHON_VERSION[1],
         CPYTHON_VERSION[2],
         get_repo_version_info(root=pypyroot)[1],
         date,
         time,
-        ver,
-        compiler_version()))
+        ver)
+    assert template.count('%') == 1     # only for the "%s" near the end
+    return template
+_VERSION_TEMPLATE = _make_version_template()
+
+def get_version(space):
+    return space.wrap(_VERSION_TEMPLATE % compilerinfo.get_compiler_info())
 
 def get_winver(space):
     return space.wrap("%d.%d" % (
@@ -111,8 +107,3 @@
             ver[2] << 8    |
             d[ver[3]] << 4 |
             subver)
-
-def compiler_version():
-    if not COMPILER_INFO:
-        return ""
-    return " with %s" % (COMPILER_INFO,)
diff --git a/rpython/rlib/compilerinfo.py b/rpython/rlib/compilerinfo.py
new file mode 100644
--- /dev/null
+++ b/rpython/rlib/compilerinfo.py
@@ -0,0 +1,26 @@
+from rpython.rlib.objectmodel import we_are_translated
+from rpython.rtyper.lltypesystem import rffi
+from rpython.translator.platform import platform
+
+
+def get_compiler_info():
+    """Returns a string like 'MSC v.# 32 bit' or 'GCC #.#.#'.
+    Before translation, returns '(untranslated)'.
+
+    Must be called at run-time, not before translation, otherwise
+    you're freezing the string '(untranslated)' into the executable!
+    """
+    if we_are_translated():
+        return rffi.charp2str(COMPILER_INFO)
+    return "(untranslated)"
+
+# ____________________________________________________________
+
+
+if platform.name == 'msvc':
+    # XXX hard-code the MSC version, I don't feel like computing it dynamically
+    _C_COMPILER_INFO = '"MSC v.%d 32 bit"' % (platform.version * 10 + 600)
+else:
+    _C_COMPILER_INFO = '("GCC " __VERSION__)'
+
+COMPILER_INFO = rffi.CConstant(_C_COMPILER_INFO, rffi.CCHARP)
diff --git a/rpython/rlib/test/test_compilerinfo.py 
b/rpython/rlib/test/test_compilerinfo.py
new file mode 100644
--- /dev/null
+++ b/rpython/rlib/test/test_compilerinfo.py
@@ -0,0 +1,26 @@
+from rpython.rlib.compilerinfo import get_compiler_info
+from rpython.translator.c.test.test_genc import compile
+
+
+def test_untranslated():
+    assert get_compiler_info() == "untranslated"
+
+def fn(index):
+    cc = get_compiler_info()
+    if index < len(cc):
+        return ord(cc[index])
+    return 0
+
+def test_compiled():
+    fn2 = compile(fn, [int])
+    lst = []
+    index = 0
+    while True:
+        c = fn2(index)
+        if c == 0:
+            break
+        lst.append(chr(c))
+        index += 1
+    s = ''.join(lst)
+    print s
+    assert s.startswith('MSC ') or s.startswith('GCC ')
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to