Skip to content

Commit 13cbef2

Browse files
authored
Merge pull request #73 from jbweston/feature/disable-stderr
make stderr a warning and not an exception by default
2 parents 9101ee3 + 035fbea commit 13cbef2

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

jupyter_sphinx/execute.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,8 @@ def apply(self):
379379
if output['output_type'] == 'stream'
380380
and output['name'] == 'stderr']
381381
if stderr and not node.attributes['stderr']:
382-
raise ExtensionError('Cell printed to stderr:\n{}'
383-
.format(stderr[0]['text']))
382+
logger.warning('Cell printed to stderr:\n{}'
383+
.format(stderr[0]['text']))
384384

385385
try:
386386
lexer = notebook.metadata.language_info.pygments_lexer
@@ -407,6 +407,7 @@ def apply(self):
407407
output_nodes = cell_output_to_nodes(
408408
cell,
409409
self.config.jupyter_execute_data_priority,
410+
bool(node.attributes["stderr"]),
410411
sphinx_abs_dir(self.env),
411412
thebe_config
412413
)
@@ -463,14 +464,16 @@ def count(x):
463464
return (list(x) for _, x in groupby(it, count))
464465

465466

466-
def cell_output_to_nodes(cell, data_priority, dir, thebe_config):
467+
def cell_output_to_nodes(cell, data_priority, write_stderr, dir, thebe_config):
467468
"""Convert a jupyter cell with outputs and filenames to doctree nodes.
468469
469470
Parameters
470471
----------
471472
cell : jupyter cell
472473
data_priority : list of mime types
473474
Which media types to prioritize.
475+
write_stderr : bool
476+
If True include stderr in cell output
474477
dir : string
475478
Sphinx "absolute path" to the output folder, so it is a relative path
476479
to the source folder prefixed with ``/``.
@@ -483,6 +486,8 @@ def cell_output_to_nodes(cell, data_priority, dir, thebe_config):
483486
if (
484487
output_type == 'stream'
485488
):
489+
if not write_stderr and output["name"] == "stderr":
490+
continue
486491
to_add.append(docutils.nodes.literal_block(
487492
text=output['text'],
488493
rawsource=output['text'],

tests/test_execute.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,27 @@ def doctree():
2626
apps = []
2727
syspath = sys.path[:]
2828

29-
def doctree(source, config=None):
29+
def doctree(source, config=None, return_warnings=False):
3030
src_dir = tempfile.mkdtemp()
3131
source_trees.append(src_dir)
3232
with open(os.path.join(src_dir, 'conf.py'), 'w') as f:
3333
f.write("extensions = ['jupyter_sphinx.execute']")
3434
if config is not None:
3535
f.write('\n' + config)
36-
with open(os.path.join(src_dir, 'index.rst'), 'w') as f:
36+
with open(os.path.join(src_dir, 'contents.rst'), 'w') as f:
3737
f.write(source)
38+
warnings = StringIO()
3839
app = SphinxTestApp(srcdir=path(src_dir), status=StringIO(),
39-
warning=StringIO())
40+
warning=warnings)
4041
apps.append(app)
4142
app.build()
42-
return app.env.get_doctree('index')
43+
44+
doctree = app.env.get_doctree("contents")
45+
46+
if return_warnings:
47+
return doctree, warnings.getvalue()
48+
else:
49+
return doctree
4350

4451
yield doctree
4552

@@ -217,8 +224,11 @@ def test_stderr(doctree):
217224
import sys
218225
print('hello world', file=sys.stderr)
219226
"""
220-
with pytest.raises(ExtensionError):
221-
tree = doctree(source)
227+
228+
tree, warnings = doctree(source, return_warnings=True)
229+
assert "hello world" in warnings
230+
cell, = tree.traverse(JupyterCellNode)
231+
assert len(cell.children) == 1 # no output
222232

223233
source = """
224234
.. jupyter-execute::

0 commit comments

Comments
 (0)