aboutsummaryrefslogtreecommitdiff
blob: bfcffea357107f919dc62266665fe606786a0c52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# hacks += 1
class Cache:
    frozen = True 

    def __init__(self):
        self.content = {}
        self.frozen = False

    def __hash__(self):
        if not self.frozen: 
            #raise TypeError, "cannot get hash of un-frozen cache"
            self.freeze()
        return id(self)

    def clear(self):
        if self.frozen:
            raise TypeError, "cannot clear frozen cache"
        self.content.clear()

    def getorbuild(self, key, builder, stuff):
        try:
            return self.content[key]
        except KeyError:
            assert not self.frozen, "cannot build %r, cache already frozen" % key
            result = builder(key, stuff)
            #assert key not in self.content, "things messed up"
            self.content[key] = result
            return result
    # note to annotator: we want loadfromcache() to be 
    # specialized for the different cache types 
    getorbuild._specialize_ = "location"

    def freeze(self):
        del self.frozen