Fix traceback handling of SyntaxErrors without line numbers. #2503

Merged
merged 2 commits into from Oct 19, 2012

Conversation

Projects
None yet
4 participants
Contributor

int3 commented Oct 18, 2012

E.g. lxml generates such errors.

Contributor

int3 commented Oct 18, 2012

Hmm, build for python 3.2 seems to fail on Travis due to unrelated pyzmq issues.

Owner

minrk commented Oct 18, 2012

@int3 - nope, pyzmq is working fine. This is actually your code:

======================================================================
2341FAIL: test_syntaxerror_without_lineno (IPython.core.tests.test_ultratb.SyntaxErrorTest)
2342----------------------------------------------------------------------
2343Traceback (most recent call last):
2344  File "/home/travis/virtualenv/python3.2/lib/python3.2/site-packages/ipython-0.14.dev-py3.2.egg/IPython/core/tests/test_ultratb.py", line 101, in test_syntaxerror_without_lineno
2345    ip.run_cell("raise SyntaxError()")
2346  File "/home/travis/virtualenv/python3.2/lib/python3.2/site-packages/ipython-0.14.dev-py3.2.egg/IPython/testing/tools.py", line 341, in __exit__
2347    assert self.s in printed, notprinted_msg.format(self.s, self.channel, printed)
2348AssertionError: Did not find 'line unknown' in printed output (on stdout):
<snip>
2402/home/travis/virtualenv/python3.2/lib/python3.2/linecache.py in getline(filename='<string>', lineno='unknown', module_globals=None)
2403     14 def getline(filename, lineno, module_globals=None):
2404     15     lines = getlines(filename, module_globals)
2405---> 16     if 1 <= lineno <= len(lines):
2406        lineno = 'unknown'
2407        global len = undefined
2408        lines = []
2409     17         return lines[lineno-1]
2410     18     else:
2411
2412TypeError: unorderable types: int() <= str()

Later code assumes lineno is a number, and you have violated that assumption by setting it to 'undefined'. Python 3 introduces very strict comparison rules, which is why it only fails on 3.2.

Contributor

bfroehle commented Oct 18, 2012

Can we just change the the format string to use %s instead of %d? For integers this would be the same, and it'll render None in the case that the line number is None.

Owner

takluyver commented Oct 18, 2012

@bfroehle : Same issue - TypeError: unorderable types: int() <= NoneType()

Owner

takluyver commented Oct 18, 2012

The second argument to linecache needs to be a number, so if we don't have a number, we need to skip that call to ulinecache.getline (ultratb line 564).

Contributor

int3 commented Oct 19, 2012

Okay, got it to pass now.

Owner

takluyver commented Oct 19, 2012

That looks better, thanks. I suspect a SyntaxError without the proper attributes should just fall back on the normal traceback system, but showing a useful error message is definitely an improvement.

Contributor

bfroehle commented Oct 19, 2012

This looks good to me now. Thanks.

Owner

takluyver commented Oct 19, 2012

OK, landing this. Further improvement could be done later, if anyone's interested.

takluyver added a commit that referenced this pull request Oct 19, 2012

Merge pull request #2503 from int3/syntaxerror-tb
Fix traceback handling of SyntaxErrors without line numbers.

@takluyver takluyver merged commit 47260f5 into ipython:master Oct 19, 2012

1 check passed

default The Travis build passed
Details

minrk added a commit that referenced this pull request Mar 5, 2013

Backport PR #2503: Fix traceback handling of SyntaxErrors without lin…
…e numbers.

E.g. lxml generates such errors.

mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this pull request Nov 3, 2014

Merge pull request #2503 from int3/syntaxerror-tb
Fix traceback handling of SyntaxErrors without line numbers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment