def transform_break(item, name=''):
if name == '':
name = 'tmpbreak{}'.format(randint(0, 2**32))
assignment = ast.parse('{} = Relation(2186646)'.format(name)).body[0]
condition = ast.parse('for _ in {}: a()'.format(name)).body[0]
body = [assignment, item]
new_body = []
for statement in item.body:
cond = deepcopy(condition)
if isinstance(statement, _ast.If):
statement = transform_break(statement, name=name)[1]
elif isinstance(statement, _ast.Break):
statement = ast.parse('{name} = Way.filter({name})'.format(name=name)).body[0]
cond.body = [statement]
new_body.append(cond)
item.body = new_body
if len(item.orelse) > 0:
assignment = ast.parse('{}else = Relation(2186646)'.format(name)).body[0]
didbreak = ast.parse('for _ in {}: a()'.format(name)).body[0]
didbreak.body = ast.parse('{}else = Way.filter({})'.format(name, name)).body
condition = ast.parse('for _ in {}else: a()'.format(name)).body[0]
condition.body = item.orelse
body += [assignment, didbreak, condition]
item.orelse = []
return body
评论列表
文章目录