[pypy-svn] r43763 - in pypy/branch/kill-ctypes/pypy/rpython/lltypesystem: . test

fijal at codespeak.net fijal at codespeak.net
Mon May 28 00:20:23 CEST 2007


Author: fijal
Date: Mon May 28 00:20:22 2007
New Revision: 43763

Modified:
   pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/rfficache.py
   pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/test/test_rfficache.py
Log:
Add caching mechanism


Modified: pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/rfficache.py
==============================================================================
--- pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/rfficache.py	(original)
+++ pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/rfficache.py	Mon May 28 00:20:22 2007
@@ -8,7 +8,7 @@
 from subprocess import PIPE, Popen
 from pypy.tool.udir import udir
 
-def sizeof_c_type(c_typename, includes={}):
+def sizeof_c_type(c_typename, includes={}, compiler_exe=None):
     includes['stdio.h'] = True
     include_string = "\n".join(["#include <%s>" % i for i in includes.keys()])
     c_source = py.code.Source('''
@@ -25,7 +25,39 @@
     c_file = udir.join("typetest.c")
     c_file.write(c_source)
 
-    c_exec = build_executable([str(c_file)])
+    c_exec = build_executable([str(c_file)], compiler_exe=compiler_exe)
     pipe = Popen(c_exec, stdout=PIPE)
     pipe.wait()
     return int(pipe.stdout.read()) * 8
+
+def machine_key():
+    """ Key unique to machine type, but general enough to share
+    it between eg different kernels
+    """
+    import platform
+    return platform.processor(), platform.architecture(), platform.system()
+
+TYPES = []
+for _name in 'char short int long'.split():
+    for name in (_name, 'unsigned ' + _name):
+        TYPES.append(name)
+TYPES.append('long long')
+TYPES.append('unsigned long long')
+TYPES.append('size_t')
+
+def get_type_sizes(filename, platform_key=machine_key(), types=TYPES,
+                   compiler_exe=None):
+    try:
+        mod = py.path.local(filename).pyimport()
+        platforms = mod.platforms
+    except (ImportError, py.error.ENOENT):
+        platforms = {}
+    try:
+        return platforms[platform_key]
+    except KeyError:
+        value = dict([(i, sizeof_c_type(i, compiler_exe=compiler_exe))
+                      for i in types])
+        platforms[platform_key] = value
+        py.path.local(filename).write('platforms = ' + repr(platforms))
+        return value
+

Modified: pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/test/test_rfficache.py
==============================================================================
--- pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/test/test_rfficache.py	(original)
+++ pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/test/test_rfficache.py	Mon May 28 00:20:22 2007
@@ -1,6 +1,21 @@
 
-from pypy.rpython.lltypesystem.rfficache import sizeof_c_type
+from pypy.rpython.lltypesystem.rfficache import *
+from pypy.tool.udir import udir
 
 def test_sizeof_c_type():
     sizeofchar = sizeof_c_type('char')
     assert sizeofchar == 8
+
+def test_gettypesizes():
+    tmpfile = udir.join("somecrappyfile.py")
+    assert get_type_sizes(tmpfile)['char'] == 8
+    # this should not invoke a compiler
+    assert get_type_sizes(tmpfile, compiler_exe='xxx')['char'] == 8
+
+def test_gettypesizes_platforms():
+    tmpfile = udir.join("plat.py")
+    tmpfile.write(py.code.Source("""
+    platforms = {'xxx':{'char':4}}
+    """))
+    assert get_type_sizes(tmpfile)['char'] == 8
+    assert get_type_sizes(tmpfile, platform_key='xxx', compiler_exe='xxx')['char'] == 4



More information about the Pypy-commit mailing list