##// 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:

r6624:9d90e074
r9972:8ab632a4
Show More
ipy_render.py
67 lines | 1.6 KiB | text/x-python | PythonLexer
vivainio
crlf cleanup
r680 """ IPython extension: Render templates from variables and paste to clipbard """
Brian Granger
ipapi.py => core/ipapi.py and imports updated.
r2027 from IPython.core import ipapi
vivainio
crlf cleanup
r680
Brian Granger
ipapi.py => core/ipapi.py and imports updated.
r2027 ip = ipapi.get()
vivainio
crlf cleanup
r680
from string import Template
import sys,os
Thomas Kluyver
Remove bundled Itpl module.
r6624 # We no longer bundle Itpl. If you update this module, you should use advanced
# string formatting instead.
Brian Granger
Moved Itpl.py to deathrow as we already have a copy in external.
r2031 from IPython.external.Itpl import itplns
vivainio
crlf cleanup
r680
def toclip_w32(s):
""" Places contents of s to clipboard
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
crlf cleanup
r680 Needs pyvin32 to work:
http://sourceforge.net/projects/pywin32/
"""
import win32clipboard as cl
import win32con
cl.OpenClipboard()
cl.EmptyClipboard()
cl.SetClipboardText( s.replace('\n','\r\n' ))
cl.CloseClipboard()
try:
Bernardo B. Marques
remove all trailling spaces
r4872 import win32clipboard
vivainio
crlf cleanup
r680 toclip = toclip_w32
except ImportError:
def toclip(s): pass
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
crlf cleanup
r680
def render(tmpl):
""" Render a template (Itpl format) from ipython variables
Example:
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
crlf cleanup
r680 $ import ipy_render
$ my_name = 'Bob' # %store this for convenience
$ t_submission_form = "Submission report, author: $my_name" # %store also
$ render t_submission_form
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
crlf cleanup
r680 => returns "Submission report, author: Bob" and copies to clipboard on win32
# if template exist as a file, read it. Note: ;f hei vaan => f("hei vaan")
Bernardo B. Marques
remove all trailling spaces
r4872 $ ;render c:/templates/greeting.txt
vivainio
crlf cleanup
r680 Template examples (Ka-Ping Yee's Itpl library):
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
crlf cleanup
r680 Here is a $string.
Here is a $module.member.
Here is an $object.member.
Here is a $functioncall(with, arguments).
Here is an ${arbitrary + expression}.
Here is an $array[3] member.
Here is a $dictionary['member'].
"""
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
crlf cleanup
r680 if os.path.isfile(tmpl):
tmpl = open(tmpl).read()
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
crlf cleanup
r680 res = itplns(tmpl, ip.user_ns)
toclip(res)
return res
Brian Granger
Continuing a massive refactor of everything.
r2205 ip.push('render')