Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOGS.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Change Logs
===========

0.4.3
+++++

* :pr:`56`: Add `:hide_err:` option to suppress `[runpythonerror]` from runpython output

0.4.2
+++++

Expand Down
3 changes: 1 addition & 2 deletions _doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ Sources available on
Older versions
++++++++++++++

* `0.4.3 <../v0.4.3/index.html>`_
* `0.4.2 <../v0.4.2/index.html>`_
* `0.4.1 <../v0.4.1/index.html>`_
* `0.4.0 <../v0.4.0/index.html>`_
* `0.3.0 <../v0.3.0/index.html>`_
29 changes: 29 additions & 0 deletions _unittests/ut_runpython/test_runpython_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,35 @@ def depart_rp_node(self, node):
if t1 not in html:
raise AssertionError(html)

def test_runpython_hide_err(self):
"""
Test that :hide-err: suppresses [runpythonerror] from the output.
"""
if "enable_disabled_documented_pieces_of_code" in sys.__dict__:
raise AssertionError("this case should not be")

content = """
test a directive
================

.. runpython::
:rst:
:hide-err:

import warnings
warnings.warn("deprecated", DeprecationWarning)
print("output line")
""".replace(" ", "")

html = rst2html(content, writer_name="rst")

if "runpythonerror" in html:
raise AssertionError(
f"[runpythonerror] should not appear in output:\n{html}"
)
if "output line" not in html:
raise AssertionError(f"stdout should still appear in output:\n{html}")

def test_runpython_raw(self):
"""
this test also test the extension runpython
Expand Down
2 changes: 1 addition & 1 deletion sphinx_runpython/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.4.2"
__version__ = "0.4.3"
__author__ = "Xavier Dupré"
__github__ = "https://github.com/sdpython/sphinx-runpython"
__url__ = "https://sdpython.github.io/doc/sphinx-runpython/dev/"
Expand Down
8 changes: 7 additions & 1 deletion sphinx_runpython/runpython/sphinx_runpython_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,10 @@ class RunPythonDirective(Directive):
the script to be stored somewhere in order to retrieve it.
* ``:debug:`` if ``:rst:`` is used, it shows some information to help
debugging.
* ``:hide-err:`` if present, hides any error or warning output that would
otherwise be appended as ``[runpythonerror]`` in the content. This is
useful when the code produces stderr output (such as deprecation warnings)
that should not appear in the rendered documentation.

Option *rst* can be used the following way::

Expand Down Expand Up @@ -630,6 +634,7 @@ class RunPythonDirective(Directive):
"store_in_file": directives.unchanged,
"linenos": directives.unchanged,
"debug": directives.unchanged,
"hide-err": directives.unchanged,
}
has_content = True
runpython_class = runpython_node
Expand Down Expand Up @@ -693,6 +698,7 @@ def run(self):
"store": "store" in self.options and self.options["store"] in bool_set_,
"restore": "restore" in self.options
and self.options["restore"] in bool_set_,
"hide-err": "hide-err" in self.options,
}

if p["setsysvar"] is not None and len(p["setsysvar"]) == 0:
Expand Down Expand Up @@ -821,7 +827,7 @@ def run(self):
if err is not None:
err = err.rstrip(" \n\r\t")
content = out
if len(err) > 0:
if len(err) > 0 and not p["hide-err"]:
content += "\n[runpythonerror]\n" + err

# add member
Expand Down
Loading