manim icon indicating copy to clipboard operation
manim copied to clipboard

Module exec leads to crash

Open Splines opened this issue 10 months ago • 2 comments

The following scene fails on startup:

from dataclasses import dataclass
from manimlib import *


@dataclass
class Coordinates2D:
    x: float
    y: float


class MyScene(Scene):
    def construct(self):
        print("Hello, world!")

Start it via

manimgl "./path/to/bug-test-file.py" MyScene -e 13

and you get the following traceback (Python version: 3.12.8):

Traceback (most recent call last):
  File "/home/dominic/dev/splience-manim-videos/venv/bin/manimgl", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/dominic/dev/manim-fork/manimlib/__main__.py", line 26, in main
    reload_manager.run()
  File "/home/dominic/dev/manim-fork/manimlib/reload_manager.py", line 47, in run
    self.retrieve_scenes_and_run()
  File "/home/dominic/dev/manim-fork/manimlib/reload_manager.py", line 81, in retrieve_scenes_and_run
    scenes = manimlib.extract_scene.main(scene_config, run_config)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/dominic/dev/manim-fork/manimlib/extract_scene.py", line 173, in main
    module = get_scene_module(
             ^^^^^^^^^^^^^^^^^
  File "/home/dominic/dev/manim-fork/manimlib/extract_scene.py", line 168, in get_scene_module
    insert_embed_line_to_module(module, embed_line)
  File "/home/dominic/dev/manim-fork/manimlib/extract_scene.py", line 162, in insert_embed_line_to_module
    exec(code_object, module.__dict__)
  File ".home.dominic.dev.splience-manim-videos.manimbug", line 5, in <module>
  File "/usr/lib/python3.12/dataclasses.py", line 1275, in dataclass
    return wrap(cls)
           ^^^^^^^^^
  File "/usr/lib/python3.12/dataclasses.py", line 1265, in wrap
    return _process_class(cls, init, repr, eq, order, unsafe_hash,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/dataclasses.py", line 983, in _process_class
    and _is_type(type, cls, dataclasses, dataclasses.KW_ONLY,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/dataclasses.py", line 749, in _is_type
    ns = sys.modules.get(cls.__module__).__dict__
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute '__dict__'. Did you mean: '__dir__'?

The latest commit where it works correctly is df1e0674. Then, in the next commit 1a14a6bd it doesn't work anymore. Thus, #2262 has introduced this bug. It's not by chance that this PR is called "Refactor scene creation".

Splines avatar Feb 04 '25 15:02 Splines

Relevant Python docs for the failing lines:

Splines avatar Feb 04 '25 15:02 Splines

If I manually add the module to sys beforehand, it works for me:

sys.modules[module.__name__] = module

See this ChatGPT session.


This could potentially lead to unintended side-effects when users call their scenes like internal Manim modules, so maybe there is a better solution.

Splines avatar Feb 04 '25 16:02 Splines