def summarize_execution( fn: Callable, args: Sequence[object] = (), kwargs: Mapping[str, object] = None, detach_path: bool = True, ) -> ExecutionResult: if not kwargs: kwargs = {} ret = None exc = None try: symbolic_ret = fn(*args, **kwargs) if detach_path: context_statespace().detach_path() _ret = realize(symbolic_ret) # TODO, this covers up potential issues with return types. Handle differently? # summarize iterators as the values they produce: if hasattr(_ret, "__next__"): ret = list(_ret) else: ret = _ret except BaseException as e: if isinstance(e, (UnexploredPath, IgnoreAttempt)): raise if in_debug(): debug("hit exception:", type(e), e, test_stack(e.__traceback__)) exc = e if detach_path: context_statespace().detach_path() args = tuple(realize(a) for a in args) kwargs = {k: realize(v) for (k, v) in kwargs.items()} return ExecutionResult(ret, exc, args, kwargs)
def describe(self, include_postexec=False) -> str: ret = "" if self.exc: exc = self.exc exc_type = name_of_type(type(exc)) tb = test_stack(exc.__traceback__) ret = f"exc={exc_type}: {str(exc)} {tb}" else: ret = f"ret={self.ret!r}" if include_postexec: a = [repr(a) for a in self.post_args] a += [f"{k}={v!r}" for k, v in self.post_kwargs.items()] ret += f' post=({", ".join(a)})' return ret
def find_model_value(self, expr: z3.ExprRef) -> object: with NoTracing(): while True: if self.search_position.is_stem(): self.search_position = self.search_position.grow_into( ModelValueNode(self._random, expr, self.solver)) node = self.search_position.simplify() assert isinstance(node, ModelValueNode) (chosen, next_node) = node.choose(favor_true=True) self.choices_made.append(node) self.search_position = next_node if chosen: self.solver.add(expr == node.condition_value) ret = model_value_to_python(node.condition_value) if in_debug(): debug("SMT realized symbolic:", expr, "==", repr(ret)) debug("Realized at", test_stack()) return ret else: self.solver.add(expr != node.condition_value)
def summarize_execution( fn: Callable, args: Sequence[object] = (), kwargs: Mapping[str, object] = None) -> ExecutionResult: if not kwargs: kwargs = {} ret = None exc = None try: _ret = realize(fn(*args, **kwargs)) # summarize iterators as the values they produce: if hasattr(_ret, "__next__"): ret = list(_ret) else: ret = _ret except BaseException as e: if isinstance(e, (UnexploredPath, IgnoreAttempt)): raise if in_debug(): debug(type(e), e, test_stack(e.__traceback__)) exc = e args = tuple(realize(a) for a in args) kwargs = {k: realize(v) for (k, v) in kwargs.items()} return ExecutionResult(ret, exc, args, kwargs)
def __init__(self, result: ExecutionResult): self.ret = result.ret self.exc = result.exc self.tb = test_stack(self.exc.__traceback__) if self.exc else None self.post_args = result.post_args self.post_kwargs = result.post_kwargs