Handle inspect.getsourcelines failures in FixtureLookupError

Fixes #553

--HG--
branch : getsourcelines-error-issue-553-pytest2.7
This commit is contained in:
Bruno Oliveira 2015-04-15 19:31:22 -03:00
parent 6591d7a209
commit 15328c04eb
3 changed files with 34 additions and 7 deletions

View File

@ -1,6 +1,11 @@
2.7.1.dev (compared to 2.7.0) 2.7.1.dev (compared to 2.7.0)
----------------------------- -----------------------------
- fix issue553: properly handling inspect.getsourcelines failures in
FixtureLookupError which would lead to to an internal error,
obfuscating the original problem. Thanks talljosh for initial
diagnose/patch and Bruno Oliveira for final patch.
- fix issue660: properly report scope-mismatch-access errors - fix issue660: properly report scope-mismatch-access errors
independently from ordering of fixture arguments. Also independently from ordering of fixture arguments. Also
avoid the pytest internal traceback which does not provide avoid the pytest internal traceback which does not provide

View File

@ -1537,13 +1537,18 @@ class FixtureLookupError(LookupError):
# it at the requesting side # it at the requesting side
for function in stack: for function in stack:
fspath, lineno = getfslineno(function) fspath, lineno = getfslineno(function)
lines, _ = inspect.getsourcelines(function) try:
addline("file %s, line %s" % (fspath, lineno+1)) lines, _ = inspect.getsourcelines(function)
for i, line in enumerate(lines): except IOError:
line = line.rstrip() error_msg = "file %s, line %s: source code not available"
addline(" " + line) addline(error_msg % (fspath, lineno+1))
if line.lstrip().startswith('def'): else:
break addline("file %s, line %s" % (fspath, lineno+1))
for i, line in enumerate(lines):
line = line.rstrip()
addline(" " + line)
if line.lstrip().startswith('def'):
break
if msg is None: if msg is None:
fm = self.request._fixturemanager fm = self.request._fixturemanager

View File

@ -353,6 +353,23 @@ class TestGeneralUsage:
*unrecognized* *unrecognized*
""") """)
def test_getsourcelines_error_issue553(self, testdir):
p = testdir.makepyfile("""
def raise_error(obj):
raise IOError('source code not available')
import inspect
inspect.getsourcelines = raise_error
def test_foo(invalid_fixture):
pass
""")
res = testdir.runpytest(p)
res.stdout.fnmatch_lines([
"*source code not available*",
"*fixture 'invalid_fixture' not found",
])
class TestInvocationVariants: class TestInvocationVariants:
def test_earlyinit(self, testdir): def test_earlyinit(self, testdir):