Title: json: C decoder reports "Invalid \uXXXX escape" instead of "Unterminated string starting at" for a complete \uXXXX escape at end of input · Issue #152052 · python/cpython · GitHub
Open Graph Title: json: C decoder reports "Invalid \uXXXX escape" instead of "Unterminated string starting at" for a complete \uXXXX escape at end of input · Issue #152052 · python/cpython
X Title: json: C decoder reports "Invalid \uXXXX escape" instead of "Unterminated string starting at" for a complete \uXXXX escape at end of input · Issue #152052 · python/cpython
Description: The C accelerator of json reports Invalid \uXXXX escape where the pure-Python decoder reports Unterminated string starting at, for a \uXXXX escape whose final hex digit is the last character of the input. Minimal repro The input is an op...
Open Graph Description: The C accelerator of json reports Invalid \uXXXX escape where the pure-Python decoder reports Unterminated string starting at, for a \uXXXX escape whose final hex digit is the last character of the...
X Description: The C accelerator of json reports Invalid \uXXXX escape where the pure-Python decoder reports Unterminated string starting at, for a \uXXXX escape whose final hex digit is the last character of the...
Opengraph URL: https://github.com/python/cpython/issues/152052
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"json: C decoder reports \"Invalid \\uXXXX escape\" instead of \"Unterminated string starting at\" for a complete \\uXXXX escape at end of input","articleBody":"The C accelerator of `json` reports `Invalid \\uXXXX escape` where the pure-Python decoder reports `Unterminated string starting at`, for a `\\uXXXX` escape whose final hex digit is the last character of the input.\n\n### Minimal repro\n\nThe input is an opening quote followed by a complete `A` escape and **no** closing quote (7 characters: `\"`, `\\`, `u`, `0`, `0`, `4`, `1`):\n\n```python\n>>> import json\n>>> json.loads(r'\"\\u0041') # C accelerator\nTraceback (most recent call last):\n ...\njson.decoder.JSONDecodeError: Invalid \\uXXXX escape: line 1 column 3 (char 2)\n\n>>> from json import decoder\n>>> decoder.py_scanstring(r'\"\\u0041', 1) # pure-Python\nTraceback (most recent call last):\n ...\njson.decoder.JSONDecodeError: Unterminated string starting at: line 1 column 1 (char 0)\n```\n\nThe four hex digits `0041` form a complete, valid `\\uXXXX` escape, so the string is merely *unterminated*. The pure-Python decoder diagnoses this correctly; the C accelerator misreports it as an *invalid escape*.\n\n### Root cause\n\n`Modules/_json.c`, in `scanstring_unicode()`:\n\n```c\nend = next + 4;\nif (end >= len) {\n raise_errmsg(\"Invalid \\\\uXXXX escape\", pystr, next - 1);\n goto bail;\n}\n```\n\nThe four hex digits are read at indices `next .. next+3`, i.e. `end-4 .. end-1`. When `end == len` those indices are `len-4 .. len-1` — all in bounds — so the escape is complete and valid; the input is simply unterminated. The check should be `>` rather than `>=`: only `end > len` means the escape itself runs past the end of the input. At `end == len` control should fall through, the forward scan finds no closing quote, and the existing `raise_errmsg(\"Unterminated string starting at\", ...)` fires, matching the pure-Python decoder exactly.\n\n### Why this matters\n\nThis is a C-accelerator vs pure-Python parity / wrong-diagnostic bug: the two decoders disagree on the error class for the same input, and the C one points the user at the wrong problem (a non-existent invalid escape rather than the missing closing quote). The `json` C and pure-Python error messages were deliberately synchronized in bpo-5067, so divergence here regresses that contract.\n\nThis is distinct from gh-125660 (which is about the pure-Python decoder *accepting* invalid unicode escapes); here both decoders correctly reject genuinely truncated escapes, and the only divergence is the misclassification of a *complete* escape at end-of-input.\n\nI have a one-character fix (`>=` → `>`) plus regression tests covering both the C and pure-Python decoders ready to submit.\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-152053\n* gh-152283\n* gh-152284\n* gh-152285\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/tonghuaroot","@type":"Person","name":"tonghuaroot"},"datePublished":"2026-06-24T07:19:30Z","url":"https://github.com/python/cpython/issues/152052"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:a30434c4-e0d7-f15b-b439-50919b4ae854 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A8AA:785A9:4636CF5:58F7148:6B023AA1 |
| html-safe-nonce | a6d22db1614062d54e9bd7e161b8e6d911ddee791fdd62d2e78fa8b601017ccc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBOEFBOjc4NUE5OjQ2MzZDRjU6NThGNzE0ODo2QjAyM0FBMSIsInZpc2l0b3JfaWQiOiI2OTk3ODYzNzk0MjUyODU1OTY5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | a3491277a8bfd0e5f06a865ad36318c90844aab8c0ec5667a0994ffd0b283a11 |
| hovercard-subject-tag | issue:4732605164 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/152052/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0cb6b6860b54f3cbdbc410debe9d7ae6346ea5409fc3f1983b8a15ca073c405f/python/cpython/issues/152052 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0cb6b6860b54f3cbdbc410debe9d7ae6346ea5409fc3f1983b8a15ca073c405f/python/cpython/issues/152052 |
| og:image:alt | The C accelerator of json reports Invalid \uXXXX escape where the pure-Python decoder reports Unterminated string starting at, for a \uXXXX escape whose final hex digit is the last character of the... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tonghuaroot |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width