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

r5390:c82649ea
r9972:8ab632a4
Show More
test_configurable.py
183 lines | 5.7 KiB | text/x-python | PythonLexer
# encoding: utf-8
"""
Tests for IPython.config.configurable
Authors:
* Brian Granger
* Fernando Perez (design help)
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2008-2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from unittest import TestCase
from IPython.config.configurable import (
Configurable,
SingletonConfigurable
)
from IPython.utils.traitlets import (
Integer, Float, Unicode
)
from IPython.config.loader import Config
from IPython.utils.py3compat import PY3
#-----------------------------------------------------------------------------
# Test cases
#-----------------------------------------------------------------------------
class MyConfigurable(Configurable):
a = Integer(1, config=True, help="The integer a.")
b = Float(1.0, config=True, help="The integer b.")
c = Unicode('no config')
mc_help=u"""MyConfigurable options
----------------------
--MyConfigurable.a=<Integer>
Default: 1
The integer a.
--MyConfigurable.b=<Float>
Default: 1.0
The integer b."""
mc_help_inst=u"""MyConfigurable options
----------------------
--MyConfigurable.a=<Integer>
Current: 5
The integer a.
--MyConfigurable.b=<Float>
Current: 4.0
The integer b."""
# On Python 3, the Integer trait is a synonym for Int
if PY3:
mc_help = mc_help.replace(u"<Integer>", u"<Int>")
mc_help_inst = mc_help_inst.replace(u"<Integer>", u"<Int>")
class Foo(Configurable):
a = Integer(0, config=True, help="The integer a.")
b = Unicode('nope', config=True)
class Bar(Foo):
b = Unicode('gotit', config=False, help="The string b.")
c = Float(config=True, help="The string c.")
class TestConfigurable(TestCase):
def test_default(self):
c1 = Configurable()
c2 = Configurable(config=c1.config)
c3 = Configurable(config=c2.config)
self.assertEquals(c1.config, c2.config)
self.assertEquals(c2.config, c3.config)
def test_custom(self):
config = Config()
config.foo = 'foo'
config.bar = 'bar'
c1 = Configurable(config=config)
c2 = Configurable(config=c1.config)
c3 = Configurable(config=c2.config)
self.assertEquals(c1.config, config)
self.assertEquals(c2.config, config)
self.assertEquals(c3.config, config)
# Test that copies are not made
self.assert_(c1.config is config)
self.assert_(c2.config is config)
self.assert_(c3.config is config)
self.assert_(c1.config is c2.config)
self.assert_(c2.config is c3.config)
def test_inheritance(self):
config = Config()
config.MyConfigurable.a = 2
config.MyConfigurable.b = 2.0
c1 = MyConfigurable(config=config)
c2 = MyConfigurable(config=c1.config)
self.assertEquals(c1.a, config.MyConfigurable.a)
self.assertEquals(c1.b, config.MyConfigurable.b)
self.assertEquals(c2.a, config.MyConfigurable.a)
self.assertEquals(c2.b, config.MyConfigurable.b)
def test_parent(self):
config = Config()
config.Foo.a = 10
config.Foo.b = "wow"
config.Bar.b = 'later'
config.Bar.c = 100.0
f = Foo(config=config)
b = Bar(config=f.config)
self.assertEquals(f.a, 10)
self.assertEquals(f.b, 'wow')
self.assertEquals(b.b, 'gotit')
self.assertEquals(b.c, 100.0)
def test_override1(self):
config = Config()
config.MyConfigurable.a = 2
config.MyConfigurable.b = 2.0
c = MyConfigurable(a=3, config=config)
self.assertEquals(c.a, 3)
self.assertEquals(c.b, config.MyConfigurable.b)
self.assertEquals(c.c, 'no config')
def test_override2(self):
config = Config()
config.Foo.a = 1
config.Bar.b = 'or' # Up above b is config=False, so this won't do it.
config.Bar.c = 10.0
c = Bar(config=config)
self.assertEquals(c.a, config.Foo.a)
self.assertEquals(c.b, 'gotit')
self.assertEquals(c.c, config.Bar.c)
c = Bar(a=2, b='and', c=20.0, config=config)
self.assertEquals(c.a, 2)
self.assertEquals(c.b, 'and')
self.assertEquals(c.c, 20.0)
def test_help(self):
self.assertEquals(MyConfigurable.class_get_help(), mc_help)
def test_help_inst(self):
inst = MyConfigurable(a=5, b=4)
self.assertEquals(MyConfigurable.class_get_help(inst), mc_help_inst)
class TestSingletonConfigurable(TestCase):
def test_instance(self):
from IPython.config.configurable import SingletonConfigurable
class Foo(SingletonConfigurable): pass
self.assertEquals(Foo.initialized(), False)
foo = Foo.instance()
self.assertEquals(Foo.initialized(), True)
self.assertEquals(foo, Foo.instance())
self.assertEquals(SingletonConfigurable._instance, None)
def test_inheritance(self):
class Bar(SingletonConfigurable): pass
class Bam(Bar): pass
self.assertEquals(Bar.initialized(), False)
self.assertEquals(Bam.initialized(), False)
bam = Bam.instance()
bam == Bar.instance()
self.assertEquals(Bar.initialized(), True)
self.assertEquals(Bam.initialized(), True)
self.assertEquals(bam, Bam._instance)
self.assertEquals(bam, Bar._instance)
self.assertEquals(SingletonConfigurable._instance, None)