Skip to content

Escape surrogate values in tracing output - #684

Closed
Himanshuagrawal4 wants to merge 1 commit into
pytest-dev:mainfrom
Himanshuagrawal4:PRO/pluggy-safe-tracing-surrogates
Closed

Himanshuagrawal4 wants to merge 1 commit into
pytest-dev:mainfrom
Himanshuagrawal4:PRO/pluggy-safe-tracing-surrogates

Conversation

@Himanshuagrawal4

Copy link
Copy Markdown

Summary

  • Use
    epr() for hook kwarg values in trace output so surrogate characters are escaped safely.
  • Pre-format hook return values before writing tracing output while keeping structural trace labels readable.
  • Add regression tests for surrogate values in hook arguments and return values.

Tests

  • git diff --check
  • PYTHONPATH=src python -m pytest testing\test_tracer.py::test_dictargs_escape_surrogate_values testing\test_pluginmanager.py::test_hook_tracing_escapes_surrogate_values -q (2 passed)
  • PYTHONPATH=src python -m pytest testing\test_tracer.py testing\test_pluginmanager.py -q (49 passed)

Fixes #681.

@Himanshuagrawal4
Himanshuagrawal4 marked this pull request as ready for review June 4, 2026 19:35
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) <[email protected]>
Co-Authored-By: Claude Code <[email protected]>
RonnyPfannschmidt added a commit to RonnyPfannschmidt/pluggy that referenced this pull request Sep 23, 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) <[email protected]>
Co-Authored-By: Claude Code <[email protected]>
RonnyPfannschmidt added a commit to RonnyPfannschmidt/pluggy that referenced this pull request Sep 24, 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() catch
Exception from the conversion and render an unpresentable-object marker
in the shape of pytest's saferepr, naming the exception, or only its
type when the exception's own repr fails too, so no user code runs in the
last fallback. Anything outside Exception -- KeyboardInterrupt,
SystemExit, pytest's outcome exceptions -- propagates: a __str__ or
__repr__ that raises one of those is a bug in that object, not something
tracing should hide. Lone surrogates are escaped 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) <[email protected]>
Co-Authored-By: Claude Code <[email protected]>
@RonnyPfannschmidt

Copy link
Copy Markdown
Member

Written by Claude Opus 5.5 via Claude Code for the PR author; I prompted it, it did the work, I read it.

Superseded by #728, which is now merged and carries this PR's contribution. See the "Relation to the existing PRs" section there for what was taken from each. Thank you. Closing.

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.

Tracing crashes with UnicodeEncodeError on surrogate escapes in hook arguments/results

2 participants