[pypy-commit] pypy expressions-2: Clean up V_Type code, add tests

rlamy noreply at buildbot.pypy.org
Mon Nov 17 19:59:22 CET 2014


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: expressions-2
Changeset: r74562:45cea07d4528
Date: 2014-11-12 00:32 +0000
http://bitbucket.org/pypy/pypy/changeset/45cea07d4528/

Log:	Clean up V_Type code, add tests

diff --git a/rpython/flowspace/expression.py b/rpython/flowspace/expression.py
--- a/rpython/flowspace/expression.py
+++ b/rpython/flowspace/expression.py
@@ -1,9 +1,8 @@
 from rpython.flowspace.model import Variable
-from rpython.flowspace.operation import op
-from rpython.annotator.model import SomeType
 
 class V_Type(Variable):
     def __init__(self, v_obj):
+        from rpython.annotator.model import SomeType
         Variable.__init__(self)
         self.arg = v_obj
         s = SomeType()
@@ -11,11 +10,18 @@
         self.annotation = s
 
     def as_operation(self):
+        from rpython.flowspace.operation import op
         return op.type(self.arg)
 
     def __eq__(self, other):
         return isinstance(other, V_Type) and other.arg == self.arg
 
+    def __hash__(self):
+        return hash((type(self), self.arg))
+
+    def __repr__(self):
+        return 'type(%s)' % self.arg
+
     def replace(self, mapping):
         if self.arg in mapping:
             return V_Type(mapping[self.arg])
diff --git a/rpython/flowspace/generator.py b/rpython/flowspace/generator.py
--- a/rpython/flowspace/generator.py
+++ b/rpython/flowspace/generator.py
@@ -5,6 +5,7 @@
 from rpython.flowspace.pygraph import PyGraph
 from rpython.flowspace.model import (Block, Link, Variable,
     Constant, checkgraph, const)
+from rpython.flowspace.expression import V_Type
 from rpython.flowspace.operation import op
 from rpython.translator.unsimplify import insert_empty_startblock, split_block
 from rpython.translator.simplify import eliminate_empty_blocks, simplify_graph
@@ -106,7 +107,6 @@
 def tweak_generator_body_graph(Entry, graph):
     # First, always run simplify_graph in order to reduce the number of
     # variables passed around
-    from rpython.flowspace.expression import V_Type
     simplify_graph(graph)
     insert_empty_startblock(None, graph)
     _insert_reads(graph.startblock, Entry.varnames)
diff --git a/rpython/flowspace/operation.py b/rpython/flowspace/operation.py
--- a/rpython/flowspace/operation.py
+++ b/rpython/flowspace/operation.py
@@ -12,6 +12,7 @@
 from rpython.tool.sourcetools import compile2
 from rpython.flowspace.model import (Constant, WrapException, const, Variable,
                                      SpaceOperation)
+from rpython.flowspace.expression import V_Type
 from rpython.flowspace.specialcase import register_flow_sc
 from rpython.annotator.model import (
     SomeTuple, AnnotatorError, read_can_only_throw)
@@ -453,7 +454,6 @@
         if result is not None:
             return result
         ctx.merge_point()
-        from rpython.flowspace.expression import V_Type
         v_instance, = self.args
         return V_Type(v_instance)
 
diff --git a/rpython/flowspace/test/test_expression.py b/rpython/flowspace/test/test_expression.py
new file mode 100644
--- /dev/null
+++ b/rpython/flowspace/test/test_expression.py
@@ -0,0 +1,15 @@
+from rpython.flowspace.model import Variable, const
+from rpython.flowspace.expression import V_Type
+
+def test_type():
+    v1, v2 = Variable(), Variable()
+    assert V_Type(v1) == V_Type(v1)
+    assert V_Type(v1) != V_Type(v2)
+    assert hash(V_Type(v1)) == hash(V_Type(v1))
+    assert hash(V_Type(v1)) != hash(V_Type(v2))
+
+def test_type_replace():
+    v1, v2 = Variable(), Variable()
+    assert V_Type(v1).replace({v1: v2}) == V_Type(v2)
+    assert V_Type(const(1)).replace({v1: v2}) == V_Type(const(1))
+    assert V_Type(v1).replace({v2: v1}) == V_Type(v1)


More information about the pypy-commit mailing list