Skip to content

Commit fda0362

Browse files
Copilotxadupre
andauthored
Add :hide_err: option to suppress [runpythonerror] in runpython output
Agent-Logs-Url: https://github.com/sdpython/sphinx-runpython/sessions/398ae408-14c9-4d08-b0cf-259ba560c7bb Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com>
1 parent c35d206 commit fda0362

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

_unittests/ut_runpython/test_runpython_extension.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,35 @@ def depart_rp_node(self, node):
218218
if t1 not in html:
219219
raise AssertionError(html)
220220

221+
def test_runpython_hide_err(self):
222+
"""
223+
Test that :hide_err: suppresses [runpythonerror] from the output.
224+
"""
225+
if "enable_disabled_documented_pieces_of_code" in sys.__dict__:
226+
raise AssertionError("this case should not be")
227+
228+
content = """
229+
test a directive
230+
================
231+
232+
.. runpython::
233+
:rst:
234+
:hide_err:
235+
236+
import warnings
237+
warnings.warn("deprecated", DeprecationWarning)
238+
print("output line")
239+
""".replace(" ", "")
240+
241+
html = rst2html(content, writer_name="rst")
242+
243+
if "runpythonerror" in html:
244+
raise AssertionError(f"[runpythonerror] should not appear in output:\n{html}")
245+
if "DeprecationWarning" in html:
246+
raise AssertionError(f"DeprecationWarning should not appear in output:\n{html}")
247+
if "output line" not in html:
248+
raise AssertionError(f"stdout should still appear in output:\n{html}")
249+
221250
def test_runpython_raw(self):
222251
"""
223252
this test also test the extension runpython

sphinx_runpython/runpython/sphinx_runpython_extension.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,10 @@ class RunPythonDirective(Directive):
559559
the script to be stored somewhere in order to retrieve it.
560560
* ``:debug:`` if ``:rst:`` is used, it shows some information to help
561561
debugging.
562+
* ``:hide_err:`` if present, hides any error or warning output that would
563+
otherwise be appended as ``[runpythonerror]`` in the content. This is
564+
useful when the code produces stderr output (such as deprecation warnings)
565+
that should not appear in the rendered documentation.
562566
563567
Option *rst* can be used the following way::
564568
@@ -630,6 +634,7 @@ class RunPythonDirective(Directive):
630634
"store_in_file": directives.unchanged,
631635
"linenos": directives.unchanged,
632636
"debug": directives.unchanged,
637+
"hide_err": directives.unchanged,
633638
}
634639
has_content = True
635640
runpython_class = runpython_node
@@ -693,6 +698,7 @@ def run(self):
693698
"store": "store" in self.options and self.options["store"] in bool_set_,
694699
"restore": "restore" in self.options
695700
and self.options["restore"] in bool_set_,
701+
"hide_err": "hide_err" in self.options,
696702
}
697703

698704
if p["setsysvar"] is not None and len(p["setsysvar"]) == 0:
@@ -821,7 +827,7 @@ def run(self):
821827
if err is not None:
822828
err = err.rstrip(" \n\r\t")
823829
content = out
824-
if len(err) > 0:
830+
if len(err) > 0 and not p["hide_err"]:
825831
content += "\n[runpythonerror]\n" + err
826832

827833
# add member

0 commit comments

Comments
 (0)