René's URL Explorer Experiment


Title: Replace malformed new_unicode_decode_error / new_unicode_encode_error helpers with the _real variants · Issue #8352 · RustPython/RustPython · GitHub

Open Graph Title: Replace malformed new_unicode_decode_error / new_unicode_encode_error helpers with the _real variants · Issue #8352 · RustPython/RustPython

X Title: Replace malformed new_unicode_decode_error / new_unicode_encode_error helpers with the _real variants · Issue #8352 · RustPython/RustPython

Description: Summary VirtualMachine::new_unicode_decode_error (and its sibling new_unicode_encode_error) construct a UnicodeDecodeError that is structurally invalid: it has none of the five attributes that a UnicodeDecodeError must have (encoding, ob...

Open Graph Description: Summary VirtualMachine::new_unicode_decode_error (and its sibling new_unicode_encode_error) construct a UnicodeDecodeError that is structurally invalid: it has none of the five attributes that a Un...

X Description: Summary VirtualMachine::new_unicode_decode_error (and its sibling new_unicode_encode_error) construct a UnicodeDecodeError that is structurally invalid: it has none of the five attributes that a Un...

Opengraph URL: https://github.com/RustPython/RustPython/issues/8352

X: @github

direct link

Domain: Github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Replace malformed new_unicode_decode_error / new_unicode_encode_error helpers with the _real variants","articleBody":"## Summary\n\n`VirtualMachine::new_unicode_decode_error` (and its sibling `new_unicode_encode_error`) construct a `UnicodeDecodeError` that is **structurally invalid**: it has none of the five attributes that a `UnicodeDecodeError` must have (`encoding`, `object`, `start`, `end`, `reason`), and its `args` is a 1‑tuple holding a plain message string. A correct helper — `new_unicode_decode_error_real` — already exists, and both macro-generated helpers are already marked for removal:\n\n```rust\n// crates/vm/src/vm/vm_new.rs\n// TODO: remove \u0026 replace with new_unicode_decode_error_real\ndefine_exception_fn!(fn new_unicode_decode_error, unicode_decode_error, UnicodeDecodeError);\n\n// TODO: remove \u0026 replace with new_unicode_encode_error_real\ndefine_exception_fn!(fn new_unicode_encode_error, unicode_encode_error, UnicodeEncodeError);\n```\n\nThis issue tracks replacing the remaining call sites of the broken helpers with the `_real` variants and removing the broken helpers.\n\n## Why the macro version is wrong\n\n`new_unicode_decode_error` is generated by the `define_exception_fn!` macro, which just calls `new_exception_msg`:\n\n```rust\npub fn $fn_name(\u0026self, msg: impl Into\u003cWtf8Buf\u003e) -\u003e PyBaseExceptionRef {\n    let err = self.ctx.exceptions.$attr.to_owned();\n    self.new_exception_msg(err, msg.into())   // -\u003e new_exception(type, vec![msg_str])\n}\n```\n\n`new_exception` builds the exception via `PyBaseException::new(args, vm)`, which only stores `args`. It **does not run `slot_init`**, so `PyUnicodeDecodeError`'s initializer — the only thing that sets `encoding`/`object`/`start`/`end`/`reason` — never runs. The resulting object is a `UnicodeDecodeError` with `args = (\"some message\",)` and no unicode-error attributes at all.\n\n`new_unicode_decode_error_real` instead passes the full 5-tuple and sets every attribute, matching what `UnicodeDecodeError(encoding, object, start, end, reason)` produces.\n\nNote that in CPython this object cannot even be constructed — `UnicodeDecodeError` requires exactly 5 arguments:\n\n```\n\u003e\u003e\u003e UnicodeDecodeError('csv not utf8')\nTypeError: function takes exactly 5 arguments (1 given)\n```\n\n## Reproduction\n\n`csv.reader(..., quoting=csv.QUOTE_STRINGS)` reaches `crates/stdlib/src/csv.rs:1113`, which uses the broken helper:\n\n```python\nimport csv\nr = csv.reader(['\\udcff'], quoting=csv.QUOTE_STRINGS)\ntry:\n    next(r)\nexcept UnicodeDecodeError as e:\n    print(\"str(e) =\", repr(str(e)))\n    print(\"args   =\", e.args)\n    for attr in (\"encoding\", \"object\", \"start\", \"end\", \"reason\"):\n        try:\n            print(\" \", attr, \"=\", repr(getattr(e, attr)))\n        except AttributeError:\n            print(\" \", attr, \"-\u003e AttributeError\")\n```\n\nRustPython output:\n\n```\nstr(e) = ''\nargs   = ('csv not utf8',)\n  encoding -\u003e AttributeError\n  object -\u003e AttributeError\n  start -\u003e AttributeError\n  end -\u003e AttributeError\n  reason -\u003e AttributeError\n```\n\nTwo concrete symptoms:\n\n1. **The diagnostic message is silently lost.** `UnicodeDecodeError.__str__` returns an empty string when the `object` attribute is missing, so `str(e)` is `''` instead of the message that was passed in.\n2. **Attribute access raises `AttributeError`.** Any error handler that inspects `e.encoding` / `e.start` / `e.reason` (as the `codecs` error-handler machinery and normal user code do) breaks. A genuine `UnicodeDecodeError` always has these attributes.\n\nFor comparison, a well-formed instance (CPython, and RustPython via `new_unicode_decode_error_real`):\n\n```\nstr(e) = \"'utf-8' codec can't decode byte 0xff in position 0: invalid start byte\"\nargs   = ('utf-8', b'\\xff', 0, 1, 'invalid start byte')\nencoding = utf-8 | object = b'\\xff' | start = 0 | end = 1 | reason = 'invalid start byte'\n```\n\n## Call sites to convert\n\nCorrect (already using `_real`) — for reference:\n\n- `crates/vm/src/codecs.rs:805`\n- `crates/vm/src/stdlib/_codecs.rs:1023`, `:1114`, `:1125`\n- `crates/capi/src/pyerrors.rs:353`\n\nBroken — `new_unicode_decode_error(msg)`:\n\n- `crates/stdlib/src/csv.rs:1113`, `:1239`, `:1415`, `:1461`, `:1510`, `:1584`\n- `crates/vm/src/stdlib/nt.rs:218`, `:722`, `:724`, `:726`, `:938` (Windows)\n- `crates/stdlib/src/socket.rs:2616`, `:2637`\n- `crates/vm/src/stdlib/_codecs.rs:483`, `:499`, `:608`, `:624` (mbcs/oem, Windows)\n- `crates/vm/src/stdlib/posix.rs:1281`, `:1737`\n- `crates/vm/src/function/fspath.rs:129`\n- `crates/vm/src/stdlib/os.rs:134`\n\nBroken — `new_unicode_encode_error(msg)` (same defect, `UnicodeEncodeError` side; `_real` variant is `new_unicode_encode_error_real`):\n\n- `crates/stdlib/src/array.rs:641`, `:1700`\n- `crates/stdlib/src/socket.rs:2630`\n- `crates/vm/src/stdlib/_codecs.rs:397`, `:431`, `:521`, `:555` (mbcs/oem, Windows)\n\n## Plan\n\n1. At each broken decode call site, supply the real `(encoding, object, start, end, reason)` values and call `new_unicode_decode_error_real`. Most of these already hold the source bytes and the failing offset (or can compute them from `Utf8Error::valid_up_to`); where only a generic reason is available, pass `\"utf-8\"` as encoding, the original bytes as `object`, and the reason string. Do the same for the encode call sites with `new_unicode_encode_error_real` and `(encoding, object: str, start, end, reason)`.\n2. Remove `new_unicode_decode_error` and `new_unicode_encode_error` once their last callers are gone.\n\n## Related\n\n- #8345 — separate but adjacent: even the *correctly* constructed `UnicodeDecodeError` leaks its five attributes into `__dict__` instead of storing them as struct fields.\n\n---\n\n*Investigated and drafted by Claude; reviewed before filing.*\n","author":{"url":"https://github.com/youknowone","@type":"Person","name":"youknowone"},"datePublished":"2026-07-23T03:22:55.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/8352/RustPython/issues/8352"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:4332b461-69b5-2732-89c0-39f15cf8d725
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idDE42:12F7EE:1655221:1E12C39:6A62DD23
html-safe-nonce5599232cc6a84fab42484ccfd7ee7a36a8ba76e424dbf69eca6df39ec38fb086
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERTQyOjEyRjdFRToxNjU1MjIxOjFFMTJDMzk6NkE2MkREMjMiLCJ2aXNpdG9yX2lkIjoiMTUzNjg5NTYxMDIxNzM0MTc5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmace94d29f497f5ab86b9c1c26aeb0c9d650c0244c72418cfa618bc702f50fdccf8
hovercard-subject-tagissue:4955079592
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/RustPython/RustPython/8352/issue_layout
twitter:imagehttps://opengraph.githubassets.com/1bb6896f65cbc994682cc9d56c3bc628c66bcd4f2205a84438e268742e1bd83c/RustPython/RustPython/issues/8352
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/1bb6896f65cbc994682cc9d56c3bc628c66bcd4f2205a84438e268742e1bd83c/RustPython/RustPython/issues/8352
og:image:altSummary VirtualMachine::new_unicode_decode_error (and its sibling new_unicode_encode_error) construct a UnicodeDecodeError that is structurally invalid: it has none of the five attributes that a Un...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameyouknowone
hostnamegithub.com
expected-hostnamegithub.com
Nonedf33b1b61ee7b9a0af988199bfc3503c9c1acafb1f1d40e1f140ea7c84f890dd
turbo-cache-controlno-preview
go-importgithub.com/RustPython/RustPython git https://github.com/RustPython/RustPython.git
octolytics-dimension-user_id39710557
octolytics-dimension-user_loginRustPython
octolytics-dimension-repository_id135201145
octolytics-dimension-repository_nwoRustPython/RustPython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id135201145
octolytics-dimension-repository_network_root_nwoRustPython/RustPython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
released41cd1bdb290013455c0ac430fa755621733f5eb
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://Github.com/RustPython/RustPython/issues/8352#start-of-content
https://Github.com/
Sign in https://Github.com/login?return_to=https%3A%2F%2Fgithub.com%2FRustPython%2FRustPython%2Fissues%2F8352
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
Code QualityEnforce quality at mergehttps://github.com/features/code-quality
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://Github.com/login?return_to=https%3A%2F%2Fgithub.com%2FRustPython%2FRustPython%2Fissues%2F8352
Sign up https://Github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=RustPython%2FRustPython
Reloadhttps://Github.com/RustPython/RustPython/issues/8352
Reloadhttps://Github.com/RustPython/RustPython/issues/8352
Reloadhttps://Github.com/RustPython/RustPython/issues/8352
Please reload this pagehttps://Github.com/RustPython/RustPython/issues/8352
RustPython https://Github.com/RustPython
RustPythonhttps://Github.com/RustPython/RustPython
Notifications https://Github.com/login?return_to=%2FRustPython%2FRustPython
Fork 1.5k https://Github.com/login?return_to=%2FRustPython%2FRustPython
Star 22.2k https://Github.com/login?return_to=%2FRustPython%2FRustPython
Code https://Github.com/RustPython/RustPython
Issues 294 https://Github.com/RustPython/RustPython/issues
Pull requests 105 https://Github.com/RustPython/RustPython/pulls
Discussions https://Github.com/RustPython/RustPython/discussions
Actions https://Github.com/RustPython/RustPython/actions
Projects https://Github.com/RustPython/RustPython/projects
Models https://Github.com/RustPython/RustPython/models
Wiki https://Github.com/RustPython/RustPython/wiki
Security and quality 0 https://Github.com/RustPython/RustPython/security
Insights https://Github.com/RustPython/RustPython/pulse
Code https://Github.com/RustPython/RustPython
Issues https://Github.com/RustPython/RustPython/issues
Pull requests https://Github.com/RustPython/RustPython/pulls
Discussions https://Github.com/RustPython/RustPython/discussions
Actions https://Github.com/RustPython/RustPython/actions
Projects https://Github.com/RustPython/RustPython/projects
Models https://Github.com/RustPython/RustPython/models
Wiki https://Github.com/RustPython/RustPython/wiki
Security and quality https://Github.com/RustPython/RustPython/security
Insights https://Github.com/RustPython/RustPython/pulse
Replace malformed new_unicode_decode_error / new_unicode_encode_error helpers with the _real variantshttps://Github.com/RustPython/RustPython/issues/8352#top
C-compatA discrepancy between RustPython and CPythonhttps://github.com/RustPython/RustPython/issues?q=state%3Aopen%20label%3A%22C-compat%22
https://github.com/youknowone
youknowonehttps://github.com/youknowone
on Jul 23, 2026https://github.com/RustPython/RustPython/issues/8352#issue-4955079592
Several built-in exceptions store their attributes in the instance dict instead of as fields #8345https://github.com/RustPython/RustPython/issues/8345
C-compatA discrepancy between RustPython and CPythonhttps://github.com/RustPython/RustPython/issues?q=state%3Aopen%20label%3A%22C-compat%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.