[pypy-dev] Re: [pypy-svn] r17783 - in pypy/dist/pypy/objspace/flow: . test

Ben.Young at risk.sungard.com Ben.Young at risk.sungard.com
Fri Sep 23 12:11:01 CEST 2005


Hi Armin,

Just a quick question. Is mutating a list while iterating over it rpython?

(Also was wondering if you noticed the mail I sent yesterday to pypy-dev?)

Cheers,
Ben

P.S Do you think there is ever going to be a sprint in London sometime as 
I would love to be able to come to one one day! I guess it's down to 
someone to organise a room etc.


pypy-svn-bounces at codespeak.net wrote on 23/09/2005 11:04:05:

> Author: arigo
> Date: Fri Sep 23 12:04:04 2005
> New Revision: 17783
> 
> Modified:
>    pypy/dist/pypy/objspace/flow/model.py
>    pypy/dist/pypy/objspace/flow/test/test_model.py
> Log:
> Further simplification of traverse(), the full flexibility of which
> is never used anywhere and the speed of which is crucial during
> translation.
> 
> 
> Modified: pypy/dist/pypy/objspace/flow/model.py
> 
==============================================================================
> --- pypy/dist/pypy/objspace/flow/model.py   (original)
> +++ pypy/dist/pypy/objspace/flow/model.py   Fri Sep 23 12:04:04 2005
> @@ -344,52 +344,65 @@
>  #_________________________________________________________
>  # a visitor for easy traversal of the above model
> 
> -import inspect   # for getmro
> +##import inspect   # for getmro
> 
> -class traverse:
> +##class traverse:
> 
> -    def __init__(self, visitor, functiongraph):
> -        """ send the visitor over all (reachable) nodes. 
> -            the visitor needs to have either callable attributes 
> 'visit_typename'
> -            or otherwise is callable itself. 
> -        """
> -        self.visitor = visitor
> -        self.visitor_cache = {}
> -        self.seen = {}
> -        self.visit(functiongraph)
> -
> -    def visit(self, node):
> -        if id(node) in self.seen:
> -            return
> +##    def __init__(self, visitor, functiongraph):
> +##        """ send the visitor over all (reachable) nodes. 
> +##            the visitor needs to have either callable attributes 
> 'visit_typename'
> +##            or otherwise is callable itself. 
> +##        """
> +##        self.visitor = visitor
> +##        self.visitor_cache = {}
> +##        self.seen = {}
> +##        self.visit(functiongraph)
> +
> +##    def visit(self, node):
> +##        if id(node) in self.seen:
> +##            return
> +
> +##        # do the visit
> +##        cls = node.__class__
> +##        try:
> +##            consume = self.visitor_cache[cls]
> +##        except KeyError:
> +##            for subclass in inspect.getmro(cls):
> +##                consume = getattr(self.visitor, "visit_" + 
> subclass.__name__, None)
> +##                if consume:
> +##                    break
> +##            else:
> +##                consume = getattr(self.visitor, 'visit', 
self.visitor)
> +
> +##                assert callable(consume), "visitor not found for 
> %r on %r" % (cls, self.visitor)
> +
> +##                self.visitor_cache[cls] = consume
> +
> +##        self.seen[id(node)] = consume(node)
> +
> +##        # recurse
> +##        if isinstance(node, Block):
> +##            for obj in node.exits:
> +##                self.visit(obj)
> +##        elif isinstance(node, Link):
> +##            self.visit(node.target)
> +##        elif isinstance(node, FunctionGraph):
> +##            self.visit(node.startblock)
> +##        else:
> +##            raise ValueError, "could not dispatch %r" % cls
> +
> +def traverse(visit, functiongraph):
> +    pending = [functiongraph.startblock]
> +    seen = {id(functiongraph.startblock): True}
> +    for block in pending:
> +        visit(block)
> +        for link in block.exits:
> +            visit(link)
> +            targetid = id(link.target)
> +            if targetid not in seen:
> +                pending.append(link.target)
> +                seen[targetid] = True
> 
> -        # do the visit
> -        cls = node.__class__
> -        try:
> -            consume = self.visitor_cache[cls]
> -        except KeyError:
> -            for subclass in inspect.getmro(cls):
> -                consume = getattr(self.visitor, "visit_" + 
> subclass.__name__, None)
> -                if consume:
> -                    break
> -            else:
> -                consume = getattr(self.visitor, 'visit', self.visitor)
> -
> -                assert callable(consume), "visitor not found for %r
> on %r" % (cls, self.visitor)
> -
> -                self.visitor_cache[cls] = consume
> -
> -        self.seen[id(node)] = consume(node)
> -
> -        # recurse
> -        if isinstance(node, Block):
> -            for obj in node.exits:
> -                self.visit(obj)
> -        elif isinstance(node, Link):
> -            self.visit(node.target)
> -        elif isinstance(node, FunctionGraph):
> -            self.visit(node.startblock)
> -        else:
> -            raise ValueError, "could not dispatch %r" % cls
> 
>  def flatten(funcgraph):
>      l = []
> 
> Modified: pypy/dist/pypy/objspace/flow/test/test_model.py
> 
==============================================================================
> --- pypy/dist/pypy/objspace/flow/test/test_model.py   (original)
> +++ pypy/dist/pypy/objspace/flow/test/test_model.py   Fri Sep 23 
12:04:04 2005
> @@ -28,27 +28,27 @@
>          graph = self.getflow(self.simplefunc)
>          assert all_operations(graph) == {'add': 1}
> 
> -    def test_class(self):
> -        graph = self.getflow(self.simplefunc)
> +##    def test_class(self):
> +##        graph = self.getflow(self.simplefunc)
> 
> -        class MyVisitor:
> -            def __init__(self):
> -                self.blocks = []
> -                self.links = []
> -
> -            def visit_FunctionGraph(self, graph):
> -                self.graph = graph
> -            def visit_Block(self, block):
> -                self.blocks.append(block)
> -            def visit_Link(self, link):
> -                self.links.append(link)
> -
> -        v = MyVisitor()
> -        traverse(v, graph)
> -        #assert len(v.blocks) == 2
> -        #assert len(v.links) == 1
> -        assert v.graph == graph
> -        assert v.links[0] == graph.startblock.exits[0]
> +##        class MyVisitor:
> +##            def __init__(self):
> +##                self.blocks = []
> +##                self.links = []
> +
> +##            def visit_FunctionGraph(self, graph):
> +##                self.graph = graph
> +##            def visit_Block(self, block):
> +##                self.blocks.append(block)
> +##            def visit_Link(self, link):
> +##                self.links.append(link)
> +
> +##        v = MyVisitor()
> +##        traverse(v, graph)
> +##        #assert len(v.blocks) == 2
> +##        #assert len(v.links) == 1
> +##        assert v.graph == graph
> +##        assert v.links[0] == graph.startblock.exits[0]
> 
>  ##    def test_partial_class(self):
>  ##        graph = self.getflow(self.simplefunc)
> _______________________________________________
> pypy-svn mailing list
> pypy-svn at codespeak.net
> http://codespeak.net/mailman/listinfo/pypy-svn
> 




More information about the Pypy-dev mailing list