[pypy-svn] r10977 - in pypy/dist/pypy: . interpreter tool translator translator/test

tismer at codespeak.net tismer at codespeak.net
Thu Apr 21 18:39:58 CEST 2005


Author: tismer
Date: Thu Apr 21 17:08:04 2005
New Revision: 10977

Added:
   pypy/dist/pypy/translator/test/test_rpystone.py
Modified:
   pypy/dist/pypy/   (props changed)
   pypy/dist/pypy/interpreter/gateway.py
   pypy/dist/pypy/tool/sourcetools.py
   pypy/dist/pypy/translator/geninterplevel.py
   pypy/dist/pypy/translator/test/rpystone.py
Log:
added test_rpystone (forgotton)
added _cache to the svn ignore list
wrote an initial folder based geninterp cache.
Todo: find the names of the source modules.

Modified: pypy/dist/pypy/interpreter/gateway.py
==============================================================================
--- pypy/dist/pypy/interpreter/gateway.py	(original)
+++ pypy/dist/pypy/interpreter/gateway.py	Thu Apr 21 17:08:04 2005
@@ -7,7 +7,7 @@
 
 """
 
-import types, sys
+import types, sys, md5
 
 from pypy.tool import hack
 from pypy.interpreter.error import OperationError 
@@ -536,7 +536,7 @@
     def _freeze_(self):
         return True  # hint for the annotator: applevel instances are constants
 
-class ApplevelInterpClass(ApplevelClass):
+class old_ApplevelInterpClass(ApplevelClass):
     """ similar to applevel, but using translation to interp-level.
     """
     NOT_RPYTHON_ATTRIBUTES = []
@@ -620,6 +620,119 @@
         cls._setup_done = True
     _setup = classmethod(_setup)
 
+class ApplevelInterpClass(ApplevelClass):
+    """ similar to applevel, but using translation to interp-level.
+        This version maintains a cache folder with single files.
+    """
+    NOT_RPYTHON_ATTRIBUTES = []
+
+    def __init__(self, source, filename = None, modname = 'applevelinterp', do_imports=False):
+        "NOT_RPYTHON"
+        self.filename = filename
+        self.source = source
+        self.modname = modname
+        self.do_imports = do_imports
+
+    def _builddict(self, space):
+        "NOT_RPYTHON"
+        if not self._setup_done:
+            self._setup()
+        from pypy.translator.geninterplevel import translate_as_module
+        scramble = md5.new(self.seed)
+        scramble.update(self.source)
+        key = scramble.digest()
+        initfunc = self.known_source.get(key)
+        if not initfunc:
+            # try to get it from file
+            name = scramble.hexdigest()
+            try:
+                __import__("pypy._cache."+name)
+            except ImportError, x:
+                # print x
+                pass
+            else:
+                initfunc = self.known_source[key]
+        if not initfunc:
+            # build it and put it into a file
+            initfunc, newsrc = translate_as_module(
+                self.source, self.filename, self.modname, self.do_imports)
+            name = scramble.hexdigest()
+            fname = self.cache_path.join(name+".py").strpath
+            f = file(fname, "w")
+            print >> f, """\
+# self-destruct on double-click:
+if __name__ == "__main__":
+    from pypy import _cache
+    import os
+    namestart = os.path.join(os.path.split(_cache.__file__)[0], '%s')
+    for ending in ('.py', '.pyc', '.pyo'):
+        try:
+            os.unlink(namestart+ending)
+        except os.error:
+            pass""" % name
+            print >> f
+            print >> f, newsrc
+            print >> f, "from pypy._cache import known_source"
+            print >> f, "known_source[%r] = %s" % (key, initfunc.__name__)
+        w_glob = initfunc(space)
+        return w_glob
+
+    _setup_done = False
+    
+    def _setup(cls):
+        """NOT_RPYTHON"""
+        from pypy.tool.getpy import py
+        lp = py.path.local
+        import pypy, os
+        p = lp(pypy.__file__).new(basename='_cache').ensure(dir=1)
+        cls.cache_path = p
+        ini = p.join('__init__.py')
+        try:
+            if not ini.check():
+                raise ImportError  # don't import if only a .pyc file left!!!
+            from pypy._cache import known_source, \
+                 GI_VERSION_RENDERED
+        except ImportError:
+            GI_VERSION_RENDERED = 0
+        from pypy.translator.geninterplevel import GI_VERSION
+        cls.seed = md5.new(str(GI_VERSION)).digest()
+        if GI_VERSION != GI_VERSION_RENDERED or GI_VERSION is None:
+            for pth in p.listdir():
+                try:
+                    pth.remove()
+                except: pass
+            file(str(ini), "w").write("""\
+# This folder acts as a cache for code snippets which have been
+# compiled by compile_as_module().
+# It will get a new entry for every piece of code that has
+# not been seen, yet.
+#
+# Caution! Only the code snippet is checked. If something
+# is imported, changes are not detected. Also, changes
+# to geninterplevel or gateway are also not checked.
+# Exception: There is a checked version number in geninterplevel.py
+#
+# If in doubt, remove this file from time to time.
+
+GI_VERSION_RENDERED = %r
+
+known_source = {}
+
+# self-destruct on double-click:
+if __name__ == "__main__":
+    import pypy._cache as _c
+    from pypy.tool.getpy import py
+    lp = py.path.local
+    for pth in lp(_c.__file__).dirpath().listdir():
+        try:
+            pth.remove()
+        except: pass
+""" % GI_VERSION)
+        import pypy._cache
+        cls.known_source = pypy._cache.known_source
+        cls._setup_done = True
+    _setup = classmethod(_setup)
+
 def applevel(source, filename = None,
                    modname = 'applevelinterp', do_imports=False):
     # look at the first three lines

Modified: pypy/dist/pypy/tool/sourcetools.py
==============================================================================
--- pypy/dist/pypy/tool/sourcetools.py	(original)
+++ pypy/dist/pypy/tool/sourcetools.py	Thu Apr 21 17:08:04 2005
@@ -48,7 +48,7 @@
             # missing source, what to do?
             self.srctext = None
 
-    def __call__(self, src, args):
+    def __call__(self, src, args={}):
         """ instance NiceCompile (src, args) -- formats src with args
             and returns a code object ready for exec. Instead of <string>,
             the code object has correct co_filename and line numbers.
@@ -90,7 +90,7 @@
         return src % name.__sourceargs__
     return src
 
-## the following is stolen frompy.code.source.py for now.
+## the following is stolen from py.code.source.py for now.
 ## XXX discuss whether and how to put this functionality
 ## into py.code.source.
 #

Modified: pypy/dist/pypy/translator/geninterplevel.py
==============================================================================
--- pypy/dist/pypy/translator/geninterplevel.py	(original)
+++ pypy/dist/pypy/translator/geninterplevel.py	Thu Apr 21 17:08:04 2005
@@ -527,11 +527,7 @@
             base_class = None
             base = cls
         def initinstance():
-            try:
-                content = instance.__dict__.items()
-            except:
-                import pdb
-                pdb.set_trace()
+            content = instance.__dict__.items()
             content.sort()
             for key, value in content:
                 if self.should_translate_attr(instance, key):
@@ -1538,17 +1534,18 @@
                    builtins_can_raise_exceptions=True,
                    do_imports_immediately=do_imports)
     hold = sys.path
-    sys.path.insert(0, os.path.join(pypy.__path__[0], "lib"))
+    libdir = os.path.join(pypy.__path__[0], "lib")
     gen = GenRpy(t, entrypoint, modname, dic)
-    sys.path = hold
     if tmpname:
         _file = file
     else:
         _file = memfile
-        tmpname = sourcetext
+        tmpname = 'nada'
     out = _file(tmpname, 'w')
     gen.f = out
+    sys.path.insert(0, libdir)
     gen.gen_source(tmpname, file=_file)
+    sys.path.remove(libdir)
     out.close()
     newsrc = _file(tmpname).read()
     code = py.code.Source(newsrc).compile()

Modified: pypy/dist/pypy/translator/test/rpystone.py
==============================================================================
--- pypy/dist/pypy/translator/test/rpystone.py	(original)
+++ pypy/dist/pypy/translator/test/rpystone.py	Thu Apr 21 17:08:04 2005
@@ -38,7 +38,9 @@
 class G:pass
 g = G()
 
-import sys
+# import sys
+# we cannot import sys here
+# because flow space must produce a late lookup
 
 from time import clock
 
@@ -64,13 +66,15 @@
 FALSE = 0
 
 def main(loops=LOOPS):
-    benchtime, stones = pystones(loops)
+    benchtime, stones = pystones(abs(loops))
     #print "Pystone(%s) time for %d passes = %g" % \
     #      (__version__, loops, benchtime)
-    sys.stdout.write("Pystone(%s) time for %d passes = %g\n" % \
-          (__version__, loops, benchtime) )
-    #print "This machine benchmarks at %g pystones/second" % stones
-    sys.stdout.write("This machine benchmarks at %g pystones/second\n" % stones)
+    if loops >= 0:
+        import sys
+        sys.stdout.write("Pystone(%s) time for %d passes = %g\n" % \
+              (__version__, loops, benchtime) )
+        #print "This machine benchmarks at %g pystones/second" % stones
+        sys.stdout.write("This machine benchmarks at %g pystones/second\n" % stones)
 
 
 def pystones(loops=LOOPS):
@@ -296,6 +300,7 @@
     # for now, we use main in the test.
     
     def error(msg):
+        import sys
         #print >>sys.stderr, msg,
         sys.stderr.write(msg+" ")
         #print >>sys.stderr, "usage: %s [number_of_loops]" % sys.argv[0]

Added: pypy/dist/pypy/translator/test/test_rpystone.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/test/test_rpystone.py	Thu Apr 21 17:08:04 2005
@@ -0,0 +1,22 @@
+from pypy.translator.geninterplevel import translate_as_module, __file__ as __
+from pypy.objspace.std import Space
+import os
+fname = os.path.join(os.path.dirname(__), "test", "rpystone.py")
+src = file(fname).read()
+init, ign = translate_as_module(src)
+
+LOOPS = 25
+
+def test_rpystone():
+    space = Space()
+    modic = init(space)
+    # entry = space.getitem(modic, space.wrap("entrypoint"))
+    # XXX wecan't use stuff like sys.argv, yet
+    entry = space.getitem(modic, space.wrap("main"))
+    # warm-up,to get everything translated
+    space.call(entry, space.newtuple([space.wrap(-1)]))
+    # now this is the real one
+    space.call(entry, space.newtuple([space.wrap(LOOPS)]))
+
+if __name__ == "__main__":
+    test_rpystone()
\ No newline at end of file



More information about the Pypy-commit mailing list