本文整理汇总了Python中astroid.TryExcept方法的典型用法代码示例。如果您正苦于以下问题:Python astroid.TryExcept方法的具体用法?Python astroid.TryExcept怎么用?Python astroid.TryExcept使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类astroid
的用法示例。
在下文中一共展示了astroid.TryExcept方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_exception_handlers
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import TryExcept [as 别名]
def get_exception_handlers(node, exception):
"""Return the collections of handlers handling the exception in arguments.
Args:
node (astroid.Raise): the node raising the exception.
exception (builtin.Exception or str): exception or name of the exception.
Returns:
generator: the collection of handlers that are handling the exception or None.
"""
context = _import_node_context(node)
if isinstance(context, astroid.TryExcept):
return (_handler for _handler in context.handlers
if error_of_type(_handler, exception))
return None
示例2: visit_functiondef
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import TryExcept [as 别名]
def visit_functiondef(self, node):
if not self.linter.is_message_enabled('wrong-import-position', node.fromlineno):
return
# If it is the first non import instruction of the module, record it.
if self._first_non_import_node:
return
# Check if the node belongs to an `If` or a `Try` block. If they
# contain imports, skip recording this node.
if not isinstance(node.parent.scope(), astroid.Module):
return
root = node
while not isinstance(root.parent, astroid.Module):
root = root.parent
if isinstance(root, (astroid.If, astroid.TryFinally, astroid.TryExcept)):
if any(root.nodes_of_class((astroid.Import, astroid.ImportFrom))):
return
self._first_non_import_node = node
示例3: get_exception_handlers
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import TryExcept [as 别名]
def get_exception_handlers(
node: astroid.node_classes.NodeNG, exception=Exception
) -> List[astroid.ExceptHandler]:
"""Return the collections of handlers handling the exception in arguments.
Args:
node (astroid.NodeNG): A node that is potentially wrapped in a try except.
exception (builtin.Exception or str): exception or name of the exception.
Returns:
list: the collection of handlers that are handling the exception or None.
"""
context = find_try_except_wrapper_node(node)
if isinstance(context, astroid.TryExcept):
return [
handler for handler in context.handlers if error_of_type(handler, exception)
]
return None
示例4: visit_functiondef
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import TryExcept [as 别名]
def visit_functiondef(self, node):
if not self.linter.is_message_enabled("wrong-import-position", node.fromlineno):
return
# If it is the first non import instruction of the module, record it.
if self._first_non_import_node:
return
# Check if the node belongs to an `If` or a `Try` block. If they
# contain imports, skip recording this node.
if not isinstance(node.parent.scope(), astroid.Module):
return
root = node
while not isinstance(root.parent, astroid.Module):
root = root.parent
if isinstance(root, (astroid.If, astroid.TryFinally, astroid.TryExcept)):
if any(root.nodes_of_class((astroid.Import, astroid.ImportFrom))):
return
self._first_non_import_node = node
示例5: _import_node_context
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import TryExcept [as 别名]
def _import_node_context(node):
"""Return the ExceptHandler or the TryExcept node in which the node is."""
current = node
ignores = (astroid.ExceptHandler, astroid.TryExcept)
while current and not isinstance(current.parent, ignores):
current = current.parent
if current and isinstance(current.parent, ignores):
return current.parent
return None
示例6: is_node_inside_try_except
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import TryExcept [as 别名]
def is_node_inside_try_except(node):
"""Check if the node is directly under a Try/Except statement.
(but not under an ExceptHandler!)
Args:
node (astroid.Raise): the node raising the exception.
Returns:
bool: True if the node is inside a try/except statement, False otherwise.
"""
context = _import_node_context(node)
return isinstance(context, astroid.TryExcept)
示例7: node_ignores_exception
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import TryExcept [as 别名]
def node_ignores_exception(node, exception):
"""Check if the node is in a TryExcept which handles the given exception."""
managing_handlers = get_exception_handlers(node, exception)
if not managing_handlers:
return False
return any(managing_handlers)
示例8: visit_expr
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import TryExcept [as 别名]
def visit_expr(self, node):
"""check for various kind of statements without effect"""
expr = node.value
if isinstance(expr, astroid.Const) and isinstance(expr.value,
six.string_types):
# treat string statement in a separated message
# Handle PEP-257 attribute docstrings.
# An attribute docstring is defined as being a string right after
# an assignment at the module level, class level or __init__ level.
scope = expr.scope()
if isinstance(scope, (astroid.ClassDef, astroid.Module, astroid.FunctionDef)):
if isinstance(scope, astroid.FunctionDef) and scope.name != '__init__':
pass
else:
sibling = expr.previous_sibling()
if (sibling is not None and sibling.scope() is scope and
isinstance(sibling, astroid.Assign)):
return
self.add_message('pointless-string-statement', node=node)
return
# ignore if this is :
# * a direct function call
# * the unique child of a try/except body
# * a yield (which are wrapped by a discard node in _ast XXX)
# warn W0106 if we have any underlying function call (we can't predict
# side effects), else pointless-statement
if (isinstance(expr, (astroid.Yield, astroid.Await, astroid.Call)) or
(isinstance(node.parent, astroid.TryExcept) and
node.parent.body == [node])):
return
if any(expr.nodes_of_class(astroid.Call)):
self.add_message('expression-not-assigned', node=node,
args=expr.as_string())
else:
self.add_message('pointless-statement', node=node)
示例9: compute_first_non_import_node
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import TryExcept [as 别名]
def compute_first_non_import_node(self, node):
if not self.linter.is_message_enabled('wrong-import-position', node.fromlineno):
return
# if the node does not contain an import instruction, and if it is the
# first node of the module, keep a track of it (all the import positions
# of the module will be compared to the position of this first
# instruction)
if self._first_non_import_node:
return
if not isinstance(node.parent, astroid.Module):
return
nested_allowed = [astroid.TryExcept, astroid.TryFinally]
is_nested_allowed = [
allowed for allowed in nested_allowed if isinstance(node, allowed)]
if is_nested_allowed and \
any(node.nodes_of_class((astroid.Import, astroid.ImportFrom))):
return
if isinstance(node, astroid.Assign):
# Add compatibility for module level dunder names
# https://www.python.org/dev/peps/pep-0008/#module-level-dunder-names
valid_targets = [
isinstance(target, astroid.AssignName) and
target.name.startswith('__') and target.name.endswith('__')
for target in node.targets]
if all(valid_targets):
return
self._first_non_import_node = node
示例10: visit_tryexcept
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import TryExcept [as 别名]
def visit_tryexcept(self, node: astroid.TryExcept) -> None:
if self._current_block.statements != []:
self._current_block = self._current_cfg.create_block(self._current_block)
node.cfg_block = self._current_block
for child in node.body:
child.accept(self)
end_body = self._current_block
end_block = self._current_cfg.create_block()
after_body = []
for handler in node.handlers:
h = self._current_cfg.create_block()
self._current_block = h
handler.cfg_block = h
if handler.name is not None: # The name assigned to the caught exception.
handler.name.accept(self)
for child in handler.body:
child.accept(self)
end_handler = self._current_block
self._current_cfg.link_or_merge(end_handler, end_block)
after_body.append(h)
if node.orelse == []:
after_body.append(end_block)
else:
self._current_block = self._current_cfg.create_block()
after_body.append(self._current_block)
for child in node.orelse:
child.accept(self)
self._current_cfg.link_or_merge(self._current_block, end_block)
self._current_cfg.multiple_link_or_merge(end_body, after_body)
self._current_block = end_block
示例11: find_try_except_wrapper_node
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import TryExcept [as 别名]
def find_try_except_wrapper_node(
node: astroid.node_classes.NodeNG
) -> Union[astroid.ExceptHandler, astroid.TryExcept]:
"""Return the ExceptHandler or the TryExcept node in which the node is."""
current = node
ignores = (astroid.ExceptHandler, astroid.TryExcept)
while current and not isinstance(current.parent, ignores):
current = current.parent
if current and isinstance(current.parent, ignores):
return current.parent
return None
示例12: is_node_inside_try_except
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import TryExcept [as 别名]
def is_node_inside_try_except(node: astroid.Raise) -> bool:
"""Check if the node is directly under a Try/Except statement.
(but not under an ExceptHandler!)
Args:
node (astroid.Raise): the node raising the exception.
Returns:
bool: True if the node is inside a try/except statement, False otherwise.
"""
context = find_try_except_wrapper_node(node)
return isinstance(context, astroid.TryExcept)
示例13: node_ignores_exception
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import TryExcept [as 别名]
def node_ignores_exception(
node: astroid.node_classes.NodeNG, exception=Exception
) -> bool:
"""Check if the node is in a TryExcept which handles the given exception.
If the exception is not given, the function is going to look for bare
excepts.
"""
managing_handlers = get_exception_handlers(node, exception)
if not managing_handlers:
return False
return any(managing_handlers)
示例14: compute_first_non_import_node
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import TryExcept [as 别名]
def compute_first_non_import_node(self, node):
if not self.linter.is_message_enabled("wrong-import-position", node.fromlineno):
return
# if the node does not contain an import instruction, and if it is the
# first node of the module, keep a track of it (all the import positions
# of the module will be compared to the position of this first
# instruction)
if self._first_non_import_node:
return
if not isinstance(node.parent, astroid.Module):
return
nested_allowed = [astroid.TryExcept, astroid.TryFinally]
is_nested_allowed = [
allowed for allowed in nested_allowed if isinstance(node, allowed)
]
if is_nested_allowed and any(
node.nodes_of_class((astroid.Import, astroid.ImportFrom))
):
return
if isinstance(node, astroid.Assign):
# Add compatibility for module level dunder names
# https://www.python.org/dev/peps/pep-0008/#module-level-dunder-names
valid_targets = [
isinstance(target, astroid.AssignName)
and target.name.startswith("__")
and target.name.endswith("__")
for target in node.targets
]
if all(valid_targets):
return
self._first_non_import_node = node
示例15: _is_conditional_import
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import TryExcept [as 别名]
def _is_conditional_import(node):
"""Checks if an import node is in the context of a conditional.
"""
parent = node.parent
return isinstance(
parent, (astroid.TryExcept, astroid.ExceptHandler, astroid.If, astroid.IfExp)
)