Fix out-of-range when truncating a long exception message - #1929
Open
simonbeyer1 wants to merge 1 commit into
Open
simonbeyer1 wants to merge 1 commit into
simonbeyer1 wants to merge 1 commit into
Conversation
conditionMessage_from_py_exception() keeps the first two lines of a long
message and appends the tail. The start of that tail was computed as
error.size() - max_msg_len + head.size() + trunc.size() + 20, which is past
the end of the string whenever the first two lines alone exceed
getOption("warning.length"). std::string::substr then throws, and the R error
becomes basic_string::substr: __pos > this->size() instead of the Python
message.
A SyntaxError hits this reliably, because Python embeds the offending source
line in the message: any expression longer than the message budget turns its
own SyntaxError into an out_of_range.
Clamp the head to the budget and derive the tail start from the string size.
Messages that already truncated correctly come out byte for byte the same.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This branch has not been deployed
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.
Problem
conditionMessage_from_py_exception()insrc/python.cpptruncates a Pythonexception message that exceeds
getOption("warning.length"): it keeps the firsttwo lines, inserts
<...truncated...>, and appends the tail. The tail start iswhich lands past the end of the string whenever the first two lines alone fill
the budget, that is whenever
head.size() + trunc.size() + 20 > max_msg_len.std::string::substrthen throwsstd::out_of_range, Rcpp turns that into theR error, and the Python message is lost:
A
SyntaxErrorreaches it reliably, because Python embeds the offending sourceline in the message, so any expression longer than the budget turns its own
SyntaxErrorinto anout_of_range:Both are on reticulate 1.46.0, and the code is unchanged on
main.We hit this through SymPy's
parse_expr: a long equation made every parsefailure arrive in R as
basic_string::substrinstead of the reason, which tooka while to trace back to the message formatter.
Fix
Compute the budget once, drop the head when it does not fit, and derive the tail
start from
error.size()so it cannot run past the end. The arithmetic isotherwise unchanged:
error.size() - (budget - head.size())is the sameposition as
over + head.size() + trunc.size() + 20, only written so it cannotexceed the string. Messages that already truncated correctly come out byte for
byte identical, checked against 1.46.0 for a two-line message, a single-line
message, a short message and one under the limit.
first_line_end_posandsecond_line_start_posalso becomestd::size_t. Asintthey heldnposas-1, which happened to work throughsubstr(0, -1 + 1), but only by accident.Test
tests/testthat/test-python-exceptions.Rgains a case for both shapes. It failson the current code and passes with the fix.