From ba6e199b037c8c324cc908dc630914254e92f727 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Fri, 11 Oct 2024 16:47:45 +0200 Subject: [PATCH] improve io mocking (#751) --- CHANGELOG.md | 2 ++ pdoc/extract.py | 6 +++--- test/test_extract.py | 9 +++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e51a6a9..c085743d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ - 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. + ([#751](https://github.com/mitmproxy/pdoc/pull/751), @mhils) ## 2024-09-11: pdoc 14.7.0 diff --git a/pdoc/extract.py b/pdoc/extract.py index d01a8a28..fa9f2524 100644 --- a/pdoc/extract.py +++ b/pdoc/extract.py @@ -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 diff --git a/test/test_extract.py b/test/test_extract.py index a2f3f385..639b3ab5 100644 --- a/test/test_extract.py +++ b/test/test_extract.py @@ -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 @@ -137,3 +138,11 @@ 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")