Merge pull request #1205 from The-Compiler/reportinfo-getattr

Fix getting line number with nasty __getattr__. fixes #1204
This commit is contained in:
Ronny Pfannschmidt 2015-11-30 17:23:05 +01:00
commit 3a3f0f5c56
3 changed files with 21 additions and 2 deletions

View File

@ -6,6 +6,9 @@
module. Thanks Mikhail Chernykh for the report and Bruno Oliveira for the
PR.
- fix #1204: another error when collecting with a nasty __getattr__().
Thanks Florian Bruhin for the PR.
2.8.3
-----

View File

@ -384,12 +384,13 @@ class PyobjMixin(PyobjContext):
def reportinfo(self):
# XXX caching?
obj = self.obj
if hasattr(obj, 'compat_co_firstlineno'):
compat_co_firstlineno = getattr(obj, 'compat_co_firstlineno', None)
if isinstance(compat_co_firstlineno, int):
# nose compatibility
fspath = sys.modules[obj.__module__].__file__
if fspath.endswith(".pyc"):
fspath = fspath[:-1]
lineno = obj.compat_co_firstlineno
lineno = compat_co_firstlineno
else:
fspath, lineno = getfslineno(obj)
modpath = self.getmodpath()

View File

@ -880,6 +880,21 @@ class TestReportInfo:
pass
"""
def test_reportinfo_with_nasty_getattr(self, testdir):
# https://github.com/pytest-dev/pytest/issues/1204
modcol = testdir.getmodulecol("""
# lineno 0
class TestClass:
def __getattr__(self, name):
return "this is not an int"
def test_foo(self):
pass
""")
classcol = testdir.collect_by_name(modcol, "TestClass")
instance = classcol.collect()[0]
fspath, lineno, msg = instance.reportinfo()
def test_customized_python_discovery(testdir):
testdir.makeini("""