-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Allow lone-surrogate keyword keys in f(**d) #8409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
youknowone
merged 2 commits into
RustPython:main
from
HyoJongPark:fix/kwargs-surrogate-key
Jul 30, 2026
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Allow lone-surrogate keyword keys in f(**d)
`f(**{'\udc81': 2})` raised `TypeError: keywords must be strings` even
though the key is a valid `str`, because `KwArgs` stored keys as Rust
`String` (strict UTF-8) and `collect_ex_args` narrowed each key to
`PyUtf8Str` (valid UTF-8 required). CPython only checks that the key is a
`str` (`Py_TPFLAGS_UNICODE_SUBCLASS`), never encoding validity.
Change `KwArgs<T>`'s key type from `String` to `Wtf8Buf` (the WTF-8
storage `PyStr` already uses) and relax the keyword-key downcast from
`PyUtf8Str` to `PyStr` in `collect_ex_args`, `from_vectorcall`/
`from_vectorcall_owned` (which previously panicked on surrogate keys),
the `functools.partial` keyword merge, and the C-API `dict_to_kwargs`.
`Wtf8Buf` borrows only as `Wtf8`, so inherent `get`/`contains_key`/
`swap_remove`/`shift_remove(&str)` on `KwArgs` restore the `&str` lookup
interface `String: Borrow<str>` used to provide (via the zero-cost
`Wtf8::new` cast), and a generic `FromIterator<(K: Into<Wtf8Buf>, T)>`
keeps construction sites unchanged. WTF-8 awareness stays localized to
`function/argument.rs`.
Fixes #8228
Assisted-by: Claude Code:claude-opus-4-8- Loading branch information
commit daf72d7d55a1d402703f5334023096b5ae15f49a
There are no files selected for viewing
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,8 +8,9 @@ use super::{ | |
| pub(crate) mod _ast { | ||
| use crate::{ | ||
| AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, | ||
| builtins::{PyDictRef, PySet, PyStr, PyTupleRef, PyType, PyTypeRef, PyUtf8Str}, | ||
| builtins::{PyDictRef, PySet, PyStr, PyTupleRef, PyType, PyTypeRef}, | ||
| class::{PyClassImpl, StaticType}, | ||
| common::wtf8::Wtf8Buf, | ||
| function::{ArgIterable, FuncArgs, KwArgs, PyMethodDef, PyMethodFlags}, | ||
| stdlib::_ast::repr, | ||
| types::{Constructor, Initializer}, | ||
|
|
@@ -229,7 +230,7 @@ pub(crate) mod _ast { | |
| ast_replace_set_update(&expecting, attributes.as_ref(), vm)?; | ||
|
|
||
| for (key, _value) in &args.kwargs { | ||
| let key_obj: PyObjectRef = vm.ctx.new_str(key.as_str()).into(); | ||
| let key_obj: PyObjectRef = vm.ctx.new_str(key.clone()).into(); | ||
| if !ast_replace_set_discard(&expecting, &key_obj, vm)? { | ||
| return Err(vm.new_type_error(format!( | ||
| "{}.__replace__ got an unexpected keyword argument '{}'.", | ||
|
|
@@ -290,11 +291,11 @@ pub(crate) mod _ast { | |
| .into_iter() | ||
| .map(|(key, value)| { | ||
| let key = key | ||
| .downcast::<PyUtf8Str>() | ||
| .downcast::<PyStr>() | ||
| .map_err(|_| vm.new_type_error("keywords must be strings"))?; | ||
| Ok((key.as_str().to_owned(), value)) | ||
| Ok((key.as_wtf8().to_owned(), value)) | ||
| }) | ||
| .collect::<PyResult<IndexMap<String, PyObjectRef>>>()?; | ||
| .collect::<PyResult<IndexMap<Wtf8Buf, PyObjectRef>>>()?; | ||
| let result = type_obj.call(FuncArgs::new(vec![], KwArgs::new(kwargs)), vm)?; | ||
| Ok(result) | ||
| } | ||
|
|
@@ -418,7 +419,7 @@ pub(crate) mod _ast { | |
| ast_replace_set_discard(&remaining_fields, &name, vm)?; | ||
| } | ||
| for (key, value) in args.kwargs { | ||
| let key_obj: PyObjectRef = vm.ctx.new_str(key.as_str()).into(); | ||
| let key_obj: PyObjectRef = vm.ctx.new_str(key.clone()).into(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. another clone here |
||
| let contains = fields_seq.contains(&key_obj, vm)?; | ||
| if contains { | ||
| if !ast_replace_set_discard(&remaining_fields, &key_obj, vm)? { | ||
|
|
||
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there must be a way not to use clone here. could you check? I will find out one if there isn't any way to do it yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for (key, _value) in &args.kwargs { - let key_obj: PyObjectRef = vm.ctx.new_str(key.clone()).into(); + let key_obj: PyObjectRef = vm.ctx.new_str(key.as_ref()).into();Replaced the clone with
as_ref()to avoid an unnecessary copy.new_stronly needs a&Wtf8, so instead of cloning the key into an ownedWtf8Buf, I passkey.as_ref()to borrow it instead.Does this match what you had in mind?