[pypy-svn] rev 2483 - in pypy/trunk/src/pypy/annotation: . test
arigo at codespeak.net
arigo at codespeak.net
Thu Dec 18 12:09:20 CET 2003
Author: arigo
Date: Thu Dec 18 12:09:19 2003
New Revision: 2483
Modified:
pypy/trunk/src/pypy/annotation/annset.py
pypy/trunk/src/pypy/annotation/model.py
pypy/trunk/src/pypy/annotation/test/test_annset.py
Log:
Removed dependencies. They are really getting in the way.
We'll try now to be more goal-oriented and work on translation with just
reflowing things in the blocks.
Modified: pypy/trunk/src/pypy/annotation/annset.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/annset.py (original)
+++ pypy/trunk/src/pypy/annotation/annset.py Thu Dec 18 12:09:19 2003
@@ -132,8 +132,7 @@
self.simplify(kill=annlist)
def simplify(self, kill=[]):
- """Kill annotations in the list, and recursively all the annotations
- that depend on them, and simplify the resulting list to remove
+ """Kill annotations in the 'kill' list, and normalize and remove
duplicates."""
# temporarykey() returns a tuple with all the information about
# the annotation; equal temporarykey() means equal annotations.
@@ -148,32 +147,16 @@
allkeys = {} # map temporarykeys to Annotation instances
for ann in self.annlist:
key = temporarykey(ann)
- if key in allkeys: # duplicate?
- previous = allkeys[key]
- previous.forward_deps += ann.forward_deps # merge
- else:
+ if key not in allkeys: # if not duplicate
allkeys[key] = ann
- killkeys = {} # set of temporarykeys of annotations to remove
for ann in kill:
- killkeys[temporarykey(ann)] = True
-
- pending = killkeys.keys()
- for key in pending:
+ key = temporarykey(ann)
if key in allkeys:
- ann = allkeys[key]
- del allkeys[key] # remove annotations from the dict
- for dep in ann.forward_deps: # propagate dependencies
- depkey = temporarykey(dep)
- if depkey not in killkeys:
- killkeys[depkey] = True
- pending.append(depkey)
+ del allkeys[key]
self.annlist = allkeys.values()
- def adddependency(self, hypothesisann, conclusionann):
- hypothesisann.forward_deps.append(conclusionann)
-
def merge(self, oldcell, newcell):
"""Update the heap to account for the merging of oldcell and newcell.
Return the merged cell."""
@@ -237,32 +220,8 @@
for oldann, newann in common:
resultann = newann.copy(renameargs={newcell: resultcell})
annlist.append(resultann)
- self.adddependency(oldann, resultann)
- self.adddependency(newann, resultann)
return resultcell
-
-class Recorder:
- """A recorder contains methods to look for annotations in the
- AnnotationSet and create new annotations accordingly. Each
- Recorder instance records which Annotations were needed, which
- allows dependencies to be tracked."""
-
- def __init__(self, annset):
- self.annset = annset
- self.using_annotations = [] # annotations that we have used
-
- def using(self, *annlist):
- """Mark all 'ann' in 'annlist' as used in this transaction."""
- self.using_annotations += annlist
-
- def query(self, *querylist):
- results = []
- for matchanns, matchvalue in self.annset.match(*querylist):
- self.using(*matchanns)
- results.append(matchvalue)
- return results
-
def get(self, *querylist):
"""Like query() but asserts that there is at most one answer.
Returns None if there isn't any answer."""
@@ -274,17 +233,14 @@
return None
def set(self, ann):
- """Insert the annotation into the AnnotationSet, recording dependency
- from all previous queries done on this Recorder instance."""
- self.annset.normalizeann(ann)
- self.annset.annlist.append(ann)
- for previous_ann in self.using_annotations:
- self.annset.adddependency(previous_ann, ann)
+ """Insert the annotation into the AnnotationSet."""
+ self.normalizeann(ann)
+ self.annlist.append(ann)
def delete(self, queryann):
"""Kill the annotations matching the pattern."""
- matchannlist = self.annset.findall(queryann)
- self.annset.simplify(kill=matchannlist)
+ matchannlist = self.findall(queryann)
+ self.simplify(kill=matchannlist)
def checktype(self, someval, checktype):
if isinstance(checktype, tuple):
Modified: pypy/trunk/src/pypy/annotation/model.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/model.py (original)
+++ pypy/trunk/src/pypy/annotation/model.py Thu Dec 18 12:09:19 2003
@@ -42,7 +42,6 @@
def __init__(self, predicate, *args):
self.predicate = predicate # the operation or predicate
self.args = list(args) # list of SomeValues
- self.forward_deps = [] # for annset.py
assert len(args) == predicate.arity
# note that for predicates that are simple operations like
# op.add, the result is stored as the last argument.
Modified: pypy/trunk/src/pypy/annotation/test/test_annset.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/test/test_annset.py (original)
+++ pypy/trunk/src/pypy/annotation/test/test_annset.py Thu Dec 18 12:09:19 2003
@@ -9,31 +9,27 @@
c1,c2,c3,c4 = SomeValue(), SomeValue(), SomeValue(), SomeValue()
-def assertSameSet(testcase, annset, annotations):
- for ann in annotations:
- annset.normalizeann(ann)
- a = list(annset)
- b = annotations
- # try to reorder a to match b, without failing if the lists
- # are different -- this will be checked by assertEquals()
- for i in range(len(b)):
- try:
- j = i + a[i:].index(b[i])
- except ValueError:
- pass
- else:
- a[i], a[j] = a[j], a[i]
- testcase.assertEquals(a, b)
-
-def assertSameCells(testcase, annset, *cells):
- cells = [annset.normalized(c) for c in cells]
- for c in cells[1:]:
- testcase.assertEquals(cells[0], c)
-
-
class TestAnnotationSet(test.IntTestCase):
- assertSameSet = assertSameSet
- assertSameCells = assertSameCells
+ def assertSameSet(self, annset, annotations):
+ for ann in annotations:
+ annset.normalizeann(ann)
+ a = list(annset)
+ b = annotations
+ # try to reorder a to match b, without failing if the lists
+ # are different -- this will be checked by assertEquals()
+ for i in range(len(b)):
+ try:
+ j = i + a[i:].index(b[i])
+ except ValueError:
+ pass
+ else:
+ a[i], a[j] = a[j], a[i]
+ self.assertEquals(a, b)
+
+ def assertSameCells(self, annset, *cells):
+ cells = [annset.normalized(c) for c in cells]
+ for c in cells[1:]:
+ self.assertEquals(cells[0], c)
def test_isshared(self):
a = AnnotationSet()
@@ -90,9 +86,7 @@
def test_newconstant(self):
a = AnnotationSet([])
- def f(rec):
- return rec.newconstant(42)
- c = a.record(f)
+ c = a.newconstant(42)
self.assertSameSet(a, [ANN.constant(42)[c]])
def test_queryconstant(self):
@@ -145,18 +139,6 @@
a.kill(ann1)
self.assertSameSet(a, lst[1:])
- def test_adddependency(self):
- ann1 = ANN.add[c1, c3, c2]
- ann2 = ANN.add[c1, c2, c2]
- ann3 = ANN.add[c1, c1, c2]
- lst = [ann1, ann2, ann3,
- ANN.neg[c2, c3]]
- a = AnnotationSet(lst)
- a.adddependency(ann1, ann2)
- a.adddependency(ann2, ann3)
- a.kill(ann1)
- self.assertSameSet(a, lst[3:])
-
def test_merge_blackholevalue(self):
lst = [ANN.add[c1, c3, c2],
ANN.neg[c2, c3]]
@@ -280,54 +262,45 @@
## ANN.immutable', [], c)]
## self.assertSameSet(a, lst)
-
-class TestRecording(test.IntTestCase):
- assertSameSet = assertSameSet
- assertSameCells = assertSameCells
-
- def setUp(self):
- self.lst = [
+ def test_set_kill(self):
+ lst = [
ANN.add[c1, c3, c2],
ANN.type[c1, c4],
ANN.constant(int)[c4],
]
- self.annset = AnnotationSet(self.lst)
-
- def test_simple(self):
- a = self.annset
- def f(rec):
- if rec.query(ANN.add[c1, c3, QUERYARG]):
- rec.set(ANN.type[c1, c3])
- a.record(f)
- self.assertSameSet(a, self.lst + [ANN.type[c1, c3]])
+ a = AnnotationSet(lst)
+ a.set(ANN.type[c1, c3])
+ lst += [ANN.type[c1, c3]]
+ self.assertSameSet(a, lst)
- a.kill(self.lst[0])
- self.assertSameSet(a, self.lst[1:])
+ a.kill(lst[0])
+ del lst[0]
+ self.assertSameSet(a, lst)
def test_type(self):
- a = self.annset
- def f(rec):
- if rec.checktype(c1, int):
- rec.settype(c2, str)
- a.record(f)
- self.assert_(a.query(ANN.type[c2, QUERYARG],
- ANN.constant(str)[QUERYARG]))
-
- def test_type2(self):
- a = self.annset
- def f(rec):
- if rec.checktype(c1, (int, long)):
- rec.settype(c2, str)
- a.record(f)
+ lst = [
+ ANN.add[c1, c3, c2],
+ ANN.type[c1, c4],
+ ANN.constant(int)[c4],
+ ]
+ a = AnnotationSet(lst)
+ self.assert_(a.checktype(c1, int))
+ self.assert_(a.checktype(c1, (int, long)))
+ self.failIf(a.checktype(c1, str))
+ a.settype(c2, str)
self.assert_(a.query(ANN.type[c2, QUERYARG],
ANN.constant(str)[QUERYARG]))
def test_delete(self):
- a = self.annset
- def f(rec):
- rec.delete(ANN.add[c1, c3, ...])
- a.record(f)
- self.assertSameSet(a, self.lst[1:])
+ lst = [
+ ANN.add[c1, c3, c2],
+ ANN.type[c1, c4],
+ ANN.constant(int)[c4],
+ ]
+ a = AnnotationSet(lst)
+ a.delete(ANN.add[c1, c3, ...])
+ self.assertSameSet(a, lst[1:])
+
if __name__ == '__main__':
test.main()
More information about the Pypy-commit
mailing list