always directly use basename for tracebacks, independently from code.path
fixes issue77 although i guess it was already fixed before. --HG-- branch : trunk
This commit is contained in:
parent
866255e1f5
commit
55fcc5a219
|
@ -1,5 +1,5 @@
|
||||||
import py
|
import py
|
||||||
import sys
|
import sys, os.path
|
||||||
|
|
||||||
builtin_repr = repr
|
builtin_repr = repr
|
||||||
|
|
||||||
|
@ -70,13 +70,13 @@ class Code(object):
|
||||||
return py.std.new.code(*arglist)
|
return py.std.new.code(*arglist)
|
||||||
|
|
||||||
def path(self):
|
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
|
fn = self.raw.co_filename
|
||||||
try:
|
try:
|
||||||
return fn.__path__
|
return fn.__path__
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
p = py.path.local(self.raw.co_filename)
|
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
|
# XXX maybe try harder like the weird logic
|
||||||
# in the standard lib [linecache.updatecache] does?
|
# in the standard lib [linecache.updatecache] does?
|
||||||
p = self.raw.co_filename
|
p = self.raw.co_filename
|
||||||
|
@ -537,9 +537,9 @@ class FormattedExcinfo(object):
|
||||||
else:
|
else:
|
||||||
if self.style == "short":
|
if self.style == "short":
|
||||||
line = source[line_index].lstrip()
|
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' % (
|
lines.append(' File "%s", line %d, in %s' % (
|
||||||
trybasename, entry.lineno+1, entry.name))
|
basename, entry.lineno+1, entry.name))
|
||||||
lines.append(" " + line)
|
lines.append(" " + line)
|
||||||
if excinfo:
|
if excinfo:
|
||||||
lines.extend(self.get_exconly(excinfo, indent=4))
|
lines.extend(self.get_exconly(excinfo, indent=4))
|
||||||
|
|
|
@ -503,6 +503,32 @@ raise ValueError()
|
||||||
reprtb = p.repr_traceback(excinfo)
|
reprtb = p.repr_traceback(excinfo)
|
||||||
assert len(reprtb.reprentries) == 3
|
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):
|
def test_repr_traceback_and_excinfo(self, importasmod):
|
||||||
mod = importasmod("""
|
mod = importasmod("""
|
||||||
def f(x):
|
def f(x):
|
||||||
|
|
Loading…
Reference in New Issue