本文整理匯總了Python中ast.TryExcept方法的典型用法代碼示例。如果您正苦於以下問題:Python ast.TryExcept方法的具體用法?Python ast.TryExcept怎麽用?Python ast.TryExcept使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ast
的用法示例。
在下文中一共展示了ast.TryExcept方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _find_blacklist_imports
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import TryExcept [as 別名]
def _find_blacklist_imports(self):
for child in self.ast.body:
names = []
if isinstance(child, ast.Import):
names.extend(child.names)
elif isinstance(child, ast.TryExcept):
bodies = child.body
for handler in child.handlers:
bodies.extend(handler.body)
for grandchild in bodies:
if isinstance(grandchild, ast.Import):
names.extend(grandchild.names)
for name in names:
for blacklist_import, options in BLACKLIST_IMPORTS.items():
if re.search(blacklist_import, name.name):
msg = options['msg']
new_only = options['new_only']
if self._is_new_module() and new_only:
self.errors.append(msg)
elif not new_only:
self.errors.append(msg)
示例2: _find_has_import
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import TryExcept [as 別名]
def _find_has_import(self):
for child in self.ast.body:
found_try_except_import = False
found_has = False
if isinstance(child, ast.TryExcept):
bodies = child.body
for handler in child.handlers:
bodies.extend(handler.body)
for grandchild in bodies:
if isinstance(grandchild, ast.Import):
found_try_except_import = True
if isinstance(grandchild, ast.Assign):
for target in grandchild.targets:
if target.id.lower().startswith('has_'):
found_has = True
if found_try_except_import and not found_has:
self.warnings.append('Found Try/Except block without HAS_ '
'assginment')
示例3: visit_Try
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import TryExcept [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: visit_TryFinally
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import TryExcept [as 別名]
def visit_TryFinally(self, node):
# Try with except and finally is a TryFinally with the first statement as a
# TryExcept in Python2
self.attr(node, 'open_try', ['try', self.ws, ':', self.ws_oneline],
default='try:\n')
# TODO(soupytwist): Find a cleaner solution for differentiating this.
if len(node.body) == 1 and self.check_is_continued_try(node.body[0]):
node.body[0].is_continued = True
self.visit(node.body[0])
else:
for stmt in self.indented(node, 'body'):
self.visit(stmt)
self.attr(node, 'open_finally',
[self.ws, 'finally', self.ws, ':', self.ws_oneline],
default='finally:\n')
for stmt in self.indented(node, 'finalbody'):
self.visit(stmt)
示例5: getAlternatives
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import TryExcept [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]
示例6: is_try
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import TryExcept [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)
示例7: getNodeType
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import TryExcept [as 別名]
def getNodeType(node_class):
return node_class.__name__.upper()
# Python >= 3.3 uses ast.Try instead of (ast.TryExcept + ast.TryFinally)
示例8: p_try_stmt_1
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import TryExcept [as 別名]
def p_try_stmt_1(p):
'''try_stmt : TRY COLON suite try_stmt_plus'''
# 1 2 3 4
p[0] = ast.TryExcept(p[3], p[4], [], rule=inspect.currentframe().f_code.co_name, **p[1][1])
示例9: p_try_stmt_2
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import TryExcept [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])
示例10: p_try_stmt_3
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import TryExcept [as 別名]
def p_try_stmt_3(p):
'''try_stmt : TRY COLON suite try_stmt_plus ELSE COLON suite'''
# 1 2 3 4 5 6 7
p[0] = ast.TryExcept(p[3], p[4], p[7], rule=inspect.currentframe().f_code.co_name, **p[1][1])
示例11: p_try_stmt_4
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import TryExcept [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])
示例12: check_is_continued_try
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import TryExcept [as 別名]
def check_is_continued_try(self, node):
"""Return True iff the TryExcept node is a continued `try` in the source."""
return (isinstance(node, ast.TryExcept) and
self.tokens.peek_non_whitespace().src != 'try')
示例13: translate_try
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import TryExcept [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]
示例14: get_last_child
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import TryExcept [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]