From e938580257e8120dabbea789d5b7a3dd3354d0a3 Mon Sep 17 00:00:00 2001 From: Reagan Lee <96998476+reaganjlee@users.noreply.github.com> Date: Thu, 10 Aug 2023 17:37:48 -0700 Subject: [PATCH] check for user-generated exceptions --- src/_pytest/assertion/util.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/_pytest/assertion/util.py b/src/_pytest/assertion/util.py index fc5dfdbd5ba..47244b688ac 100644 --- a/src/_pytest/assertion/util.py +++ b/src/_pytest/assertion/util.py @@ -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( @@ -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