ipython/ipython — Issue #851: IPython shell swallows exceptions in certain circumstances
Author: sccolbert
Created: 2011-10-10 16:46:43+00:00
State: open

A vanilla Python shell session:

Chris-Colberts-MacBook-Pro:enaml chris$ python
Enthought Python Distribution -- www.enthought.com
Version: 7.1-2 (32-bit)

Python 2.7.2 |EPD 7.1-2 (32-bit)| (default, Jul 3 2011, 15:40:35)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "packages", "demo" or "enthought" for more information.
>>> from traits.api import *
>>> class Foo(HasTraits):
... a = Int
...
>>> def default(self):
... self.remove_trait('a')
... setattr(self, 'a', 42.0)
... return 42.0
...
>>> c = Any().as_ctrait()
>>> c.default_value(8, default)
>>> f = Foo()
>>> f.add_trait('a', c)
>>> f.a
Traceback (most recent call last):
File "", line 1, in
File "", line 3, in default
File "/Library/Frameworks/Python.framework/Versions/7.1/lib/python2.7/site-packages/traits/trait_handlers.py", line 168, in error
value )
traits.trait_errors.TraitError: The 'a' trait of a Foo instance must be an integer, but a value of 42.0 was specified.
>>> f.a
0

The equivalent IPython session:

Python 2.7.2 |EPD 7.1-2 (32-bit)| (default, Jul 3 2011, 15:40:35)
Type "copyright", "credits" or "license" for more information.

IPython 0.11 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.

In [1]: from traits.api import *

In [2]: def default(self):
...: self.remove_trait('a')
...: setattr(self, 'a', 42.0)
...: return 42.0
...:

In [3]: class Foo(HasTraits):
...: a = Int
...:

In [4]: c = Any().as_ctrait()

In [5]: c.default_value(8, default)

In [6]: f = Foo()

In [7]: f.add_trait('a', c)

In [8]: f.a
Out[8]: 0

The IPython shell swallows the exception raised by the `default` function. It appears that IPython is doing subsequent getattr(obj, name) calls (which succeed in this case) after the exception is raised.