##// END OF EJS Templates
Backport PR #2924: safe_run_module: Silence SystemExit codes 0 and None....
Backport PR #2924: safe_run_module: Silence SystemExit codes 0 and None. In `safe_execfile` we ignore SystemExit exceptions with codes 0 and 1. We don't do this for `safe_run_module` which leads to the following mismatch of tracebacks between Python and IPython: ``` $ cat > exit0.py import sys sys.exit(0) $ python -m exit0 $ ipython -m exit0 --------------------------------------------------------------------------- SystemExit Traceback (most recent call last) /usr/lib/python2.7/runpy.pyc in run_module(mod_name, init_globals, run_name, alter_sys) 174 if alter_sys: 175 return _run_module_code(code, init_globals, run_name, --> 176 fname, loader, pkg_name) 177 else: 178 # Leave the sys module alone /usr/lib/python2.7/runpy.pyc in _run_module_code(code, init_globals, mod_name, mod_fname, mod_loader, pkg_name) 80 mod_globals = temp_module.module.__dict__ 81 _run_code(code, mod_globals, init_globals, ---> 82 mod_name, mod_fname, mod_loader, pkg_name) 83 # Copy the globals of the temporary module, as they 84 # may be cleared when the temporary module goes away /usr/lib/python2.7/runpy.pyc in _run_code(code, run_globals, init_globals, mod_name, mod_fname, mod_loader, pkg_name) 70 __loader__ = mod_loader, 71 __package__ = pkg_name) ---> 72 exec code in run_globals 73 return run_globals 74 /tmp/exit0.py in <module>() 1 import sys ----> 2 sys.exit(0) SystemExit: 0 WARNING: Unknown failure executing module: <exit0> ``` The attached pull request silences SystemExit exceptions with codes 0 and None.

File last commit:

r5745:943a2321
r9972:8ab632a4
Show More
nbexamples.py
109 lines | 2.2 KiB | text/x-python | PythonLexer
MinRK
fix base64 code in nbformat.v2...
r5175 import os
from base64 import encodestring
Brian E. Granger
Full versioning added to nbformat.
r4406 from ..nbbase import (
Brian E. Granger
Initial draft of more formal notebook format....
r4401 NotebookNode,
Brian E. Granger
Implemented metadata for notebook format.
r4637 new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output,
new_metadata, new_author
Brian E. Granger
Initial draft of more formal notebook format....
r4401 )
MinRK
fix base64 code in nbformat.v2...
r5175 # some random base64-encoded *bytes*
png = encodestring(os.urandom(5))
jpeg = encodestring(os.urandom(6))
Brian E. Granger
Initial draft of more formal notebook format....
r4401
ws = new_worksheet(name='worksheet1')
Brian E. Granger
Markdown cells are now saved and restored in notebooks.
r4511 ws.cells.append(new_text_cell(
u'html',
source='Some NumPy Examples',
rendered='Some NumPy Examples'
Brian E. Granger
Initial draft of more formal notebook format....
r4401 ))
ws.cells.append(new_code_cell(
Brian E. Granger
Updates to basic notebook format....
r4402 input='import numpy',
Brian E. Granger
Added collapsed field to the code cell.
r4533 prompt_number=1,
collapsed=False
Brian E. Granger
Initial draft of more formal notebook format....
r4401 ))
Brian E. Granger
Markdown cells are now saved and restored in notebooks.
r4511 ws.cells.append(new_text_cell(
u'markdown',
Brian E. Granger
New .py notebook format implemented.
r4536 source='A random array',
rendered='A random array'
Brian E. Granger
Markdown cells are now saved and restored in notebooks.
r4511 ))
Brian E. Granger
Initial draft of more formal notebook format....
r4401 ws.cells.append(new_code_cell(
Brian E. Granger
Updates to basic notebook format....
r4402 input='a = numpy.random.rand(100)',
Brian E. Granger
Added collapsed field to the code cell.
r4533 prompt_number=2,
collapsed=True
Brian E. Granger
Initial draft of more formal notebook format....
r4401 ))
ws.cells.append(new_code_cell(
input='print a',
Brian E. Granger
Updates to basic notebook format....
r4402 prompt_number=3,
Brian E. Granger
Added collapsed field to the code cell.
r4533 collapsed=False,
Brian E. Granger
Updates to basic notebook format....
r4402 outputs=[new_output(
output_type=u'pyout',
output_text=u'<array a>',
output_html=u'The HTML rep',
output_latex=u'$a$',
MinRK
fix base64 code in nbformat.v2...
r5175 output_png=png,
output_jpeg=jpeg,
Brian E. Granger
Updates to basic notebook format....
r4402 output_svg=u'<svg>',
output_json=u'json data',
Brian E. Granger
Starting to rename text cell to html cell.
r4498 output_javascript=u'var i=0;',
prompt_number=3
Brian E. Granger
Updates to basic notebook format....
r4402 ),new_output(
output_type=u'display_data',
output_text=u'<array a>',
output_html=u'The HTML rep',
output_latex=u'$a$',
MinRK
fix base64 code in nbformat.v2...
r5175 output_png=png,
output_jpeg=jpeg,
Brian E. Granger
Updates to basic notebook format....
r4402 output_svg=u'<svg>',
output_json=u'json data',
Brian E. Granger
Adding tracebacks, evalue and etype to the nbformat and notebook.
r4540 output_javascript=u'var i=0;'
),new_output(
output_type=u'pyerr',
etype=u'NameError',
evalue=u'NameError was here',
traceback=[u'frame 0', u'frame 1', u'frame 2']
Brian E. Granger
Updates to basic notebook format....
r4402 )]
Brian E. Granger
Initial draft of more formal notebook format....
r4401 ))
Brian E. Granger
Implemented metadata for notebook format.
r4637 authors = [new_author(name='Bart Simpson',email='bsimpson@fox.com',
affiliation=u'Fox',url=u'http://www.fox.com')]
md = new_metadata(name=u'My Notebook',license=u'BSD',created=u'8601_goes_here',
modified=u'8601_goes_here',gistid=u'21341231',authors=authors)
Brian E. Granger
Initial draft of more formal notebook format....
r4401 nb0 = new_notebook(
Brian E. Granger
New .py notebook format implemented.
r4536 worksheets=[ws, new_worksheet(name='worksheet2')],
Brian E. Granger
Implemented metadata for notebook format.
r4637 metadata=md
Brian E. Granger
Initial draft of more formal notebook format....
r4401 )
Thomas Kluyver
Use emacs-recognised encoding declaration.
r5745 nb0_py = """# -*- coding: utf-8 -*-
Thomas Kluyver
Add encoding declaration to test for notebook -> .py export.
r5740 # <nbformat>2</nbformat>
Brian E. Granger
Full versioning added to nbformat.
r4406
Brian E. Granger
New .py notebook format implemented.
r4536 # <htmlcell>
# Some NumPy Examples
Brian E. Granger
Full versioning added to nbformat.
r4406 # <codecell>
Brian E. Granger
Initial draft of more formal notebook format....
r4401
import numpy
Brian E. Granger
New .py notebook format implemented.
r4536 # <markdowncell>
# A random array
Brian E. Granger
Initial draft of more formal notebook format....
r4401 # <codecell>
a = numpy.random.rand(100)
# <codecell>
print a
Brian E. Granger
Full versioning added to nbformat.
r4406
Brian E. Granger
Initial draft of more formal notebook format....
r4401 """