[pypy-commit] pypy default: add an option to not write C files, but only simulate writing them. Useful for benchmarking without actually write to the disk

antocuni noreply at buildbot.pypy.org
Mon Jul 18 11:31:04 CEST 2011


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: 
Changeset: r45709:90fbcd637136
Date: 2011-07-09 19:10 +0200
http://bitbucket.org/pypy/pypy/changeset/90fbcd637136/

Log:	add an option to not write C files, but only simulate writing them.
	Useful for benchmarking without actually write to the disk

diff --git a/pypy/config/translationoption.py b/pypy/config/translationoption.py
--- a/pypy/config/translationoption.py
+++ b/pypy/config/translationoption.py
@@ -140,7 +140,10 @@
                  ["annotate", "rtype", "backendopt", "database", "source",
                   "pyjitpl"],
                  default=None, cmdline="--fork-before"),
-
+    BoolOption("dont_write_c_files",
+               "Make the C backend write everyting to /dev/null. " +
+               "Useful for benchmarking, so you don't actually involve the disk",
+               default=False, cmdline="--dont-write-c-files"),
     ArbitraryOption("instrumentctl", "internal",
                default=None),
     StrOption("output", "Output file name", cmdline="--output"),
diff --git a/pypy/tool/nullpath.py b/pypy/tool/nullpath.py
new file mode 100644
--- /dev/null
+++ b/pypy/tool/nullpath.py
@@ -0,0 +1,12 @@
+import py
+
+class NullPyPathLocal(py.path.local):
+
+    def join(self, *args):
+        return self.__class__(py.path.local.join(self, *args))
+
+    def open(self, mode):
+        return open('/dev/null', mode)
+
+    def __repr__(self):
+        return py.path.local.__repr__(self) + ' [fake]'
diff --git a/pypy/tool/test/test_nullpath.py b/pypy/tool/test/test_nullpath.py
new file mode 100644
--- /dev/null
+++ b/pypy/tool/test/test_nullpath.py
@@ -0,0 +1,16 @@
+import sys
+import py
+from pypy.tool.nullpath import NullPyPathLocal
+
+def setup_module():
+    if 'posix' not in sys.builtin_module_names:
+        py.test.skip('posix only')
+
+def test_nullpath(tmpdir):
+    path = NullPyPathLocal(tmpdir)
+    assert repr(path).endswith('[fake]')
+    foo_txt = path.join('foo.txt')
+    assert isinstance(foo_txt, NullPyPathLocal)
+    #
+    f = foo_txt.open('w')
+    assert f.name == '/dev/null'
diff --git a/pypy/translator/c/genc.py b/pypy/translator/c/genc.py
--- a/pypy/translator/c/genc.py
+++ b/pypy/translator/c/genc.py
@@ -13,6 +13,7 @@
 from pypy.rpython.typesystem import getfunctionptr
 from pypy.translator.c import gc
 from pypy.rlib import exports
+from pypy.tool.nullpath import NullPyPathLocal
 
 def import_module_from_directory(dir, modname):
     file, pathname, description = imp.find_module(modname, [str(dir)])
@@ -237,7 +238,9 @@
             self.modulename = uniquemodulename('testing')
         modulename = self.modulename
         targetdir = udir.ensure(modulename, dir=1)
-        
+        if self.config.translation.dont_write_c_files:
+            targetdir = NullPyPathLocal(targetdir)
+
         self.targetdir = targetdir
         defines = defines.copy()
         if self.config.translation.countmallocs:
@@ -944,11 +947,13 @@
     ]
     return eci.merge(ExternalCompilationInfo(separate_module_files=files))
 
+
 def gen_source_standalone(database, modulename, targetdir, eci,
                           entrypointname, defines={}): 
     assert database.standalone
     if isinstance(targetdir, str):
         targetdir = py.path.local(targetdir)
+
     filename = targetdir.join(modulename + '.c')
     f = filename.open('w')
     incfilename = targetdir.join('common_header.h')
@@ -1002,6 +1007,7 @@
     assert not database.standalone
     if isinstance(targetdir, str):
         targetdir = py.path.local(targetdir)
+
     filename = targetdir.join(modulename + '.c')
     f = filename.open('w')
     incfilename = targetdir.join('common_header.h')
diff --git a/pypy/translator/c/test/test_genc.py b/pypy/translator/c/test/test_genc.py
--- a/pypy/translator/c/test/test_genc.py
+++ b/pypy/translator/c/test/test_genc.py
@@ -13,6 +13,7 @@
 from pypy.translator.backendopt.all import backend_optimizations
 from pypy.translator.interactive import Translation
 from pypy.rlib.entrypoint import entrypoint
+from pypy.tool.nullpath import NullPyPathLocal
 
 def compile(fn, argtypes, view=False, gcpolicy="ref", backendopt=True,
             annotatorpolicy=None):
@@ -63,6 +64,22 @@
 
     py.test.raises(Exception, f1, "world")  # check that it's really typed
 
+
+def test_dont_write_source_files():
+    def f(x):
+        return x*2
+    t = TranslationContext()
+    t.buildannotator().build_types(f, [int])
+    t.buildrtyper().specialize()
+
+    t.config.translation.countmallocs = True
+    t.config.translation.dont_write_c_files = True
+    builder = genc.CExtModuleBuilder(t, f, config=t.config)
+    builder.generate_source()
+    assert isinstance(builder.targetdir, NullPyPathLocal)
+    assert builder.targetdir.listdir() == []
+
+
 def test_simple_lambda():
     f = lambda x: x*2
     t = TranslationContext()


More information about the pypy-commit mailing list