IPython ignores exceptions in the first evaulation of class attrs #212

Closed
derkuci opened this Issue Dec 16, 2010 · 1 comment

Projects

None yet

2 participants

@derkuci
derkuci commented Dec 16, 2010

I am not sure if this is a bug or a feature. When the input is an object attr, IPython seems to evaluate it twice. An exception thrown in the first evaluation is silently ignored. Here's an simple example:

# test.py
class DescriptorWithException(object):
    def __get__(self, instance, owner_class):
        if instance is None:
            return self
    print('accessing the descriptor')
        if not hasattr(instance, 'once'):
            instance.once = True
            raise RuntimeError('This will be ignored by IPython')
        return 0

class IPythonCatchException(object):
    data = DescriptorWithException()

If we access the attr "data" in python, we get an exception:

$ python
Python 2.7 (r27:82500, Sep 16 2010, 18:03:06) 
[GCC 4.5.1 20100907 (Red Hat 4.5.1-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> x = test.IPythonCatchException()
>>> x.data
accessing the descriptor
Traceback (most recent call last):
  File "", line 1, in 
  File "test.py", line 8, in __get__
    raise RuntimeError('This will be ignored by IPython')
RuntimeError: This will be ignored by IPython

However, if we do the same in ipython, we get no exceptions, and the attr is actually accessed twice.

$ ipython
Python 2.7 (r27:82500, Sep 16 2010, 18:03:06) 
Type "copyright", "credits" or "license" for more information.

IPython 0.10.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object'. ?object also works, ?? prints more.

In [1]: import test

In [2]: x = test.IPythonCatchException()

In [3]: x.data
accessing the descriptor
accessing the descriptor
Out[3]: 0

Interestingly, an alternative way triggers the exception:

In [4]: x = test.IPythonCatchException()

In [5]: a = x.data
accessing the descriptor
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)

~/ in ()

~/test.pyc in __get__(self, instance, owner_class)
      6         if not hasattr(instance, 'once'):
      7             instance.once = True
----> 8             raise RuntimeError('This will be ignored by IPython')
      9         return 0
     10 

RuntimeError: This will be ignored by IPython
Owner

I think this is the same issue as #102. Closing for now, reopen if it's different.

@takluyver takluyver closed this Mar 23, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment