A community-maintained Python framework for creating mathematical animations.
Output errors to stderr (#1570)
friedkeenan1b32bce0c21c
diff --git a/manim/_config/__init__.py b/manim/_config/__init__.pyindex 75931efb8..f534441d7 100644--- a/manim/_config/__init__.py+++ b/manim/_config/__init__.py@@ -10,6 +10,7 @@ from .utils import ManimConfig, ManimFrame, make_config_parser__all__ = ["logger","console",+ "error_console","config","frame","tempconfig",@@ -20,7 +21,10 @@ parser = make_config_parser()# The logger can be accessed from anywhere as manim.logger, or as# logging.getLogger("manim"). The console must be accessed as manim.console.# Throughout the codebase, use manim.console.print() instead of print().-logger, console = make_logger(parser["logger"], parser["CLI"]["verbosity"])+# Use error_console to print errors so that it outputs to stderr.+logger, console, error_console = make_logger(+ parser["logger"], parser["CLI"]["verbosity"]+)# TODO: temporary to have a clean terminal output when working with PIL or matplotliblogging.getLogger("PIL").setLevel(logging.INFO)logging.getLogger("matplotlib").setLevel(logging.INFO)diff --git a/manim/_config/logger_utils.py b/manim/_config/logger_utils.pyindex 493337367..effa61c00 100644--- a/manim/_config/logger_utils.py+++ b/manim/_config/logger_utils.py@@ -14,6 +14,7 @@ import copyimport jsonimport loggingimport os+import sysimport typingfrom typing import TYPE_CHECKING@@ -62,9 +63,10 @@ def make_logger(Returns-------- :class:`logging.Logger`, :class:`rich.Console`- The manim logger and console. Both use the theme returned by- :func:`parse_theme`+ :class:`logging.Logger`, :class:`rich.Console`, :class:`rich.Console`+ The manim logger and consoles. The first console outputs+ to stdout, the second to stderr. All use the theme returned by+ :func:`parse_theme`.See Also--------@@ -80,6 +82,9 @@ def make_logger(theme = parse_theme(parser)console = Console(theme=theme)+ # With rich 9.5.0+ we could pass stderr=True instead+ error_console = Console(theme=theme, file=sys.stderr)+# set the rich handlerRichHandler.KEYWORDS = HIGHLIGHTED_KEYWORDSrich_handler = RichHandler(@@ -91,7 +96,7 @@ def make_logger(logger.addHandler(rich_handler)logger.setLevel(verbosity)- return logger, console+ return logger, console, error_consoledef parse_theme(parser: configparser.ConfigParser) -> Theme:diff --git a/manim/cli/render/commands.py b/manim/cli/render/commands.pyindex 97de34353..b8ea5fecd 100644--- a/manim/cli/render/commands.py+++ b/manim/cli/render/commands.py@@ -14,7 +14,7 @@ import clickimport cloupimport requests-from ... import __version__, config, console, logger+from ... import __version__, config, console, error_console, loggerfrom ...constants import CONTEXT_SETTINGS, EPILOGfrom ...utils.module_ops import scene_classes_from_filefrom .ease_of_access_options import ease_of_access_options@@ -134,7 +134,8 @@ def render(else:breakexcept Exception:- console.print_exception()+ error_console.print_exception()+ sys.exit(1)elif config.renderer == "webgl":try:from manim.grpc.impl import frame_server_impl@@ -147,14 +148,16 @@ def render("Dependencies for the WebGL render are missing. Run ""pip install manim[webgl_renderer] to install them.")- console.print_exception()+ error_console.print_exception()+ sys.exit(1)else:for SceneClass in scene_classes_from_file(file):try:scene = SceneClass()scene.render()except Exception:- console.print_exception()+ error_console.print_exception()+ sys.exit(1)if config.notify_outdated_version:manim_info_url = "https://pypi.org/pypi/manim/json"diff --git a/tests/test_logging/basic_scenes_error.py b/tests/test_logging/basic_scenes_error.pynew file mode 100644index 000000000..5a4bda080--- /dev/null+++ b/tests/test_logging/basic_scenes_error.py@@ -0,0 +1,8 @@+from manim import *++# This module is intended to raise an error.+++class Error(Scene):+ def construct(self):+ raise Exception("An error has occurred")diff --git a/tests/test_logging/test_logging.py b/tests/test_logging/test_logging.pyindex 73346f333..22368bc53 100644--- a/tests/test_logging/test_logging.py+++ b/tests/test_logging/test_logging.py@@ -1,5 +1,6 @@import osimport re+from pathlib import Pathfrom ..utils.commands import capturefrom ..utils.logging_tester import *@@ -52,3 +53,20 @@ def test_logging_when_scene_is_not_specified(tmp_path, python_version):]_, err, exitcode = capture(command)assert exitcode == 0, err+++def test_error_logging(tmp_path, python_version):+ path_error_scene = Path("tests/test_logging/basic_scenes_error.py")++ command = [+ python_version,+ "-m",+ "manim",+ "-ql",+ "--media_dir",+ str(tmp_path),+ str(path_error_scene),+ ]++ _, err, exitcode = capture(command)+ assert exitcode != 0 and len(err) > 0