linecache encoding fails in ipython notebook

Bug #1097975 reported by Oliver Drake on 2013-01-09
This bug affects 1 person
Affects Status Importance Assigned to Milestone

Bug Description

I was trying to play with subunit in ipython notebook:

import unittest

class MyTest(unittest.TestCase):
    def test_one(self):
        assert True

    def test_two(self):
        assert False, "two failed..."

suite1 = unittest.TestLoader().loadTestsFromTestCase(MyTest)

import subunit
import sys

test_result = subunit.TestProtocolClient(sys.stdout)

The traceback I get is:
Traceback (most recent call last):
  File "<ipython-input-5-0e0da22da8b3>", line 6, in <module>
  File "/usr/lib/python2.7/unittest/", line 108, in run
  File "/usr/lib/python2.7/unittest/", line 391, in __call__
    return*args, **kwds)
  File "/usr/lib/python2.7/unittest/", line 331, in run
    result.addFailure(self, sys.exc_info())
  File "/home/drakeol/virtualenvs/dvvtools/local/lib/python2.7/site-packages/subunit/", line 668, in addFailure
    self._addOutcome("failure", test, error=error, details=details)
  File "/home/drakeol/virtualenvs/dvvtools/local/lib/python2.7/site-packages/subunit/", line 697, in _addOutcome
    tb_content = TracebackContent(error, test)
  File "/home/drakeol/virtualenvs/dvvtools/local/lib/python2.7/site-packages/testtools/", line 142, in __init__
    value = self._exc_info_to_unicode(err, test)
  File "/home/drakeol/virtualenvs/dvvtools/local/lib/python2.7/site-packages/testtools/", line 169, in _exc_info_to_unicode
    msgLines = format_exception(exctype, value, tb, length)
  File "/home/drakeol/virtualenvs/dvvtools/local/lib/python2.7/site-packages/testtools/", line 352, in _format_exc_info
    if evalue is None:
  File "/home/drakeol/virtualenvs/dvvtools/local/lib/python2.7/site-packages/testtools/", line 289, in _get_source_encoding
    newtuple = _EncodingTuple(linecache.cache[filename])
  File "/home/drakeol/virtualenvs/dvvtools/local/lib/python2.7/site-packages/testtools/", line 261, in _detect_encoding
    # Source starting with UTF-8 BOM is either UTF-8 or a SyntaxError
UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)

When I run the same code in a normal python file (utf-8 encoding) it runs fine and I see subunit output on stdout:
$ python
test: __main__.MyTest.test_one
successful: __main__.MyTest.test_one
test: __main__.MyTest.test_two
failure: __main__.MyTest.test_two [
Traceback (most recent call last):
  File "", line 8, in test_two
    assert False, "two failed..."
AssertionError: two failed...

I'm guessing it's to do with the fact that running in ipython notebook means there's no encoding for the source file of a traceback...
Python 2.7.3

Aside: when I run the same code in the vanilla interpreter it runs ok but the traceback is less informative as there's no source file (expected):
test: __main__.MyTest.test_one
failure: __main__.MyTest.test_one [
Traceback (most recent call last):
  File "<stdin>", line 3, in test_one

description: updated
Robert Collins (lifeless) wrote :

Could you provide a minimal example for this?

affects: subunit → testtools
Changed in testtools:
status: New → Triaged
importance: Undecided → Wishlist
summary: - TestProtocolClient: Formatting traceback fails in ipython notebook
+ linecache encoding fails in ipython notebook
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers