Skip to content

Commit e938580

Browse files
committed
check for user-generated exceptions
1 parent 47c0fc3 commit e938580

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/_pytest/assertion/util.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,14 @@ def isiterable(obj: Any) -> bool:
132132
try:
133133
iter(obj)
134134
return not istext(obj)
135-
except TypeError:
136-
return False
135+
except Exception as e:
136+
if (
137+
isinstance(e, TypeError)
138+
and (len(e.args) == 1)
139+
and "iter() returned non-iterator of type" in str(e.args[0])
140+
):
141+
return False
142+
raise ValueError(f"Unexpected exception {e!r} while testing object {obj!r}")
137143

138144

139145
def has_default_eq(
@@ -195,13 +201,20 @@ def assertrepr_compare(
195201
explanation = _notin_text(left, right, verbose)
196202
except outcomes.Exit:
197203
raise
198-
except Exception:
199-
explanation = [
200-
"(pytest_assertion plugin: representation of details failed: {}.".format(
201-
_pytest._code.ExceptionInfo.from_current()._getreprcrash()
202-
),
203-
" Probably an object has a faulty __repr__.)",
204-
]
204+
except Exception as e:
205+
if (
206+
isinstance(e, ValueError)
207+
and (len(e.args) == 1)
208+
and ("Unexpected exception" in str(e.args[0]))
209+
):
210+
explanation = [e.args[0]]
211+
else:
212+
explanation = [
213+
"(pytest_assertion plugin: representation of details failed: {}.".format(
214+
_pytest._code.ExceptionInfo.from_current()._getreprcrash()
215+
),
216+
" Probably an object has a faulty __repr__.)",
217+
]
205218

206219
if not explanation:
207220
return None

0 commit comments

Comments
 (0)