コード例 #1
0
 def orig_showwarning(message,
                      category,
                      filename,
                      lineno,
                      file=None,
                      line=None):
     msg = warnings.WarningMessage(message, category, filename,
                                   lineno, file, line)
     warnings._showwarnmsg_impl(msg)
コード例 #2
0
def _showwarning_chain(message,
                       category,
                       filename,
                       lineno,
                       file=None,
                       line=None,
                       chain=None):
    """Hook to write a warning to a file; replace if you like."""
    if category is SamplingProgess and chain is not None:
        print("CHAIN #{}: ".format(chain) + str(message))
    else:
        msg = WarningMessage(message, category, filename, lineno, file, line)
        _showwarnmsg_impl(msg)
コード例 #3
0
def showwarning_with_traceback(message,
                               category,
                               filename,
                               lineno,
                               file=None,
                               line=None):
    msg = warnings.WarningMessage(message, category, filename, lineno, file,
                                  line)

    msg.formatted_traceback = traceback.format_stack()
    msg.traceback = traceback.extract_stack()

    if hasattr(warnings, "_showwarnmsg_impl"):
        warnings._showwarnmsg_impl(msg)
    else:  # python 2
        warnings._show_warning(message, category, filename, lineno, file, line)
コード例 #4
0
    def showwarning(
        self,
        message: Warning,
        category: Type[Warning],
        filename: str,
        lineno: int,
        file: Optional[IO] = None,
        line: Optional[str] = None,
    ) -> None:
        """
        Hook to write a warning to a file using the built-in `warnings` lib.

        In [the documentation](https://docs.python.org/3/library/warnings.html)
        for the built-in `warnings` library, there are a few recommended ways of
        customizing the printing of warning messages.

        This method can override the `warnings.showwarning` function,
        which is called as part of the `warnings` library's workflow to print
        warning messages, e.g., when using `warnings.warn()`.
        Originally, it prints warning messages to `stderr`.
        This method will also print warning messages to `stderr` by calling
        `warnings._showwarning_orig()` or `warnings._showwarnmsg_impl()`.
        The first function will be called if the issued warning is not recognized
        as an [`OptimadeWarning`][optimade.server.warnings.OptimadeWarning].
        This is equivalent to "standard behaviour".
        The second function will be called _after_ an
        [`OptimadeWarning`][optimade.server.warnings.OptimadeWarning] has been handled.

        An [`OptimadeWarning`][optimade.server.warnings.OptimadeWarning] will be
        translated into an OPTIMADE Warnings JSON object in accordance with
        [the specification](https://github.com/Materials-Consortia/OPTIMADE/blob/v1.0.0/optimade.rst#json-response-schema-common-fields).
        This process is similar to the [Exception handlers][optimade.server.exception_handlers].

        Parameters:
            message: The `Warning` object to show and possibly handle.
            category: `Warning` type being warned about. This amounts to `type(message)`.
            filename: Name of the file, where the warning was issued.
            lineno: Line number in the file, where the warning was issued.
            file: A file-like object to which the warning should be written.
            line: Source content of the line that issued the warning.

        """
        assert isinstance(
            message, Warning
        ), "'message' is expected to be a Warning or subclass thereof."

        if not isinstance(message, OptimadeWarning):
            # If the Warning is not an OptimadeWarning or subclass thereof,
            # use the regular 'showwarning' function.
            warnings._showwarning_orig(message, category, filename, lineno,
                                       file, line)
            return

        # Format warning
        try:
            title = str(message.title)
        except AttributeError:
            title = str(message.__class__.__name__)

        try:
            detail = str(message.detail)
        except AttributeError:
            detail = str(message)

        if CONFIG.debug:
            if line is None:
                # All this is taken directly from the warnings library.
                # See 'warnings._formatwarnmsg_impl()' for the original code.
                try:
                    import linecache

                    line = linecache.getline(filename, lineno)
                except Exception:
                    # When a warning is logged during Python shutdown, linecache
                    # and the import machinery don't work anymore
                    line = None
                    linecache = None
            meta = {
                "filename": filename,
                "lineno": lineno,
            }
            if line:
                meta["line"] = line.strip()

        if CONFIG.debug:
            new_warning = Warnings(title=title, detail=detail, meta=meta)
        else:
            new_warning = Warnings(title=title, detail=detail)

        # Add new warning to self._warnings
        self._warnings.append(new_warning.dict(exclude_unset=True))

        # Show warning message as normal in sys.stderr
        warnings._showwarnmsg_impl(
            warnings.WarningMessage(message, category, filename, lineno, file,
                                    line))
コード例 #5
0
ファイル: nodes.py プロジェクト: xxoolm/Ryven
 def update_event(self, inp=-1):
     self.set_output_val(0, warnings._showwarnmsg_impl(self.input(0)))