本文整理汇总了Python中ast.TryFinally方法的典型用法代码示例。如果您正苦于以下问题:Python ast.TryFinally方法的具体用法?Python ast.TryFinally怎么用?Python ast.TryFinally使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ast
的用法示例。
在下文中一共展示了ast.TryFinally方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: CONTINUE
# 需要导入模块: import ast [as 别名]
# 或者: from ast import TryFinally [as 别名]
def CONTINUE(self, node):
# Walk the tree up until we see a loop (OK), a function or class
# definition (not OK), for 'continue', a finally block (not OK), or
# the top module scope (not OK)
n = node
while hasattr(n, 'parent'):
n, n_child = n.parent, n
if isinstance(n, LOOP_TYPES):
# Doesn't apply unless it's in the loop itself
if n_child not in n.orelse:
return
if isinstance(n, (ast.FunctionDef, ast.ClassDef)):
break
# Handle Try/TryFinally difference in Python < and >= 3.3
if hasattr(n, 'finalbody') and isinstance(node, ast.Continue):
if n_child in n.finalbody:
self.report(messages.ContinueInFinally, node)
return
if isinstance(node, ast.Continue):
self.report(messages.ContinueOutsideLoop, node)
else: # ast.Break
self.report(messages.BreakOutsideLoop, node)
示例2: CONTINUE
# 需要导入模块: import ast [as 别名]
# 或者: from ast import TryFinally [as 别名]
def CONTINUE(self, node):
# Walk the tree up until we see a loop (OK), a function or class
# definition (not OK), for 'continue', a finally block (not OK), or
# the top module scope (not OK)
n = node
while hasattr(n, '_pyflakes_parent'):
n, n_child = n._pyflakes_parent, n
if isinstance(n, LOOP_TYPES):
# Doesn't apply unless it's in the loop itself
if n_child not in n.orelse:
return
if isinstance(n, (ast.FunctionDef, ast.ClassDef)):
break
# Handle Try/TryFinally difference in Python < and >= 3.3
if hasattr(n, 'finalbody') and isinstance(node, ast.Continue):
if n_child in n.finalbody and not PY38_PLUS:
self.report(messages.ContinueInFinally, node)
return
if isinstance(node, ast.Continue):
self.report(messages.ContinueOutsideLoop, node)
else: # ast.Break
self.report(messages.BreakOutsideLoop, node)
示例3: visit_Try
# 需要导入模块: import ast [as 别名]
# 或者: from ast import TryFinally [as 别名]
def visit_Try(self, node):
if node.finalbody:
new_node = ast.TryFinally(
self._visit(node.body),
self._visit(node.finalbody)
)
else:
new_node = ast.TryExcept(
self._visit(node.body),
self._visit(node.handlers),
self._visit(node.orelse),
)
ast.copy_location(new_node, node)
return new_node
# expr
示例4: getAlternatives
# 需要导入模块: import ast [as 别名]
# 或者: from ast import TryFinally [as 别名]
def getAlternatives(n):
if isinstance(n, (ast.If, ast.TryFinally)):
return [n.body]
if isinstance(n, ast.TryExcept):
return [n.body + n.orelse] + [[hdl] for hdl in n.handlers]
示例5: is_try
# 需要导入模块: import ast [as 别名]
# 或者: from ast import TryFinally [as 别名]
def is_try(node):
return hasattr(ast, "Try") and isinstance(node, ast.Try) or \
hasattr(ast, "TryExcept") and isinstance(node, ast.TryExcept) or \
hasattr(ast, "TryFinally") and isinstance(node, ast.TryFinally)
示例6: getNodeType
# 需要导入模块: import ast [as 别名]
# 或者: from ast import TryFinally [as 别名]
def getNodeType(node_class):
return node_class.__name__.upper()
# Python >= 3.3 uses ast.Try instead of (ast.TryExcept + ast.TryFinally)
示例7: p_try_stmt_2
# 需要导入模块: import ast [as 别名]
# 或者: from ast import TryFinally [as 别名]
def p_try_stmt_2(p):
'''try_stmt : TRY COLON suite try_stmt_plus FINALLY COLON suite'''
# 1 2 3 4 5 6 7
p[0] = ast.TryFinally([ast.TryExcept(p[3], p[4], [], rule=inspect.currentframe().f_code.co_name, **p[1][1])], p[7], rule=inspect.currentframe().f_code.co_name, **p[1][1])
示例8: p_try_stmt_4
# 需要导入模块: import ast [as 别名]
# 或者: from ast import TryFinally [as 别名]
def p_try_stmt_4(p):
'''try_stmt : TRY COLON suite try_stmt_plus ELSE COLON suite FINALLY COLON suite'''
# 1 2 3 4 5 6 7 8 9 10
p[0] = ast.TryFinally([ast.TryExcept(p[3], p[4], p[7], rule=inspect.currentframe().f_code.co_name, **p[1][1])], p[10], rule=inspect.currentframe().f_code.co_name, **p[1][1])
示例9: p_try_stmt_5
# 需要导入模块: import ast [as 别名]
# 或者: from ast import TryFinally [as 别名]
def p_try_stmt_5(p):
'''try_stmt : TRY COLON suite FINALLY COLON suite'''
# 1 2 3 4 5 6
p[0] = ast.TryFinally(p[3], p[6], rule=inspect.currentframe().f_code.co_name, **p[1][1])
示例10: translate_try
# 需要导入模块: import ast [as 别名]
# 或者: from ast import TryFinally [as 别名]
def translate_try(self, exp):
if len(exp) < 2:
raise MochiSyntaxError(exp, self.filename)
body_exp = []
handler_exps = []
orelse_exp = []
final_body_exp = []
for expr in exp:
if issequence_except_str(expr) and len(expr) > 1 and isinstance(expr[0], Symbol):
expr_name = expr[0].name
if expr_name == 'finally':
final_body_exp = expr[1:]
continue
elif expr_name == 'orelse':
orelse_exp = expr[1:]
continue
elif expr_name == 'except':
handler_exps.append(expr[1:])
continue
body_exp.append(expr)
body = self._translate_sequence(body_exp, True)
handlers = self._translate_handlers(handler_exps)
orelse = self._translate_sequence(orelse_exp, True)
final_body = self._translate_sequence(final_body_exp, True)
if GE_PYTHON_34:
return (ast.Try(body=body,
handlers=handlers,
orelse=orelse,
finalbody=final_body,
lineno=exp[0].lineno,
col_offset=0),), self.translate(EMPTY_SYM, False)[1]
else:
if len(handlers) == 0:
return (ast.TryFinally(body=body,
finalbody=final_body,
lineno=exp[0].lineno,
col_offset=0),), self.translate(EMPTY_SYM, False)[1]
else:
return (ast.TryFinally(body=[ast.TryExcept(body=body,
handlers=handlers,
orelse=orelse,
lineno=exp[0].lineno,
col_offset=0)],
finalbody=final_body,
lineno=exp[0].lineno,
col_offset=0),), self.translate(EMPTY_SYM, False)[1]
示例11: get_last_child
# 需要导入模块: import ast [as 别名]
# 或者: from ast import TryFinally [as 别名]
def get_last_child(node):
"""Get the last child node of a block statement.
The input must be a block statement (e.g. ast.For, ast.With, etc).
Examples:
1. with first():
second()
last()
2. try:
first()
except:
second()
finally:
last()
In both cases, the last child is the node for `last`.
"""
if isinstance(node, ast.Module):
try:
return node.body[-1]
except IndexError:
return None
if isinstance(node, ast.If):
if (len(node.orelse) == 1 and isinstance(node.orelse[0], ast.If) and
fmt.get(node.orelse[0], 'is_elif')):
return get_last_child(node.orelse[0])
if node.orelse:
return node.orelse[-1]
elif isinstance(node, ast.With):
if (len(node.body) == 1 and isinstance(node.body[0], ast.With) and
fmt.get(node.body[0], 'is_continued')):
return get_last_child(node.body[0])
elif hasattr(ast, 'Try') and isinstance(node, ast.Try):
if node.finalbody:
return node.finalbody[-1]
if node.orelse:
return node.orelse[-1]
elif hasattr(ast, 'TryFinally') and isinstance(node, ast.TryFinally):
if node.finalbody:
return node.finalbody[-1]
elif hasattr(ast, 'TryExcept') and isinstance(node, ast.TryExcept):
if node.orelse:
return node.orelse[-1]
if node.handlers:
return get_last_child(node.handlers[-1])
return node.body[-1]