def _format_result_output(
        self,
        result: object,
        *,
        reformat: bool,
        highlight: bool,
        line_length: int,
        paginate: bool,
    ) -> Generator[OneStyleAndTextTuple, None, None]:
        """
        Format __repr__ for an `eval` result.

        Note: this can raise `KeyboardInterrupt` if either calling `__repr__`,
              `__pt_repr__` or formatting the output with "Black" takes to long
              and the user presses Control-C.
        """
        # If __pt_repr__ is present, take this. This can return prompt_toolkit
        # formatted text.
        try:
            if hasattr(result, "__pt_repr__"):
                formatted_result_repr = to_formatted_text(
                    getattr(result, "__pt_repr__")()
                )
                yield from formatted_result_repr
                return
        except (GeneratorExit, KeyboardInterrupt):
            raise  # Don't catch here.
        except:
            # For bad code, `__getattr__` can raise something that's not an
            # `AttributeError`. This happens already when calling `hasattr()`.
            pass

        # Call `__repr__` of given object first, to turn it in a string.
        try:
            result_repr = repr(result)
        except KeyboardInterrupt:
            raise  # Don't catch here.
        except BaseException as e:
            # Calling repr failed.
            self.display_exception(e, highlight=highlight, paginate=paginate)
            return

        # Determine whether it's valid Python code. If not,
        # reformatting/highlighting won't be applied.
        if len(result_repr) < MAX_REFORMAT_SIZE:
            try:
                compile(result_repr, "", "eval")
            except SyntaxError:
                valid_python = False
            else:
                valid_python = True
        else:
            valid_python = False

        if valid_python and reformat:
            # Inline import. Slightly speed up start-up time if black is
            # not used.
            try:
                import black

                if not hasattr(black, "Mode"):
                    raise ImportError
            except ImportError:
                pass  # no Black package in your installation
            else:
                result_repr = black.format_str(
                    result_repr,
                    mode=black.Mode(line_length=line_length),
                )

        if valid_python and highlight:
            yield from _lex_python_result(result_repr)
        else:
            yield ("", result_repr)

        Format __repr__ for an `eval` result.

        Note: this can raise `KeyboardInterrupt` if either calling `__repr__`,
              `__pt_repr__` or formatting the output with "Black" takes to long
              and the user presses Control-C.
        
src/ptpython/printer.py