Skip to content

The animation engine for agents: blazingly fast math and 3D videos with Manim's Python API, on a Rust GPU core.

Python

Scope preview imports and requests to a source generation

Sina Atalay2191e216f533
diff --git a/src/manimgx/cli/preview.py b/src/manimgx/cli/preview.py
index bef41fde..ee265342 100644
--- a/src/manimgx/cli/preview.py
+++ b/src/manimgx/cli/preview.py
@@ -8,10 +8,16 @@ take replaces the old at the same moment, once it gets there: an edit never lose
error is shown over the last good take.
"""
+import importlib
import sys
import time
import traceback
+from collections.abc import Callable, Iterator, Sequence
+from contextlib import contextmanager
+from importlib.abc import MetaPathFinder
+from importlib.machinery import ModuleSpec, PathFinder, SourceFileLoader
from pathlib import Path
+from types import ModuleType
from typing import Annotated
import typer
@@ -24,6 +30,7 @@ from manimgx.cli.scenes import (
Resolution,
SceneName,
Size,
+ Source,
fail,
load,
pick,
@@ -116,8 +123,6 @@ def run(
makes it out of date. Returns the files to watch: the scene's, and the modules it imported
from beside it."""
root = file.resolve().parent
- for key in [key for key, path in _own(root) if key != "__main__"]:
- del sys.modules[key] # the file's own modules are imported afresh: they change
started = time.perf_counter()
def take(data: bytes) -> None:
@@ -125,45 +130,103 @@ def run(
raise Cut # out of date: run again
window(data)
+ with imports(file, news.watch) as source:
+ try:
+ module = load(file)
+ found = [s.__name__ for s in scenes(module)]
+ # unnamed, the file's first scene: its viewer goes to the others in the window (N, P)
+ kind = pick(
+ file, module, found[0] if name is None and len(found) > 1 else name
+ )
+ window.scenes(found, kind.__name__)
+ look = Format.own(resolution, fps)
+ config.pixel_width, config.pixel_height = look.width, look.height
+ config.frame_rate = look.fps
+ film = kind().render(take=take)
+ cut = "cut: " if news.any or not window.open else ""
+ typer.echo(
+ f"{kind.__name__}: {cut}{film.frame_count} frames in"
+ f" {time.perf_counter() - started:.2f} s",
+ err=True,
+ )
+ except Exception as error:
+ text = str(error) if isinstance(error, Mistake) else report_error(error)
+ typer.echo(text, err=True)
+ own = [
+ f
+ for f in traceback.extract_tb(error.__traceback__)
+ if Path(f.filename).resolve().is_relative_to(root)
+ ]
+ window.failed(
+ error, text, own[-1].lineno if own else getattr(error, "lineno", None)
+ )
+ return list(source.files)
+
+
+class Sources(MetaPathFinder):
+ """The scene-directory imports owned by one authoring generation."""
+
+ def __init__(
+ self, file: Path, host: set[str], watch: Callable[[list[Path]], None] | None
+ ) -> None:
+ self.root = file.resolve().parent
+ self.host = host
+ self.watch = watch
+ self.modules = {file.stem}
+ self.files = {file.resolve()}
+
+ def find_spec(
+ self,
+ fullname: str,
+ path: Sequence[str] | None = None,
+ target: ModuleType | None = None,
+ ) -> ModuleSpec | None:
+ top = fullname.partition(".")[0]
+ if top in self.host and top not in self.modules:
+ return None
+ spec = PathFinder.find_spec(fullname, path, target)
+ if spec is None:
+ return None
+ if "." not in fullname:
+ local = PathFinder.find_spec(fullname, [str(self.root)])
+ if local is None or local.origin != spec.origin:
+ return None
+ if spec.origin is None and not set(
+ local.submodule_search_locations or ()
+ ) & set(spec.submodule_search_locations or ()):
+ return None
+ elif fullname.rpartition(".")[0] not in self.modules:
+ return None
+ self.modules.add(fullname)
+ if isinstance(spec.loader, SourceFileLoader):
+ file = Path(spec.loader.path).absolute()
+ # Ownership follows the import entry, including symlinks and namespace portions.
+ self.files.add(file)
+ if self.watch is not None:
+ self.watch([file])
+ spec.loader = Source(fullname, str(file))
+ return spec
+
+
+@contextmanager
+def imports(
+ file: Path, watch: Callable[[list[Path]], None] | None = None
+) -> Iterator[Sources]:
+ """Load the scene's modules for one run; preserve the process that hosts it."""
+ before, path = dict(sys.modules), sys.path.copy()
+ source = Sources(file, {name.partition(".")[0] for name in before}, watch)
+ sys.meta_path.insert(sys.meta_path.index(PathFinder), source)
+ importlib.invalidate_caches()
try:
- module = load(file)
- found = [s.__name__ for s in scenes(module)]
- # unnamed, the file's first scene: its viewer goes to the others in the window (N, P)
- kind = pick(file, module, found[0] if name is None and len(found) > 1 else name)
- window.scenes(found, kind.__name__)
- look = Format.own(resolution, fps)
- config.pixel_width, config.pixel_height = look.width, look.height
- config.frame_rate = look.fps
- film = kind().render(take=take)
- cut = "cut: " if news.any or not window.open else ""
- typer.echo(
- f"{kind.__name__}: {cut}{film.frame_count} frames in"
- f" {time.perf_counter() - started:.2f} s",
- err=True,
- )
- except Exception as error:
- text = str(error) if isinstance(error, Mistake) else report_error(error)
- typer.echo(text, err=True)
- own = [
- f
- for f in traceback.extract_tb(error.__traceback__)
- if Path(f.filename).resolve().is_relative_to(root)
- ]
- window.failed(
- error, text, own[-1].lineno if own else getattr(error, "lineno", None)
- )
- return [file, *(path for _, path in _own(root) if path != file.resolve())]
-
-
-def _own(root: Path) -> list[tuple[str, Path]]:
- """The modules imported from under `root` (the scene's directory), and their files."""
- return [
- (key, path)
- for key, module in list(sys.modules.items())
- if (where := getattr(module, "__file__", None))
- and (path := Path(where).resolve()).is_relative_to(root)
- and path.suffix == ".py"
- ]
+ yield source
+ finally:
+ sys.meta_path.remove(source)
+ sys.path[:] = path
+ for name in source.modules:
+ if name in before:
+ sys.modules[name] = before[name]
+ else:
+ sys.modules.pop(name, None)
def stamps(paths: list[Path]) -> list[float]:
diff --git a/tests/integration/test_preview.py b/tests/integration/test_preview.py
index df6b71e6..26934255 100644
--- a/tests/integration/test_preview.py
+++ b/tests/integration/test_preview.py
@@ -22,7 +22,7 @@ from manimgx.rendering.film import Cut
# the module: `manimgx.cli.preview` is also the command's name in its package
command = importlib.import_module("manimgx.cli.preview")
-START, NOTE, END = 0, 9, 12
+START, NOTE, END = 14, 9, 12
SCENE = """
import manimgx as m
diff --git a/tests/unit/cli/test_preview.py b/tests/unit/cli/test_preview.py
new file mode 100644
index 00000000..edba60d0
--- /dev/null
+++ b/tests/unit/cli/test_preview.py
@@ -0,0 +1,239 @@
+"""Each preview generation owns its author modules and preserves its hosting process."""
+
+import importlib
+import os
+import py_compile
+import sys
+from pathlib import Path
+from types import ModuleType
+from typing import cast
+from unittest.mock import Mock
+
+import pytest
+
+from manimgx.cli.preview import News, imports
+from manimgx.cli.scenes import load
+from manimgx.rendering.window import Window
+
+
+def write(path: Path, text: str) -> None:
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(text, encoding="utf-8")
+
+
+def test_same_timestamp_helper_edits_bypass_but_preserve_existing_bytecode(
+ tmp_path: Path,
+) -> None:
+ entry, helper = tmp_path / "entry.py", tmp_path / "nearby.py"
+ write(entry, "from nearby import value\n")
+ write(helper, "value = 'one'\n")
+ stamp = helper.stat()
+ cached = py_compile.compile(str(helper), doraise=True)
+ assert cached is not None
+ bytecode = Path(cached)
+ original = bytecode.read_bytes()
+ before = sys.meta_path.copy(), sys.path.copy()
+ for value in ("two", "six"):
+ write(helper, f"value = '{value}'\n")
+ os.utime(helper, ns=(stamp.st_atime_ns, stamp.st_mtime_ns))
+ with imports(entry) as source:
+ assert load(entry).value == value
+ assert source.files == {entry, helper}
+ assert "nearby" not in sys.modules
+ assert "entry" not in sys.modules
+ assert bytecode.read_bytes() == original
+ assert (sys.meta_path, sys.path) == before
+
+
+def test_relative_package_imports_use_current_encoded_source_and_resources(
+ tmp_path: Path,
+) -> None:
+ entry = tmp_path / "entry.py"
+ write(entry, "from own_package import value\n")
+ write(tmp_path / "own_package/__init__.py", "from .part import value\n")
+ part = tmp_path / "own_package/part.py"
+ part.write_bytes(
+ b"# coding: latin-1\nfrom importlib.resources import files\n"
+ b"value = 'caf\xe9' + files(__package__).joinpath('note.txt').read_text(encoding='utf-8')\n"
+ )
+ write(tmp_path / "own_package/note.txt", "!")
+ with imports(entry) as source:
+ assert load(entry).value == "café!"
+ assert part in source.files
+ assert not (tmp_path / "own_package/__pycache__").exists()
+ assert "own_package" not in sys.modules
+ assert "own_package.part" not in sys.modules
+ entry.write_bytes(b"\xef\xbb\xbfvalue = 'bom'\n")
+ with imports(entry):
+ assert load(entry).value == "bom"
+
+
[email protected]("failure", ["entry", "compile", "execute", "render"])
+def test_failed_generations_restore_host_state_and_keep_source_paths(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, failure: str
+) -> None:
+ entry, helper = tmp_path / "entry.py", tmp_path / "broken.py"
+ previous = ModuleType("entry")
+ monkeypatch.setitem(sys.modules, "entry", previous)
+ write(entry, "!\n" if failure == "entry" else "import broken\n")
+ write(
+ helper,
+ "!\n"
+ if failure == "compile"
+ else "raise RuntimeError('execution')\n"
+ if failure == "execute"
+ else "value = 1\n",
+ )
+ before = sys.meta_path.copy(), sys.path.copy()
+
+ def render() -> None:
+ load(entry)
+ raise RuntimeError("render")
+
+ with pytest.raises((SyntaxError, RuntimeError)), imports(entry) as source:
+ render()
+ assert entry in source.files
+ if failure != "entry":
+ assert helper in source.files
+ assert sys.modules["entry"] is previous
+ assert "broken" not in sys.modules
+ assert (sys.meta_path, sys.path) == before
+
+
+def test_host_packages_and_new_installed_dependencies_keep_their_identity(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ entry = tmp_path / "entry.py"
+ write(tmp_path / "resident/__init__.py", "class Base: pass\n")
+ write(tmp_path / "resident/lazy.py", "class Child: pass\n")
+ site = tmp_path / ".venv/lib/site-packages"
+ write(site / "dependency.py", "class Base: pass\n")
+ monkeypatch.syspath_prepend(str(site))
+ monkeypatch.syspath_prepend(str(tmp_path))
+ resident = importlib.import_module("resident")
+ write(entry, "import resident, resident.lazy, dependency\n")
+ try:
+ for _ in range(2):
+ with imports(entry) as source:
+ module = load(entry)
+ assert module.resident is resident
+ assert source.files == {entry}
+ assert sys.modules["resident"] is resident
+ assert sys.modules["resident.lazy"] is module.resident.lazy
+ assert sys.modules["dependency"] is module.dependency
+ finally:
+ for name in ("resident", "resident.lazy", "dependency"):
+ sys.modules.pop(name, None)
+
+
+def test_package_resolution_and_circular_imports_stay_with_python(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ entry = tmp_path / "entry.py"
+ external = tmp_path / "installed"
+ write(external / "shared/elsewhere.py", "value = 19\n")
+ write(tmp_path / "shared/nearby.py", "value = 23\n")
+ write(tmp_path / "cycle_a.py", "value = 2\nimport cycle_b\n")
+ write(tmp_path / "cycle_b.py", "import cycle_a\nvalue = cycle_a.value + 1\n")
+ write(
+ entry,
+ "import shared.elsewhere, shared.nearby, cycle_a\n"
+ "value = shared.elsewhere.value + shared.nearby.value + cycle_a.cycle_b.value\n",
+ )
+ monkeypatch.syspath_prepend(str(external))
+ for _ in range(2):
+ with imports(entry):
+ assert load(entry).value == 45
+ assert all(
+ name not in sys.modules
+ for name in (
+ "shared",
+ "shared.elsewhere",
+ "shared.nearby",
+ "cycle_a",
+ "cycle_b",
+ )
+ )
+
+
+def test_a_helper_edit_during_the_first_run_is_observed(tmp_path: Path) -> None:
+ entry, helper = tmp_path / "entry.py", tmp_path / "fresh_helper.py"
+ write(entry, "import fresh_helper\n")
+ write(helper, "value = 1\n")
+ window = cast("Window", Mock(spec=Window, asked=lambda: None))
+ news = News([entry], window)
+ with imports(entry, news.watch):
+ load(entry)
+ stamp = helper.stat().st_mtime_ns
+ write(helper, "value = 2\n")
+ os.utime(helper, ns=(stamp + 1_000_000_000, stamp + 1_000_000_000))
+ news.looked = 0 # Observe the saved edit at the next polling opportunity.
+ assert news()
+
+
+def test_explicit_entry_reload_uses_current_source_and_restores_its_host_name(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ entry = tmp_path / "entry.py"
+ previous = ModuleType("entry")
+ monkeypatch.setitem(sys.modules, "entry", previous)
+ write(entry, "value = 1\n")
+ with imports(entry):
+ module = load(entry)
+ write(entry, "value = 2\n")
+ assert importlib.reload(module).value == 2
+ assert sys.modules["entry"] is previous
+
+
+def test_a_symlinked_author_helper_uses_current_source_and_is_watched(
+ tmp_path: Path,
+) -> None:
+ root = tmp_path / "project"
+ entry, helper = root / "entry.py", root / "linked_helper.py"
+ target = tmp_path / "shared.py"
+ write(entry, "from linked_helper import value\n")
+ write(target, "value = 'one'\n")
+ try:
+ helper.symlink_to(target)
+ except OSError as error:
+ if getattr(error, "winerror", None) == 1314:
+ pytest.skip("this Windows account cannot create symbolic links")
+ raise
+ stamp = helper.stat()
+ py_compile.compile(str(helper), doraise=True)
+ write(target, "value = 'two'\n")
+ os.utime(target, ns=(stamp.st_atime_ns, stamp.st_mtime_ns))
+ with imports(entry) as source:
+ assert load(entry).value == "two"
+ assert helper in source.files
+
+
+def test_mixed_namespace_children_reload_together_across_source_roots(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ root, external = tmp_path / "project", tmp_path / "external"
+ entry = root / "entry.py"
+ helper = external / "shared_namespace/elsewhere.py"
+ write(helper, "value = 19\n")
+ stamp = helper.stat()
+ write(root / "shared_namespace/nearby.py", "value = 23\n")
+ write(
+ entry,
+ "import shared_namespace.elsewhere, shared_namespace.nearby\n"
+ "value = shared_namespace.elsewhere.value + shared_namespace.nearby.value\n",
+ )
+ monkeypatch.syspath_prepend(str(external))
+ for value in (19, 20):
+ write(helper, f"value = {value}\n")
+ os.utime(helper, ns=(stamp.st_atime_ns, stamp.st_mtime_ns))
+ with imports(entry) as source:
+ assert load(entry).value == value + 23
+ assert all(
+ name not in sys.modules
+ for name in (
+ "shared_namespace",
+ "shared_namespace.elsewhere",
+ "shared_namespace.nearby",
+ )
+ )
+ assert helper in source.files