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

r4259:657a41be
r9972:8ab632a4
Show More
qt_for_kernel.py
87 lines | 2.8 KiB | text/x-python | PythonLexer
""" Import Qt in a manner suitable for an IPython kernel.
This is the import used for the `gui=qt` or `pylab=qt` initialization.
Import Priority:
if matplotlib has been imported and doesn't support v2 (<= 1.0.1):
use PyQt4 @v1
Next, ask ETS' QT_API env variable
if QT_API not set:
ask matplotlib via rcParams['backend.qt4']
if it said PyQt:
use PyQt4 @v1
elif it said PySide:
use PySide
else: (matplotlib said nothing)
# this is the default path - nobody told us anything
try:
PyQt @v1
except:
fallback on PySide
else:
use PyQt @v2 or PySide, depending on QT_API
because ETS doesn't work with PyQt @v1.
"""
import os
import sys
from IPython.utils.warn import warn
matplotlib = sys.modules.get('matplotlib')
if matplotlib and matplotlib.__version__ <= '1.0.1':
# 1.0.1 doesn't support pyside or v2, so stick with PyQt @v1,
# and ignore everything else
from PyQt4 import QtCore, QtGui
else:
# ask QT_API ETS variable *first*
QT_API = os.environ.get('QT_API', None)
if QT_API is None:
# QT_API not set, ask matplotlib if it was imported (e.g. `pylab=qt`)
if matplotlib:
mpqt = matplotlib.rcParams.get('backend.qt4', None)
else:
mpqt = None
if mpqt is None:
# matplotlib not imported or had nothing to say.
try:
# default to unconfigured PyQt4
from PyQt4 import QtCore, QtGui
except ImportError:
# fallback on PySide
try:
from PySide import QtCore, QtGui
except ImportError:
raise ImportError('Cannot import PySide or PyQt4')
elif mpqt.lower() == 'pyqt4':
# import PyQt4 unconfigured
from PyQt4 import QtCore, QtGui
elif mpqt.lower() == 'pyside':
from PySide import QtCore, QtGui
else:
raise ImportError("unhandled value for backend.qt4 from matplotlib: %r"%mpqt)
else:
# QT_API specified, use PySide or PyQt+v2 API from external.qt
# this means ETS is likely to be used, which requires v2
try:
from IPython.external.qt import QtCore, QtGui
except ValueError as e:
if 'API' in str(e):
# PyQt4 already imported, and APIv2 couldn't be set
# Give more meaningful message, and warn instead of raising
warn("""
Assigning the ETS variable `QT_API=pyqt` implies PyQt's v2 API for
QString and QVariant, but PyQt has already been imported
with v1 APIs. You should unset QT_API to work with PyQt4
in its default mode.
""")
# allow it to still work
from PyQt4 import QtCore, QtGui
else:
raise