Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_expand_exceptions(self):
expand = partial(NodeValue.expression, eye.num_samples)
class A(object):
def __len__(self):
assert 0
with self.assertRaises(AssertionError):
len(A())
self.assertIsNone(expand(A(), 1).meta)
self.assertEqual(expand([4, 4, 4], 1).meta['len'], 3)
class FakeSet(Set):
def __len__(self):
return 0
def __iter__(self):
def after_stmt(self, node, frame, exc_value, exc_traceback, exc_node):
# type: (ast.stmt, FrameType, Optional[BaseException], Optional[TracebackType], Optional[ast.AST]) -> Optional[bool]
if frame.f_code not in self._code_infos or _tracing_recursively(frame):
return None
if exc_value and node is exc_node:
value = self._exception_value(node, frame, exc_value)
else:
value = NodeValue.covered()
self._set_node_value(node, frame, value)
self._check_inner_call(self.stack[frame], node, value)
return None
def _exception_value(self, node, frame, exc_value):
# type: (Union[ast.expr, ast.stmt], FrameType, BaseException) -> NodeValue
value = NodeValue.exception(exc_value)
self._set_node_value(node, frame, value)
return value
# If this is an expression statement and the last statement
# in the body, the value is returned from the cell magic
# to be displayed as usual
if (self._code_infos[frame.f_code].traced_file.is_ipython_cell
and isinstance(node.parent, ast.Expr)
and node.parent is node.parent.parent.body[-1]):
self._ipython_cell_value = value
if is_obvious_builtin(node, self.stack[frame].expression_values[node]):
return None
frame_info = self.stack[frame]
if exc_value:
node_value = self._exception_value(node, frame, exc_value)
else:
node_value = NodeValue.expression(
self.num_samples,
value,
level=max(1, 3 - len(node._loops) * (not self._is_first_loop_iteration(node, frame))),
)
self._set_node_value(node, frame, node_value)
self._check_inner_call(frame_info, node, node_value)
# i.e. is `node` the `y` in `[f(x) for x in y]`, making `node.parent` the `for x in y`
is_special_comprehension_iter = (
isinstance(node.parent, ast.comprehension) and
node is node.parent.iter and
# Generators execute in their own time and aren't directly attached to the parent frame
not isinstance(node.parent.parent, ast.GeneratorExp))
if not is_special_comprehension_iter:
def add_child(self, samples, level, key, value):
# type: (dict, int, str, Any) -> None
self.children = self.children or []
self.children.append((key, NodeValue.expression(samples, value, level)))