Repository: sphinx-gallery/sphinx-gallery

Issue:
Small regression in 0.4.1: print(__doc__) creates empty output block 
Seen in https://github.com/scikit-learn/scikit-learn/pull/14507#issuecomment-516466110, `print(__doc__)` creates empty output box (yellowish box in the output below). 

![image](https://user-images.githubusercontent.com/1680079/62141734-5f74e380-b2ed-11e9-9796-4ff96ae33d63.png)

This was introduced by https://github.com/sphinx-gallery/sphinx-gallery/pull/475. A simple fix would be to not have an output box if `my_stdout.isspace()` and add a test similar to the one introduced in #475 to check that `print(__doc__)` does not create an output box.

Small reminder: many scikit-learn examples have `print(__doc__)` at the beginning, so inside sphinx-gallery we set `__doc__ = ''` (`__doc__` is already rendered at the top of the example HTML so you don't want it captured in stdout as well).

ping @lucyleeow who is interested to work on this.


Discussion:

Patch:
diff --git a/sphinx_gallery/gen_rst.py b/sphinx_gallery/gen_rst.py
index 64c2047..ed04ba2 100644
--- a/sphinx_gallery/gen_rst.py
+++ b/sphinx_gallery/gen_rst.py
@@ -508,7 +508,7 @@ def execute_code_block(compiler, block, example_globals,
         os.chdir(cwd)
 
         captured_std = captured_std.getvalue().expandtabs()
-        if captured_std:
+        if captured_std and not captured_std.isspace():
             captured_std = CODE_OUTPUT.format(indent(captured_std, u' ' * 4))
         else:
             captured_std = ''


Test patch:
diff --git a/sphinx_gallery/tests/test_gen_rst.py b/sphinx_gallery/tests/test_gen_rst.py
index 82644ad..2442540 100644
--- a/sphinx_gallery/tests/test_gen_rst.py
+++ b/sphinx_gallery/tests/test_gen_rst.py
@@ -448,6 +448,27 @@ def test_output_indentation(gallery_conf):
     assert output_test_string == test_string.replace(r"\n", "\n")
 
 
+def test_empty_output_box(gallery_conf):
+    """Tests that `print(__doc__)` doesn't produce an empty output box."""
+    compiler = codeop.Compile()
+    
+    code_block = ("code", "print(__doc__)", 1)
+
+    script_vars = {
+        "execute_script": True,
+        "image_path_iterator": ImagePathIterator("temp.png"),
+        "src_file": __file__,
+        "memory_delta": [],
+    }
+
+    example_globals = {'__doc__': ''}
+
+    output = sg.execute_code_block(
+        compiler, code_block, example_globals, script_vars, gallery_conf
+    )
+    assert output.isspace() == True
+
+
 class TestLoggingTee:
     def setup(self):
         self.output_file = io.StringIO()