Permalink
Browse files

Merge pull request #1396 from takluyver/magic_tb_fix

In order for the %tb magic to work as expected, no other error can be raised by invoking it. Even if an error is caught immediately, it affects the 'last error' values in sys.

This cuts a try/except block out of ip.magic(). Happily, in this case there was a simpler way to do the same thing using `str.partition`
  • Loading branch information...
2 parents f77a23d + e14a39d commit 920beabd30116f379d0be2806940f24e17dd9a32 @minrk minrk committed Feb 11, 2012
Showing with 1 addition and 6 deletions.
  1. +1 −6 IPython/core/interactiveshell.py
@@ -2005,14 +2005,9 @@ def magic(self, arg_s, next_input=None):
if next_input:
self.set_next_input(next_input)
- args = arg_s.split(' ',1)
- magic_name = args[0]
+ magic_name, _, magic_args = arg_s.partition(' ')
magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
- try:
- magic_args = args[1]
- except IndexError:
- magic_args = ''
fn = getattr(self,'magic_'+magic_name,None)
if fn is None:
error("Magic function `%s` not found." % magic_name)

4 comments on commit 920beab

@thisch
thisch commented on 920beab Feb 12, 2012

this commit breaks the following:

In [1]: for a in b
  File "<ipython-input-1-3740e4308406>", line 1
    for a in b
              ^
SyntaxError: invalid syntax


In [2]: %tb
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/home/thomas/<ipython-input-2-eaaaf52e6c85> in <module>()
----> 1 get_ipython().magic(u'tb')

/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in magic(self, arg_s, next_input)
   2018                 self._magic_locals = sys._getframe(1).f_locals
   2019             with self.builtin_trap:
-> 2020                 result = fn(magic_args)
   2021             # Ensure we're not keeping object references around:
   2022             self._magic_locals = {}

/usr/lib/python2.7/site-packages/IPython/core/magic.pyc in magic_tb(self, s)
   3547 
   3548         See %xmode for changing exception reporting modes.
-> 3549         self.shell.showtraceback()
   3550 
   3551     @skip_doctest

/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in showtraceback(self, exc_tuple, filename, tb_offset, exception_only)
   1657                 # Though this won't be called by syntax errors in the input
   1658                 # line, there may be SyntaxError cases with imported code.
-> 1659                 self.showsyntaxerror(filename)
   1660             elif etype is UsageError:
   1661                 self.write_err("UsageError: %s" % value)

/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in showsyntaxerror(self, filename)
   1723                 pass
   1724 
-> 1725         stb = self.SyntaxTB.structured_traceback(etype, value, [])
   1726         self._showtraceback(etype, value, stb)
   1727 

/usr/lib/python2.7/site-packages/IPython/core/ultratb.pyc in structured_traceback(self, etype, value, elist, tb_offset, context)
    489             out_list.extend(self._format_list(elist))
    490         # The exception info should be a single entry in the list.
--> 491         lines = ''.join(self._format_exception_only(etype, value))
    492         out_list.append(lines)
    493 

/usr/lib/python2.7/site-packages/IPython/core/ultratb.pyc in _format_exception_only(self, etype, value)
    559         Colors = self.Colors
    560         list = []
--> 561         stype = Colors.excName + etype.__name__ + Colors.Normal
    562         if value is None:
    563             # Not sure if this can still happen in Python 2.6 and above

AttributeError: 'NoneType' object has no attribute '__name__'

In [3]: 

btw, can you tell me how to format this traceback using github markdown ?

@minrk
Member
minrk commented on 920beab Feb 12, 2012

If you wrap it in:

```python
<paste>
```

It will be highlighted.

@thisch
thisch commented on 920beab Feb 12, 2012

thx.

The expected output should be (worked in commit f77a23d)

In [2]: %tb
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
IndexError: list index out of range
@minrk
Member
minrk commented on 920beab Feb 12, 2012

No, that was a bug. The expected output should either be the SyntaxError above or 'no traceback'.

Please sign in to comment.