diff --git a/py/_code/code.py b/py/_code/code.py index 846870e7f..397398029 100644 --- a/py/_code/code.py +++ b/py/_code/code.py @@ -1,5 +1,5 @@ import py -import sys +import sys, os.path builtin_repr = repr @@ -70,13 +70,13 @@ class Code(object): return py.std.new.code(*arglist) def path(self): - """ return a py.path.local object pointing to the source code """ + """ return a path object pointing to source code""" fn = self.raw.co_filename try: return fn.__path__ except AttributeError: p = py.path.local(self.raw.co_filename) - if not p.check(file=1): + if not p.check(): # XXX maybe try harder like the weird logic # in the standard lib [linecache.updatecache] does? p = self.raw.co_filename @@ -537,9 +537,9 @@ class FormattedExcinfo(object): else: if self.style == "short": line = source[line_index].lstrip() - trybasename = getattr(entry.path, 'basename', entry.path) + basename = os.path.basename(entry.frame.code.filename) lines.append(' File "%s", line %d, in %s' % ( - trybasename, entry.lineno+1, entry.name)) + basename, entry.lineno+1, entry.name)) lines.append(" " + line) if excinfo: lines.extend(self.get_exconly(excinfo, indent=4)) diff --git a/testing/code/test_excinfo.py b/testing/code/test_excinfo.py index 306057eb4..4fbc61aa5 100644 --- a/testing/code/test_excinfo.py +++ b/testing/code/test_excinfo.py @@ -503,6 +503,32 @@ raise ValueError() reprtb = p.repr_traceback(excinfo) assert len(reprtb.reprentries) == 3 + def test_traceback_short_no_source(self, importasmod, monkeypatch): + mod = importasmod(""" + def func1(): + raise ValueError("hello") + def entry(): + func1() + """) + excinfo = py.test.raises(ValueError, mod.entry) + from py._code.code import Code + monkeypatch.setattr(Code, 'path', 'bogus') + excinfo.traceback[0].frame.code.path = "bogus" + p = FormattedExcinfo(style="short") + reprtb = p.repr_traceback_entry(excinfo.traceback[-2]) + lines = reprtb.lines + last_p = FormattedExcinfo(style="short") + last_reprtb = last_p.repr_traceback_entry(excinfo.traceback[-1], excinfo) + last_lines = last_reprtb.lines + monkeypatch.undo() + basename = py.path.local(mod.__file__).basename + assert lines[0] == ' File "%s", line 5, in entry' % basename + assert lines[1] == ' func1()' + + assert last_lines[0] == ' File "%s", line 3, in func1' % basename + assert last_lines[1] == ' raise ValueError("hello")' + assert last_lines[2] == 'E ValueError: hello' + def test_repr_traceback_and_excinfo(self, importasmod): mod = importasmod(""" def f(x):