Skip to content

f(**d) rejects str keys containing lone surrogates: TypeError: keywords must be strings #8228

Description

@youknowone

Summary

Unpacking a dict whose keys contain lone surrogates via ** raises TypeError: keywords must be strings, but the keys are str instances. CPython accepts any str key through the ** call path.

def collect(**kw):
    return kw

collect(**{'\udc81': 2})     # TypeError: keywords must be strings
dict(**{'\udc81': 1})        # TypeError: keywords must be strings

CPython 3.14.2:

>>> collect(**{'\udc81': 2})
{'\udc81': 2}

'\udc81' is a real str (e.g. produced by os.fsdecode of undecodable bytes, surrogateescape-decoded data, etc.), so this rejects valid programs. Verified on main @ 7044fdc.

Cause

ExecutingFrame::collect_ex_args (crates/vm/src/frame.rs:6539) validates kwargs keys with

key.downcast_ref::<PyUtf8Str>()
    .ok_or_else(|| vm.new_type_error("keywords must be strings"))?;

A str containing a lone surrogate is a PyStr whose payload is Wtf8, not PyUtf8Str, so the downcast fails even though the key is a str. The same pattern exists at frame.rs:6521, crates/vm/src/stdlib/_functools.rs (partial), and crates/capi/src/abstract_.rs.

The deeper constraint is that KwArgs/FuncArgs keyword names are Rust String (UTF-8), so a surrogate-carrying key currently has no representation in the calling convention; fixing this properly means keyword names need to be Wtf8Buf (or PyStrRef) end-to-end. The error message check should be downcast_ref::<PyStr>() (matching CPython, which only rejects non-str keys) once the storage can hold it.


Found while running synthetic benchmark/compat suites; researched and reported by Claude on behalf of @youknowone.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions