Skip to content

Commit

Permalink
improve io mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
mhils committed Oct 11, 2024
1 parent 308c3a1 commit 7270fd1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Inherited members are now hidden by default if the base class is not part of the documentation.
Please make yourself heard in https://github.com/mitmproxy/pdoc/issues/715 if you relied on the old behavior.
([#748](https://github.com/mitmproxy/pdoc/pull/748), @mhils)
- Improve mocking of `sys.stdin`, `sys.stdout`, and `sys.stderr` to fix runtime errors with some packages.

## 2024-09-11: pdoc 14.7.0

Expand Down
6 changes: 3 additions & 3 deletions pdoc/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ def mock_some_common_side_effects():
with (
patch("subprocess.Popen", new=_PdocDefusedPopen),
patch("os.startfile", new=_noop, create=True),
patch("sys.stdout", new=io.StringIO()),
patch("sys.stderr", new=io.StringIO()),
patch("sys.stdin", new=io.StringIO()),
patch("sys.stdout", new=io.TextIOWrapper(io.BytesIO())),
patch("sys.stderr", new=io.TextIOWrapper(io.BytesIO())),
patch("sys.stdin", new=io.TextIOWrapper(io.BytesIO())),
):
yield

Expand Down
8 changes: 8 additions & 0 deletions test/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest

from pdoc.extract import invalidate_caches
from pdoc.extract import mock_some_common_side_effects
from pdoc.extract import module_mtime
from pdoc.extract import parse_spec
from pdoc.extract import walk_specs
Expand Down Expand Up @@ -137,3 +138,10 @@ def raise_(*_):
monkeypatch.setattr(importlib, "reload", raise_)
with pytest.warns(UserWarning, match="Error reloading"):
invalidate_caches("pdoc.render_helpers")


def test_mock_sideeffects():
"""https://github.com/mitmproxy/pdoc/issues/745"""
with mock_some_common_side_effects():
import sys
sys.stdout.reconfigure(encoding='utf-8')

0 comments on commit 7270fd1

Please sign in to comment.