Visitar URL original
Add `:hide_err:` option to suppress `[runpythonerror]` from runpython… · sdpython/sphinx-runpython@45e0ff9 · GitHub
Skip to content

Commit 45e0ff9

Browse files
Copilotxadupre
andauthored
Add :hide_err: option to suppress [runpythonerror] from runpython output (#56)
* Initial plan * 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> * fix * fix * fix --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com> Co-authored-by: Xavier Dupré <xadupre@microsoft.com>
1 parent a8e6cdb commit 45e0ff9

5 files changed

Lines changed: 43 additions & 4 deletions

File tree

‎CHANGELOGS.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Change Logs
22
===========
33

4+
0.4.3
5+
+++++
6+
7+
* :pr:`56`: Add `:hide_err:` option to suppress `[runpythonerror]` from runpython output
8+
49
0.4.2
510
+++++
611

‎_doc/index.rst‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ Sources available on
5454
Older versions
5555
++++++++++++++
5656

57+
* `0.4.3 <../v0.4.3/index.html>`_
5758
* `0.4.2 <../v0.4.2/index.html>`_
58-
* `0.4.1 <../v0.4.1/index.html>`_
59-
* `0.4.0 <../v0.4.0/index.html>`_
6059
* `0.3.0 <../v0.3.0/index.html>`_

‎_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(
245+
f"[runpythonerror] should not appear in output:\n{html}"
246+
)
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/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.4.2"
1+
__version__ = "0.4.3"
22
__author__ = "Xavier Dupré"
33
__github__ = "https://github.com/sdpython/sphinx-runpython"
44
__url__ = "https://sdpython.github.io/doc/sphinx-runpython/dev/"

‎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)