Fall back to repr() when __pt_repr__ yields malformed fragments - #632
Open
chiliec wants to merge 1 commit into
Open
Fall back to repr() when __pt_repr__ yields malformed fragments#632chiliec wants to merge 1 commit into
chiliec wants to merge 1 commit into
Conversation
Inspecting a unittest.mock.MagicMock result (e.g. m.method_calls[0]) crashed the REPL with 'ValueError: not enough values to unpack (expected at least 2, got 0)'. A MagicMock fabricates any attribute, so hasattr(result, '__pt_repr__') is True; calling it returns more mocks, and to_formatted_text then produces malformed fragments such as an empty tuple, which split_lines later fails to unpack. Validate that every fragment from __pt_repr__ is a well-formed (style, text) tuple before trusting it; otherwise fall through to the normal repr() path. Adds a regression test. Fixes prompt-toolkit#610.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #610.
Problem
Inspecting a
unittest.mock.MagicMockresult crashes the REPL:Root cause
In
OutputPrinter._format_result_output, a result carrying__pt_repr__is trusted to return prompt_toolkit formatted text. AMagicMockfabricates any attribute, sohasattr(result, "__pt_repr__")isTrue; calling it returns further mocks, andto_formatted_text(...)then produces a malformed fragment list such as:The empty tuple
()has no(style, text)to unpack, so the latersplit_linespass raisesValueError.Fix
After building the formatted text from
__pt_repr__, verify that every fragment is a well-formed(style, text, …)tuple before yielding it. If any fragment is malformed, fall through to the normalrepr()path (which lexescall.get()into valid fragments). This keeps__pt_repr__support intact for well-behaved objects and only guards against fabricated / broken implementations.Test
Adds
tests/test_printer.py, which feedsMagicMock().method_calls[0]through_format_result_outputand asserts the fragments are all valid tuples,split_linesdoes not raise, and the rendered text is the object’srepr.Verification (local, Python 3.11)
pytest), andruff checkon the changed files is clean.