[Pytest-commit] commit/pytest: nicoddemus: Handle inspect.getsourcelines failures in FixtureLookupError

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Thu Apr 16 00:32:16 CEST 2015


1 new commit in pytest:

https://bitbucket.org/pytest-dev/pytest/commits/b820fe775929/
Changeset:   b820fe775929
Branch:      getsourcelines-error-issue-553-pytest2.7
User:        nicoddemus
Date:        2015-04-15 22:31:22+00:00
Summary:     Handle inspect.getsourcelines failures in FixtureLookupError

Fixes #553
Affected #:  3 files

diff -r e42f106823e9f453005a626744d7ac234a05938a -r b820fe7759295097225ee633c488df4fdbc2e239 CHANGELOG
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,11 @@
 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
   independently from ordering of fixture arguments.  Also
   avoid the pytest internal traceback which does not provide

diff -r e42f106823e9f453005a626744d7ac234a05938a -r b820fe7759295097225ee633c488df4fdbc2e239 _pytest/python.py
--- a/_pytest/python.py
+++ b/_pytest/python.py
@@ -1537,13 +1537,18 @@
                                # it at the requesting side
         for function in stack:
             fspath, lineno = getfslineno(function)
-            lines, _ = inspect.getsourcelines(function)
-            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
+            try:
+                lines, _ = inspect.getsourcelines(function)
+            except IOError:
+                error_msg = "file %s, line %s: source code not available"
+                addline(error_msg % (fspath, lineno+1))
+            else:
+                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:
             fm = self.request._fixturemanager

diff -r e42f106823e9f453005a626744d7ac234a05938a -r b820fe7759295097225ee633c488df4fdbc2e239 testing/acceptance_test.py
--- a/testing/acceptance_test.py
+++ b/testing/acceptance_test.py
@@ -353,6 +353,23 @@
             *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:
     def test_earlyinit(self, testdir):

Repository URL: https://bitbucket.org/pytest-dev/pytest/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.


More information about the pytest-commit mailing list