Commits

Alexander Hesse  committed 1ed63b0

pypy.tool.cache -> rpython.rlib.cache

  • Participants
  • Parent commits ec6e57a
  • Branches split-rpython

Comments (0)

Files changed (6)

File pypy/interpreter/baseobjspace.py

     new_exception_class, typed_unwrap_error_msg)
 from pypy.interpreter.argument import Arguments
 from pypy.interpreter.miscutils import ThreadLocals
-from pypy.tool.cache import Cache
+from rpython.rlib.cache import Cache
 from rpython.tool.uid import HUGEVAL_BYTES
 from rpython.rlib import jit
 from rpython.rlib.debug import make_sure_not_resized

File pypy/tool/cache.py

-"""
-Caches that can freeze when the annotator needs it.
-"""
-
-#
-# _freeze_() protocol:
-#     user-defined classes can define a method _freeze_(), which
-#     is called when a prebuilt instance is found.  If the method
-#     returns True, the instance is considered immutable and becomes
-#     a SomePBC().  Otherwise it's just SomeInstance().  The method
-#     should force away any laziness that remains in the instance.
-#
-# Cache class:
-#     a cache meant to map a finite number of keys to values.
-#     It is normally extended lazily, until it contains all possible
-#     keys.  The _annspecialcase_ attribute of the getorbuild() method
-#     forces the annotator to decode the argument's annotations,
-#     which must be constants or SomePBCs, actually call the
-#     method with all possible combinations, and gather the results.
-#     The idea is to completely fill the cache at annotation-time,
-#     using the information collected by the annotator itself about
-#     what the keys can actually be.
-#
-#     Cache must be subclassed, and a _build() method provided.
-#     Be sure to call the parent __init__() if you override it.
-#
-
-try:
-    from threading import RLock
-    lock = RLock()     # multithreading protection
-except ImportError:
-    lock = None
-
-
-class Cache(object):
-    def __init__(self):
-        self.content = {}
-        self._building = {}
-
-    def getorbuild(self, key):
-        if lock: lock.acquire()
-        try:
-            try:
-                return self.content[key]
-            except KeyError:
-                if key in self._building:
-                    raise Exception, "%s recursive building of %r" % (
-                        self, key)
-                self._building[key] = True
-                try:
-                    result = self._build(key)
-                    self.content[key] = result
-                finally:
-                    del self._building[key]
-                self._ready(result)
-                return result
-        finally:
-            if lock: lock.release()
-    getorbuild._annspecialcase_ = "specialize:memo"
-
-    def __repr__(self):
-        return "<Cache %r (%i items)>" % (self.__class__.__name__, len(self.content))
-
-    def _ready(self, result):
-        pass
-
-    def _freeze_(self):
-        # needs to be SomePBC, but otherwise we can't really freeze the
-        # cache because more getorbuild() calls might be discovered later
-        # during annotation.
-        return True

File pypy/tool/test/test_cache.py

-from pypy.tool.cache import Cache 
+from rpython.rlib.cache import Cache 
 
 class MyCache(Cache):
     counter = 0

File rpython/annotator/test/test_annrpython.py

         assert not s.is_constant()
 
     def test_getorbuild_as_attr(self):
-        from pypy.tool.cache import Cache
+        from rpython.rlib.cache import Cache
         class SpaceCache(Cache):
             def _build(self, callable):
                 return callable()

File rpython/rlib/cache.py

+"""
+Caches that can freeze when the annotator needs it.
+"""
+
+#
+# _freeze_() protocol:
+#     user-defined classes can define a method _freeze_(), which
+#     is called when a prebuilt instance is found.  If the method
+#     returns True, the instance is considered immutable and becomes
+#     a SomePBC().  Otherwise it's just SomeInstance().  The method
+#     should force away any laziness that remains in the instance.
+#
+# Cache class:
+#     a cache meant to map a finite number of keys to values.
+#     It is normally extended lazily, until it contains all possible
+#     keys.  The _annspecialcase_ attribute of the getorbuild() method
+#     forces the annotator to decode the argument's annotations,
+#     which must be constants or SomePBCs, actually call the
+#     method with all possible combinations, and gather the results.
+#     The idea is to completely fill the cache at annotation-time,
+#     using the information collected by the annotator itself about
+#     what the keys can actually be.
+#
+#     Cache must be subclassed, and a _build() method provided.
+#     Be sure to call the parent __init__() if you override it.
+#
+
+try:
+    from threading import RLock
+    lock = RLock()     # multithreading protection
+except ImportError:
+    lock = None
+
+
+class Cache(object):
+    def __init__(self):
+        self.content = {}
+        self._building = {}
+
+    def getorbuild(self, key):
+        if lock: lock.acquire()
+        try:
+            try:
+                return self.content[key]
+            except KeyError:
+                if key in self._building:
+                    raise Exception, "%s recursive building of %r" % (
+                        self, key)
+                self._building[key] = True
+                try:
+                    result = self._build(key)
+                    self.content[key] = result
+                finally:
+                    del self._building[key]
+                self._ready(result)
+                return result
+        finally:
+            if lock: lock.release()
+    getorbuild._annspecialcase_ = "specialize:memo"
+
+    def __repr__(self):
+        return "<Cache %r (%i items)>" % (self.__class__.__name__, len(self.content))
+
+    def _ready(self, result):
+        pass
+
+    def _freeze_(self):
+        # needs to be SomePBC, but otherwise we can't really freeze the
+        # cache because more getorbuild() calls might be discovered later
+        # during annotation.
+        return True

File rpython/rtyper/test/test_rpbc.py

 
         # this test checks that we add a separate field 
         # per specialization and also it uses a subclass of 
-        # the standard pypy.tool.cache.Cache
+        # the standard rpython.rlib.cache.Cache
 
-        from pypy.tool.cache import Cache
+        from rpython.rlib.cache import Cache
         fr1 = Freezing()
         fr2 = Freezing()