`MockPrintFunction` calls `inspect.stack()` on every `print`, which scans `sys.modules` once per frame
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.modulesscans. The example's own code took 0.33 s. - Call time with
sys.modulespadded 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from pydantic/pytest-examples
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
pydantic/pytest-examples#87 ·
-
Clean up NeededPossibly taken @Danipulok claimed this 86 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
pydantic/pytest-examples#72 · 5 comments ·
-
Updating multiline output can insert invalid Python quote delimitersPossibly taken @vinsymoyya claimed this 13 days ago. Open
Difficulty 3/5 Half a day Newbie friendliness 45/100
pydantic/pytest-examples#98 · 1 comment ·
-
Print checking swallows writes to explicit output streamsPossibly taken @vinsymoyya claimed this 13 days ago. Open
Difficulty 3/5 Half a day Newbie friendliness 22/100
pydantic/pytest-examples#96 · 1 comment ·
-
Inline backticks truncate fenced Python examplesPossibly taken @vinsymoyya claimed this 13 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 25/100
pydantic/pytest-examples#94 · 1 comment ·
All issues in pydantic/pytest-examples
Similar issues
-
ui
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
ARTPARK-SAHAI-ORG/calibrate-frontend#919 ·
Maintainers usually reply within 1 day
-
CI: MSYS2 jobs fail — mingw python3 package renamed upstream to pythonPossibly taken @ronaldtse claimed this today. Open
Difficulty 1/5 Under an hour Newbie friendliness 95/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
huggingface/lighteval#1534 ·
Maintainers usually reply within 1 day