diff --git a/CHANGELOGS.rst b/CHANGELOGS.rst index 5ab7192..94aa6c3 100644 --- a/CHANGELOGS.rst +++ b/CHANGELOGS.rst @@ -1,6 +1,11 @@ Change Logs =========== +0.4.3 ++++++ + +* :pr:`56`: Add `:hide_err:` option to suppress `[runpythonerror]` from runpython output + 0.4.2 +++++ diff --git a/_doc/index.rst b/_doc/index.rst index 7724d9c..d393840 100644 --- a/_doc/index.rst +++ b/_doc/index.rst @@ -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>`_ diff --git a/_unittests/ut_runpython/test_runpython_extension.py b/_unittests/ut_runpython/test_runpython_extension.py index 99d9a77..0a79898 100644 --- a/_unittests/ut_runpython/test_runpython_extension.py +++ b/_unittests/ut_runpython/test_runpython_extension.py @@ -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 diff --git a/sphinx_runpython/__init__.py b/sphinx_runpython/__init__.py index 104ec78..a8a9b0a 100644 --- a/sphinx_runpython/__init__.py +++ b/sphinx_runpython/__init__.py @@ -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/" diff --git a/sphinx_runpython/runpython/sphinx_runpython_extension.py b/sphinx_runpython/runpython/sphinx_runpython_extension.py index d5a15d6..9d6c54d 100644 --- a/sphinx_runpython/runpython/sphinx_runpython_extension.py +++ b/sphinx_runpython/runpython/sphinx_runpython_extension.py @@ -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:: @@ -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 @@ -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: @@ -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