Report the offending char, not the backslash, for invalid \X escapes - #380
Merged
etrepum merged 1 commit intoJul 6, 2026
Merged
Conversation
For an invalid single-character escape, the C scanstring passed end - 2 (the backslash) to raise_errmsg for ERR_STRING_ESC1, while py_scanstring reports end - 1 (the escape character). Since ERR_STRING_ESC1 renders %r of the character at the reported position, the C path produced the nonsensical "Invalid \\X escape sequence '\\'" - naming the backslash, which is always a valid escape introducer - and pointed at the wrong column. Pass end - 1 in all three scanstring branches so the C extension names the actual offending character and reports the same position as the pure-Python reference. This mirrors the control-character position fix in simplejson#379.
etrepum
approved these changes
Jul 6, 2026
netbsd-srcmastr
pushed a commit
to NetBSD/pkgsrc
that referenced
this pull request
Aug 28, 2026
Version 4.1.2 released 2026-08-26 * Fix control character error position when content precedes the control char simplejson/simplejson#379 * Report the offending char, not the backslash, for invalid \X escapes simplejson/simplejson#380 * Handle non-finite Decimals like floats in the encoder simplejson/simplejson#381 * Parse failures due to trailing commas are now reported consistently across implementations simplejson/simplejson#382
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
For an invalid single-character escape (
\X), the Cscanstringreports theposition of the backslash and renders the wrong character in the message,
while the pure-Python
py_scanstringreports the offending escape character:ERR_STRING_ESC1renders%rof the character at the reported position, sopassing
end - 2names'\'— the backslash, which is always a valid escapeintroducer — and points at the wrong column. This is actively misleading when
debugging malformed JSON through the C-accelerated path.
Cause
simplejson/_speedups.cpassesend - 2(the backslash) instead ofend - 1(the escape character) to
raise_errmsg(state, ERR_STRING_ESC1, ...)in allthree scanstring branches (
scanstring_strand bothscanstring_unicodeincludes).
Fix
Pass
end - 1in the three branches so the C extension names the actualoffending character and reports the same position as the pure-Python reference.
This mirrors the control-character position fix in #379.
Tests
test_single_char_escape_error_parityasserts exact(position, message)parity between
c_scanstringandpy_scanstringacross five invalid-escapeinputs, and that the reported position never lands on the backslash. It mirrors
the existing
test_escape_error_parity(which covers the\uXXXXbranch).Full test suite (C-speedups path and pure-Python path): 190 passed, 30 skipped,
0 regressions.