本文整理汇总了Python中pypy.objspace.flow.model.traverse函数的典型用法代码示例。如果您正苦于以下问题:Python traverse函数的具体用法?Python traverse怎么用?Python traverse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了traverse函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: remove_dead_exceptions
def remove_dead_exceptions(graph):
"""Exceptions can be removed if they are unreachable"""
clastexc = c_last_exception
def issubclassofmember(cls, seq):
for member in seq:
if member and issubclass(cls, member):
return True
return False
def visit(block):
if not (isinstance(block, Block) and block.exitswitch == clastexc):
return
exits = []
seen = []
for link in block.exits:
case = link.exitcase
# check whether exceptions are shadowed
if issubclassofmember(case, seen):
continue
# see if the previous case can be merged
while len(exits) > 1:
prev = exits[-1]
if not (issubclass(prev.exitcase, link.exitcase) and
prev.target is link.target and prev.args == link.args):
break
exits.pop()
exits.append(link)
seen.append(case)
block.recloseblock(*exits)
traverse(visit, graph)
示例2: test_jump_target_specialization
def test_jump_target_specialization(self):
x = self.codetest(self.jump_target_specialization)
def visitor(node):
if isinstance(node, Block):
for op in node.operations:
assert op.opname != 'mul', "mul should have disappeared"
traverse(visitor, x)
示例3: remove_assertion_errors
def remove_assertion_errors(graph):
"""Remove branches that go directly to raising an AssertionError,
assuming that AssertionError shouldn't occur at run-time. Note that
this is how implicit exceptions are removed (see _implicit_ in
flowcontext.py).
"""
def visit(block):
if isinstance(block, Block):
for i in range(len(block.exits)-1, -1, -1):
exit = block.exits[i]
if not (exit.target is graph.exceptblock and
exit.args[0] == Constant(AssertionError)):
continue
# can we remove this exit without breaking the graph?
if len(block.exits) < 2:
break
if block.exitswitch == c_last_exception:
if exit.exitcase is None:
break
if len(block.exits) == 2:
# removing the last non-exceptional exit
block.exitswitch = None
exit.exitcase = None
# remove this exit
lst = list(block.exits)
del lst[i]
block.recloseblock(*lst)
traverse(visit, graph)
示例4: eliminate_empty_blocks
def eliminate_empty_blocks(graph):
"""Eliminate basic blocks that do not contain any operations.
When this happens, we need to replace the preceeding link with the
following link. Arguments of the links should be updated."""
def visit(link):
if isinstance(link, Link):
while not link.target.operations:
block1 = link.target
if block1.exitswitch is not None:
break
if not block1.exits:
break
exit = block1.exits[0]
assert block1 is not exit.target, (
"the graph contains an empty infinite loop")
outputargs = []
for v in exit.args:
if isinstance(v, Variable):
# this variable is valid in the context of block1
# but it must come from 'link'
i = block1.inputargs.index(v)
v = link.args[i]
outputargs.append(v)
link.args = outputargs
link.target = exit.target
# the while loop above will simplify recursively the new link
traverse(visit, graph)
示例5: test_implicitAttributeError
def test_implicitAttributeError(self):
x = self.codetest(self.implicitAttributeError)
simplify_graph(x)
self.show(x)
def cannot_reach_exceptblock(link):
if isinstance(link, Link):
assert link.target is not x.exceptblock
traverse(cannot_reach_exceptblock, x)
示例6: all_operations
def all_operations(self, graph):
result = {}
def visit(node):
if isinstance(node, Block):
for op in node.operations:
result.setdefault(op.opname, 0)
result[op.opname] += 1
traverse(visit, graph)
return result
示例7: test_reraiseTypeError
def test_reraiseTypeError(self):
x = self.codetest(self.reraiseTypeError)
simplify_graph(x)
self.show(x)
found = []
def can_reach_exceptblock(link):
if isinstance(link, Link):
if link.target is x.exceptblock:
found.append(link)
traverse(can_reach_exceptblock, x)
assert found
示例8: test_reraiseAnything
def test_reraiseAnything(self):
x = self.codetest(self.reraiseAnything)
simplify_graph(x)
self.show(x)
found = {}
def find_exceptions(link):
if isinstance(link, Link):
if link.target is x.exceptblock:
assert isinstance(link.args[0], Constant)
found[link.args[0].value] = True
traverse(find_exceptions, x)
assert found == {ValueError: True, ZeroDivisionError: True, OverflowError: True}
示例9: test_reraiseAttributeError
def test_reraiseAttributeError(self):
x = self.codetest(self.reraiseAttributeError)
simplify_graph(x)
self.show(x)
found_AttributeError = []
def only_raise_AttributeError(link):
if isinstance(link, Link):
if link.target is x.exceptblock:
assert link.args[0] == Constant(AttributeError)
found_AttributeError.append(link)
traverse(only_raise_AttributeError, x)
assert found_AttributeError
示例10: test_const_star_call
def test_const_star_call(self):
def g(a=1,b=2,c=3):
pass
def f():
return g(1,*(2,3))
graph = self.codetest(f)
call_args = []
def visit(block):
if isinstance(block, Block):
for op in block.operations:
if op.opname == "call_args":
call_args.append(op)
traverse(visit, graph)
assert not call_args
示例11: test_reraiseAnythingDicCase
def test_reraiseAnythingDicCase(self):
x = self.codetest(self.reraiseAnythingDicCase)
simplify_graph(x)
self.show(x)
found = {}
def find_exceptions(link):
if isinstance(link, Link):
if link.target is x.exceptblock:
if isinstance(link.args[0], Constant):
found[link.args[0].value] = True
else:
found[link.exitcase] = None
traverse(find_exceptions, x)
assert found == {IndexError: True, KeyError: True, Exception: None}
示例12: test_reraiseTypeError
def test_reraiseTypeError(self):
x = self.codetest(self.reraiseTypeError)
simplify_graph(x)
self.show(x)
excfound = []
def check(link):
if isinstance(link, Link):
if link.target is x.exceptblock:
excfound.append(link.exitcase)
traverse(check, x)
assert len(excfound) == 2
excfound.sort()
expected = [Exception, TypeError]
expected.sort()
assert excfound == expected
示例13: patch_graphs
def patch_graphs(self):
def patch_consts(args):
for arg in args:
if isinstance(arg, Constant) and arg in self.constants:
arg.value = self.constants[arg]
def visit(obj):
if isinstance(obj, Link):
patch_consts(obj.args)
if (hasattr(obj, "llexitcase") and
Constant(obj.llexitcase) in self.constants):
obj.llexitcase = self.constants[Constant(obj.llexitcase)]
elif isinstance(obj, Block):
for op in obj.operations:
patch_consts(op.args)
for graph in self.graphs:
traverse(visit, graph)
示例14: graph_footprint
def graph_footprint(graph):
class Counter:
blocks = 0
links = 0
ops = 0
count = Counter()
def visit(block):
if isinstance(block, flowmodel.Block):
count.blocks += 1
count.ops += len(block.operations)
elif isinstance(block, flowmodel.Link):
count.links += 1
flowmodel.traverse(visit, graph)
return count.blocks, count.links, count.ops
示例15: ordered_blocks
def ordered_blocks(graph):
# collect all blocks
allblocks = []
def visit(block):
if isinstance(block, Block):
# first we order by offset in the code string
if block.operations:
ofs = block.operations[0].offset
else:
ofs = sys.maxint
# then we order by input variable name or value
if block.inputargs:
txt = str(block.inputargs[0])
else:
txt = "dummy"
allblocks.append((ofs, txt, block))
traverse(visit, graph)
allblocks.sort()
#for ofs, txt, block in allblocks:
# print ofs, txt, block
return [block for ofs, txt, block in allblocks]