diff --git a/IPython/core/magic.py b/IPython/core/magic.py index 3b040a9..d2b8f47 100644 --- a/IPython/core/magic.py +++ b/IPython/core/magic.py @@ -953,6 +953,8 @@ Currently the magic system has the following functions:\n""" except UnicodeEncodeError: vstr = unicode(var).encode(DEFAULT_ENCODING, 'backslashreplace') + except: + vstr = "" % id(var) vstr = vstr.replace('\n','\\n') if len(vstr) < 50: print vstr diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index 257ae5d..530085a 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -376,6 +376,14 @@ def doctest_who(): Out[7]: ['alpha', 'beta'] """ +def test_whos(): + """Check that whos is protected against objects where repr() fails.""" + class A(object): + def __repr__(self): + raise Exception() + _ip.user_ns['a'] = A() + _ip.magic("whos") + @py3compat.u_format def doctest_precision(): """doctest for %precision