pypy / pypy / tool /

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.

    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()
                return self.content[key]
            except KeyError:
                if key in self._building:
                    raise Exception, "%s recursive building of %r" % (
                        self, key)
                self._building[key] = True
                    result = self._build(key)
                    self.content[key] = result
                    del self._building[key]
                return result
            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):

    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