Skip to content

Commit d7ea4da

Browse files
committed
raise error when stderr present
Add a directive option to disable this and instead print stderr identically to stdout. Also add tests for this. Closes #48
1 parent 4dab7f6 commit d7ea4da

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

jupyter_sphinx/execute.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ class JupyterCell(Directive):
147147
'hide-output': directives.flag,
148148
'code-below': directives.flag,
149149
'raises': csv_option,
150+
'stderr': directives.flag,
150151
}
151152

152153
def run(self):
@@ -180,6 +181,7 @@ def run(self):
180181
hide_output=('hide-output' in self.options),
181182
code_below=('code-below' in self.options),
182183
raises=self.options.get('raises'),
184+
stderr=('stderr' in self.options),
183185
)]
184186

185187

@@ -291,6 +293,15 @@ def apply(self):
291293
raise ExtensionError('Cell raised uncaught exception:\n{}'
292294
.format('\n'.join(errors[0]['traceback'])))
293295

296+
# Raise error if cells print to stderr
297+
for node, cell in zip(nodes, notebook.cells):
298+
stderr = [output for output in cell.outputs
299+
if output['output_type'] == 'stream'
300+
and output['name'] == 'stderr']
301+
if stderr and not node.attributes['stderr']:
302+
raise ExtensionError('Cell printed to stderr:\n{}'
303+
.format(stderr[0]['text']))
304+
294305
# Highlight the code cells now that we know what language they are
295306
for node in nodes:
296307
source = node.children[0]
@@ -379,7 +390,6 @@ def cell_output_to_nodes(cell, data_priority, dir):
379390
output_type = output['output_type']
380391
if (
381392
output_type == 'stream'
382-
and output['name'] == 'stdout'
383393
):
384394
to_add.append(docutils.nodes.literal_block(
385395
text=output['text'],

tests/test_execute.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,38 @@ def test_javascript(doctree):
191191
node, = list(tree.traverse(raw))
192192
text, = node.children
193193
assert 'world' in text
194+
195+
196+
def test_stdout(doctree):
197+
source = """
198+
.. jupyter-execute::
199+
200+
print('hello world')
201+
"""
202+
tree = doctree(source)
203+
cell, = tree.traverse(JupyterCellNode)
204+
assert len(cell.children) == 2
205+
assert cell.children[1].rawsource.strip() == "hello world"
206+
207+
208+
def test_stderr(doctree):
209+
source = """
210+
.. jupyter-execute::
211+
212+
import sys
213+
print('hello world', file=sys.stderr)
214+
"""
215+
with pytest.raises(ExtensionError):
216+
tree = doctree(source)
217+
218+
source = """
219+
.. jupyter-execute::
220+
:stderr:
221+
222+
import sys
223+
print('hello world', file=sys.stderr)
224+
"""
225+
tree = doctree(source)
226+
cell, = tree.traverse(JupyterCellNode)
227+
assert len(cell.children) == 2
228+
assert cell.children[1].rawsource.strip() == "hello world"

0 commit comments

Comments
 (0)