Title: Wrong `SyntaxError.offset` for "Non-UTF-8 code starting with ..." when a non-ASCII character precedes the invalid byte · Issue #157378 · python/cpython · GitHub
Open Graph Title: Wrong `SyntaxError.offset` for "Non-UTF-8 code starting with ..." when a non-ASCII character precedes the invalid byte · Issue #157378 · python/cpython
X Title: Wrong `SyntaxError.offset` for "Non-UTF-8 code starting with ..." when a non-ASCII character precedes the invalid byte · Issue #157378 · python/cpython
Description: Bug report Bug description: SyntaxError.offset and SyntaxError.end_offset for the error "Non-UTF-8 code starting with ..." are too small when a valid multi-byte character precedes the invalid byte on the same line. Reproduction compile(b...
Open Graph Description: Bug report Bug description: SyntaxError.offset and SyntaxError.end_offset for the error "Non-UTF-8 code starting with ..." are too small when a valid multi-byte character precedes the invalid byte ...
X Description: Bug report Bug description: SyntaxError.offset and SyntaxError.end_offset for the error "Non-UTF-8 code starting with ..." are too small when a valid multi-byte character precedes the inv...
Opengraph URL: https://github.com/python/cpython/issues/157378
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Wrong `SyntaxError.offset` for \"Non-UTF-8 code starting with ...\" when a non-ASCII character precedes the invalid byte","articleBody":"# Bug report\n\n### Bug description:\n\n`SyntaxError.offset` and `SyntaxError.end_offset` for the error\n\"Non-UTF-8 code starting with ...\" are too small when a valid multi-byte\ncharacter precedes the invalid byte on the same line.\n\n## Reproduction\n\n```python\ncompile(b'\\xc3\\xa9X\\x80', '\u003cbug>', 'exec')\n```\n\nSame via a file:\n\n```console\n$ printf '\\xc3\\xa9X\\x80' > bug.py\n$ ./python bug.py\n```\n\nThe input is `\"éX\"` followed by the invalid byte `0x80`, with no encoding\ncookie. The invalid byte is the 3rd character of line 1.\n\n## Expected\n\n```console\n File \"\u003cbug>\", line 1\n éX\n ^\nSyntaxError: Non-UTF-8 code starting with '\\x80' on line 1, but no encoding declared; ...\n```\n\n`offset == 3`, `end_offset == 3` (1-based character column).\n\n## Actual\n\n```console\n File \"\u003cbug>\", line 1\n éX\n ^\nSyntaxError: Non-UTF-8 code starting with '\\x80' on line 1, but no encoding declared; ...\n```\n\n`offset == 2`, `end_offset == 2`.\n\nObserved values:\n\n| build | offset | end_offset |\n|-----------------------------|--------|------------|\n| main (575fe3914f4) | 2 | 2 |\n| 3.14.7 | 3 | 3 |\n| 3.15.0rc2+dev (e325fae3578) | 3 | 3 |\n\nMore inputs on main, all one column short per preceding multi-byte character:\n\n```python\ncompile(b'\\xc3\\xa9abc\\x80', ...) # offset 4, expected 5\ncompile(b'\\t\\xc3\\xa9X\\x80', ...) # offset 3, expected 4\ncompile(b'a\\xc3\\xa9b\\x80c', ...) # offset 3, expected 4\n```\n\n## Cause\n\n`_PyTokenizer_ensure_utf8()` (Parser/tokenizer/helpers.c) computes a 1-based\ncharacter column and passes it to `_PyTokenizer_syntaxerror_known_range()`:\n\n _PyTokenizer_syntaxerror_known_range(tok,\n col_offset + 1, col_offset + 1, ...)\n\nSince 59a691361cb (gh-156894, PR #156901), `_syntaxerror_range()` converts its\n`col_offset`/`end_col_offset` arguments from bytes to characters with\n`byte_col_to_char_col()`, so the character column from `ensure_utf8()` is\nconverted a second time.\n\nThe same function is unchanged on current main (b1e7554ac1e).\n\n## Suggested fix\n\nPass byte columns from `ensure_utf8()`, e.g. `(int)(badchar - line_start) + 1`\nfor both arguments, and let `_syntaxerror_range()` do the byte-to-character\nconversion.\n\n## Tests\n\n`Lib/test/test_source_encoding.py` only checks the message text for this error;\nno test checks `offset`/`end_offset`, so this case is not covered.\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-157412\n* gh-157489\n* gh-158123\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/ashm-dev","@type":"Person","name":"ashm-dev"},"datePublished":"2026-09-12T20:12:35Z","url":"https://github.com/python/cpython/issues/157378"}
| 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:282ac904-b79f-a804-0767-8007687077e6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AAD0:1EFF:8C4C2E:BAF62C:6B01FA16 |
| html-safe-nonce | 1e5d916a924f4b9c354648da5a2daa5bddcb2b8b48236f3032f97440c7303f59 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQUQwOjFFRkY6OEM0QzJFOkJBRjYyQzo2QjAxRkExNiIsInZpc2l0b3JfaWQiOiIzMjkzMjA1ODcwMDkxMDQxMzAyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 9d6c62734f9b2eb0be70561dee22e97a75cd31bcec21c4df7e8b610ca974abf0 |
| hovercard-subject-tag | issue:5435737918 |
| 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/157378/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8043d6636dad4517c91fe2ae6bfe708452946db2f9b3cde6c6015528c6ea99a5/python/cpython/issues/157378 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8043d6636dad4517c91fe2ae6bfe708452946db2f9b3cde6c6015528c6ea99a5/python/cpython/issues/157378 |
| og:image:alt | Bug report Bug description: SyntaxError.offset and SyntaxError.end_offset for the error "Non-UTF-8 code starting with ..." are too small when a valid multi-byte character precedes the invalid byte ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ashm-dev |
| 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