示例#1
0
def _capture_snapshot(a_snapshot: Snapshot,
                      resolved_kwargs: Mapping[str, Any]) -> Any:
    """
    Capture the snapshot from the keyword arguments resolved before the function call (including the default values).

    :param a_snapshot: snapshot to be captured
    :param resolved_kwargs: resolved keyword arguments (including the default values)
    :return: captured value
    """
    if a_snapshot.arg is not None:
        if a_snapshot.arg not in resolved_kwargs:
            msg_parts = []  # type: List[str]
            if a_snapshot.location is not None:
                msg_parts.append("{}:\n".format(a_snapshot.location))

            msg_parts.append((
                "The argument of the snapshot has not been set: {}. "
                "Does the original function define it? Did you supply it in the call?"
            ).format(a_snapshot.arg))

            raise TypeError(''.join(msg_parts))

        value = a_snapshot.capture(
            **{a_snapshot.arg: resolved_kwargs[a_snapshot.arg]})
    else:
        value = a_snapshot.capture()

    return value
示例#2
0
    def __init__(self,
                 capture: Callable[..., Any],
                 name: Optional[str] = None,
                 enabled: bool = __debug__) -> None:
        """
        Initialize.

        :param capture:
            function to capture the snapshot accepting a one or more arguments of the original function *prior*
            to the execution
        :param name: name of the snapshot; if omitted, the name corresponds to the name of the input argument
        :param enabled:
            The decorator is applied only if ``enabled`` is set.

            Otherwise, the snapshot is disabled and there is no run-time overhead.

            The default is to always capture the snapshot unless the interpreter runs in optimized mode (``-O`` or
            ``-OO``).

        """
        self._snapshot = None  # type: Optional[Snapshot]
        self.enabled = enabled

        # Resolve the snapshot only if enabled so that no overhead is incurred
        if enabled:
            location = None  # type: Optional[str]
            tb_stack = traceback.extract_stack(limit=2)[:1]
            if len(tb_stack) > 0:
                frame = tb_stack[0]
                location = 'File {}, line {} in {}'.format(
                    frame.filename, frame.lineno, frame.name)

            self._snapshot = Snapshot(capture=capture,
                                      name=name,
                                      location=location)
示例#3
0
def _capture_snapshot(a_snapshot: Snapshot,
                      resolved_kwargs: Mapping[str, Any]) -> Any:
    """
    Capture the snapshot from the keyword arguments resolved before the function call (including the default values).

    :param a_snapshot: snapshot to be captured
    :param resolved_kwargs: resolved keyword arguments (including the default values)
    :return: captured value
    """
    missing_args = [
        arg_name for arg_name in a_snapshot.args
        if arg_name not in resolved_kwargs
    ]
    if missing_args:
        msg_parts = []  # type: List[str]
        if a_snapshot.location is not None:
            msg_parts.append("{}:\n".format(a_snapshot.location))

        msg_parts.append((
            "The argument(s) of the snapshot have not been set: {}. "
            "Does the original function define them? Did you supply them in the call?"
        ).format(missing_args))

        raise TypeError(''.join(msg_parts))

    capture_kwargs = {
        arg_name: arg_value
        for arg_name, arg_value in resolved_kwargs.items()
        if arg_name in a_snapshot.arg_set
    }

    captured_value = a_snapshot.capture(**capture_kwargs)

    return captured_value
示例#4
0
    def __init__(self,
                 capture: Callable[..., Any],
                 name: Optional[str] = None,
                 enabled: bool = __debug__) -> None:
        """
        Initialize.

        :param capture:
            function to capture the snapshot accepting a single argument (from a set of arguments
            of the original function)
        :param name: name of the snapshot; if omitted, the name corresponds to the name of the input argument
        :param enabled:
            The decorator is applied only if ``enabled`` is set.

            Otherwise, the snapshot is disabled and there is no run-time overhead.

            The default is to always capture the snapshot unless the interpreter runs in optimized mode (``-O`` or
            ``-OO``).

        """
        self._snapshot = None  # type: Optional[Snapshot]
        self.enabled = enabled

        # Resolve the snapshot only if enabled so that no overhead is incurred
        if enabled:
            self._snapshot = Snapshot(capture=capture, name=name)