Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Allow surrogates in str #5587

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
merged 10 commits into from
Mar 26, 2025
Merged

Allow surrogates in str #5587

merged 10 commits into from
Mar 26, 2025

Conversation

coolreader18
Copy link
Member

This switches the backing format of PyStr from Box<str> to Box<Wtf8>. From the motivation I wrote in a doc comment:

An implementation of WTF-8, a utf8-compatible encoding that allows for unpaired surrogate codepoints. This implementation additionally allows for paired surrogates that are nonetheless treated as two separate codepoints.

RustPython uses this because CPython internally uses a variant of UCS-1/2/4 as its string storage, which treats each u8/u16/u32 value (depending on the highest codepoint value in the string) as simply integers, unlike UTF-8 or UTF-16 where some characters are encoded using multi-byte sequences. CPython additionally doesn't disallow the use of surrogates in strs (which in UTF-16 pair together to represent codepoints with a value higher than u16::MAX) and in fact takes quite extensive advantage of the fact that they're allowed. The surrogateescape codec-error handler uses them to represent byte sequences which are invalid in the given codec (e.g. bytes with their high bit set in ASCII or UTF-8) by mapping them into the surrogate range. surrogateescape is the default error handler in Python for interacting with the filesystem, and thus if RustPython is to properly support surrogateescape, its strs must be able to represent surrogates.

We use WTF-8 over something more similar to CPython's string implementation because of its compatibility with UTF-8, meaning that in the case where a string has no surrogates, it can be viewed as a UTF-8 Rust str without needing any copies or re-encoding.

This implementation is mostly copied from the WTF-8 implentation in the Rust standard library, which is used as the backing for OsStr on Windows targets. As previously mentioned, however, it is modified to not join two surrogates into one codepoint when concatenating strings, in order to match CPython's behavior.

Method

I opted not to change the signature of PyStr::as_str(), since that would mean a truly massive diff. Instead, I have it panic, and my hope is that we can get to a place with this where tests pass, and then we can deal with the rest of the as_str calls incrementally.

Status

Currently panicking in cformat code. Is there still a reason Parser et al need to be a separate repo if ruff isn't using it anymore? It'd be really convenient to merge it into the monorepo.

@youknowone
Copy link
Member

can we separately merge less wtf8 related changes like format/cformat changes?

@coolreader18
Copy link
Member Author

#5621

@coolreader18 coolreader18 force-pushed the wtf8 branch 3 times, most recently from c3388b2 to a556bd7 Compare March 25, 2025 21:19
Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

common/src/encodings.rs Outdated Show resolved Hide resolved
common/src/wtf8/mod.rs Show resolved Hide resolved
vm/src/anystr.rs Outdated Show resolved Hide resolved
vm/sre_engine/Cargo.toml Outdated Show resolved Hide resolved
@coolreader18 coolreader18 merged commit 7ac90f5 into RustPython:main Mar 26, 2025
11 checks passed
@coolreader18 coolreader18 deleted the wtf8 branch March 26, 2025 02:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.