Skip to content

Handle broken repr in tracing - #666

Open
ShipItAndPray wants to merge 1 commit into
pytest-dev:mainfrom
ShipItAndPray:fix-424-safe-tracing
Open

Handle broken repr in tracing#666
ShipItAndPray wants to merge 1 commit into
pytest-dev:mainfrom
ShipItAndPray:fix-424-safe-tracing

Conversation

@ShipItAndPray

Copy link
Copy Markdown

Summary

  • guard tracing stringification so hook tracing does not break hook execution when traced objects have a broken __repr__/__str__
  • keep the fallback output informative by using the default object repr and the original exception
  • add a regression test covering broken repr values in traced hook kwargs

Testing

  • uv run --python 3.12 pytest testing/test_pluginmanager.py -k broken_repr -q
  • uv run --python 3.12 pytest testing/test_pluginmanager.py -q

Closes #424.

@RonnyPfannschmidt RonnyPfannschmidt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is in part ad odds with switching to repr in another pr
also as far as i understood there is a general understanding that str/repr should never fail for object that completed their ctor

RonnyPfannschmidt added a commit to RonnyPfannschmidt/pluggy that referenced this pull request Sep 12, 2026
Tracing could turn a working hook call into a failing one in two ways:
an object whose __repr__/__str__ raises propagated that exception out of
the hook call (pytest-dev#424), and a lone surrogate in a hook argument or return
value produced a message the writer could not encode (pytest-dev#681).

Both are now handled in one place. _safe_repr()/_safe_str() wrap the
conversion the way pytest's saferepr does -- KeyboardInterrupt and
SystemExit still propagate, anything else is rendered as an
unpresentable-object marker -- and escape lone surrogates with
backslashreplace afterwards, which also covers surrogates that come out
of an object's own __repr__.

Traced values -- hook kwargs and the hook result -- now use repr() so
their type is visible in the log; structural labels such as the hook
name and the finish/--> markers keep using str() and stay unquoted.

Supersedes pytest-dev#627, pytest-dev#666, pytest-dev#684 and pytest-dev#716.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Code <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid erroring from tracing

3 participants