`MockPrintFunction` calls `inspect.stack()` on every `print`, which scans `sys.modules` once per frame

Open Beginner friendly
#102 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
testing-qa

Research direction

Read MockPrintFunction.__call__ in pytest_examples/run_code.py and check how include_print callbacks use the FrameInfo fields. The issue proposes getting the caller with sys._getframe and building a FrameInfo without source context; verify the change against the reported slow example or the relevant tests. Done means callbacks still receive a FrameInfo and prints avoid repeated module scans.

Written by the indexing model from the issue text.

Description

This issue was posted by Claude Code using claude-opus-5-5 on behalf of David.

Each mocked print costs time proportional to len(sys.modules), times the stack frames whose file belongs to no module. In pydantic-ai's docs tests this made one example the slowest of 1,562 and pushed it over an 8 s per-test budget in CI (pydantic/pydantic-ai#14915).

Cause. MockPrintFunction.__call__ calls inspect.stack() to read one frame. inspect.stack() looks up the source of every frame on the stack. For a frame whose file maps to no module, such as each <frozen importlib._bootstrap> frame of an import the example runs, inspect.getmodule loops over all of sys.modules. It does not cache the miss, so every print repeats the loop.

Numbers (pytest-examples 0.0.18, CPython 3.15, one example whose prints run inside nested imports):

  • cProfile: 7 prints took 2.22 s, in 234 full sys.modules scans. The example's own code took 0.33 s.
  • Call time with sys.modules padded to a given size:
sys.modules inspect.stack() fix below
3,226 1.27 s 1.02 s
30,077 2.22 s 1.06 s
250,077 14.20 s 1.56 s

Fix. Take the caller with sys._getframe and build the FrameInfo without source context. include_print callbacks still receive a FrameInfo; its code_context and index become None.

Diff
--- a/pytest_examples/run_code.py
+++ b/pytest_examples/run_code.py
@@ class MockPrintFunction:
     def __call__(self, *args: Any, sep: str = ' ', **kwargs: Any) -> None:
-        frame = inspect.stack()[parent_frame_id]
+        caller = sys._getframe(parent_frame_id)
+        frame = inspect.FrameInfo(caller, caller.f_code.co_filename, caller.f_lineno, caller.f_code.co_name, None, None)

         if self._include_file(frame, args):
Dominant language
Python
Stars
181
Forks
31
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pydantic/pytest-examples

All issues in pydantic/pytest-examples

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.