Skip to content

Commit b2042be

Browse files
committed
Factor out _safe_isclass
1 parent 70686d6 commit b2042be

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/_pytest/python.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,21 @@ def pytest_pycollect_makemodule(path, parent):
188188
return Module(path, parent)
189189

190190

191+
def _safe_isclass(obj):
192+
try:
193+
return isclass(obj)
194+
except Exception:
195+
return False
196+
197+
191198
@hookimpl(hookwrapper=True)
192199
def pytest_pycollect_makeitem(collector, name, obj):
193200
outcome = yield
194201
res = outcome.get_result()
195202
if res is not None:
196203
return
197204
# nothing was collected elsewhere, let's do it here
198-
try:
199-
is_class = isclass(obj)
200-
except Exception:
201-
is_class = False
202-
if is_class:
205+
if _safe_isclass(obj):
203206
if collector.istestclass(obj, name):
204207
Class = collector._getcustomclass("Class")
205208
outcome.force_result(Class(name, parent=collector))

testing/test_collection.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,3 +1004,21 @@ def test_1():
10041004
result = testdir.runpytest()
10051005
assert result.ret == 0
10061006
result.stdout.fnmatch_lines(["*1 passed in*"])
1007+
1008+
1009+
def test__safe_isclass():
1010+
from _pytest.python import _safe_isclass
1011+
1012+
assert _safe_isclass(type) is True
1013+
1014+
# Simulates Django's django.conf.settings.
1015+
class ImproperlyConfigured(Exception):
1016+
pass
1017+
1018+
class RaisesOnGetAttr(object):
1019+
def raises(self):
1020+
raise ImproperlyConfigured
1021+
1022+
__class__ = property(raises)
1023+
1024+
assert _safe_isclass(RaisesOnGetAttr()) is False

0 commit comments

Comments
 (0)