diff --git a/src/e3/anod/loader.py b/src/e3/anod/loader.py index 1fa43744..4c91a724 100644 --- a/src/e3/anod/loader.py +++ b/src/e3/anod/loader.py @@ -277,9 +277,13 @@ def spec(name: str) -> Callable[..., Anod]: :param name: name of the spec to load """ spec_repository: AnodSpecRepository | None = None - for k in inspect.stack()[1:]: - if "__spec_repository" in k[0].f_globals: - spec_repository = k[0].f_globals["__spec_repository"] + + # Implementation note: context=0 means that the no source context is + # computed for each frame. This improve drastically the performance + # as it avoids reading the source file for each frame. + for k in inspect.stack(context=0)[1:]: + spec_repository = k[0].f_globals.get("__spec_repository") + if spec_repository is not None: break assert spec_repository is not None diff --git a/src/e3/electrolyt/plan.py b/src/e3/electrolyt/plan.py index 9108ca20..24631ce0 100644 --- a/src/e3/electrolyt/plan.py +++ b/src/e3/electrolyt/plan.py @@ -292,7 +292,12 @@ def _add_action(self, name: str, *args: Any, **kwargs: Any) -> None: plan_line = "unknown filename:unknown lineno" # Retrieve the plan line try: - caller_frames = inspect.getouterframes(frame=inspect.currentframe()) + # Implementation note: context=0 means that the no source context is + # computed for each frame. This improve drastically the performance + # as it avoids reading the source file for each frame. + caller_frames = inspect.getouterframes( + frame=inspect.currentframe(), context=0 + ) caller_frames_in_plan = [] for frame in caller_frames: frame_filename = frame.filename