Skip to content

Commit

Permalink
check for user-generated exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
reaganjlee committed Aug 11, 2023
1 parent 47c0fc3 commit e938580
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/_pytest/assertion/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,14 @@ def isiterable(obj: Any) -> bool:
try:
iter(obj)
return not istext(obj)
except TypeError:
return False
except Exception as e:
if (
isinstance(e, TypeError)
and (len(e.args) == 1)
and "iter() returned non-iterator of type" in str(e.args[0])
):
return False
raise ValueError(f"Unexpected exception {e!r} while testing object {obj!r}")


def has_default_eq(
Expand Down Expand Up @@ -195,13 +201,20 @@ def assertrepr_compare(
explanation = _notin_text(left, right, verbose)
except outcomes.Exit:
raise
except Exception:
explanation = [
"(pytest_assertion plugin: representation of details failed: {}.".format(
_pytest._code.ExceptionInfo.from_current()._getreprcrash()
),
" Probably an object has a faulty __repr__.)",
]
except Exception as e:
if (
isinstance(e, ValueError)
and (len(e.args) == 1)
and ("Unexpected exception" in str(e.args[0]))
):
explanation = [e.args[0]]
else:
explanation = [
"(pytest_assertion plugin: representation of details failed: {}.".format(
_pytest._code.ExceptionInfo.from_current()._getreprcrash()
),
" Probably an object has a faulty __repr__.)",
]

if not explanation:
return None
Expand Down

0 comments on commit e938580

Please sign in to comment.