Ejemplo n.º 1
0
def parse_warning_filter(
        arg: str, *, escape: bool
) -> tuple[warnings._ActionKind, str, type[Warning], str, int]:
    """Parse a warnings filter string.

    This is copied from warnings._setoption with the following changes:

    - Does not apply the filter.
    - Escaping is optional.
    - Raises UsageError so we get nice error messages on failure.

    """
    __tracebackhide__ = True
    error_template = textwrap.dedent(f"""\
        while parsing the following warning configuration:
          {arg}
        This error occurred:
        {{error}}
        """)

    parts = arg.split(":")
    if len(parts) > 5:
        doc_url = (
            "https://docs.python.org/3/library/warnings.html#describing-warning-filters"
        )
        error = textwrap.dedent(f"""\
            Too many fields ({len(parts)}), expected at most 5 separated by colons:
              action:message:category:module:line
            For more information please consult: {doc_url}
            """)
        raise Exit(error_template.format(error=error))

    while len(parts) < 5:
        parts.append("")
    action_, message, category_, module, lineno_ = (s.strip() for s in parts)
    try:
        action: warnings._ActionKind = warnings._getaction(
            action_)  # type: ignore
    except warnings._OptionError as e:
        raise Exit(error_template.format(error=str(e)))
    try:
        category: type[Warning] = _resolve_warning_category(category_)
    except Exit as e:
        raise Exit(str(e))
    if message and escape:
        message = re.escape(message)
    if module and escape:
        module = re.escape(module) + r"\Z"
    if lineno_:
        try:
            lineno = int(lineno_)
            if lineno < 0:
                raise ValueError("number is negative")
        except ValueError as e:
            raise Exit(
                error_template.format(
                    error=f"invalid lineno {lineno_!r}: {e}"))
    else:
        lineno = 0
    return action, message, category, module, lineno
Ejemplo n.º 2
0
def _parse_filter(
    arg: str, *, escape: bool
) -> "Tuple[str, str, Type[Warning], str, int]":
    """Parse a warnings filter string.

    This is copied from warnings._setoption, but does not apply the filter,
    only parses it, and makes the escaping optional.
    """
    parts = arg.split(":")
    if len(parts) > 5:
        raise warnings._OptionError("too many fields (max 5): {!r}".format(arg))
    while len(parts) < 5:
        parts.append("")
    action_, message, category_, module, lineno_ = [s.strip() for s in parts]
    action = warnings._getaction(action_)  # type: str # type: ignore[attr-defined]
    category = warnings._getcategory(
        category_
    )  # type: Type[Warning] # type: ignore[attr-defined]
    if message and escape:
        message = re.escape(message)
    if module and escape:
        module = re.escape(module) + r"\Z"
    if lineno_:
        try:
            lineno = int(lineno_)
            if lineno < 0:
                raise ValueError
        except (ValueError, OverflowError) as e:
            raise warnings._OptionError("invalid lineno {!r}".format(lineno_)) from e
    else:
        lineno = 0
    return (action, message, category, module, lineno)
Ejemplo n.º 3
0
 def update_event(self, inp=-1):
     self.set_output_val(0, warnings._getaction(self.input(0)))