Add more unicode functions to the c-api#8272
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughThe Unicode C API gains constructors, codec-based encoding and decoding helpers, comparisons, interning, containment, formatting, partitioning, translation, and identifier checks. Four ChangesUnicode C API
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant CCaller
participant UnicodeAPI
participant PyStr
participant codec_registry
CCaller->>UnicodeAPI: provide Unicode object or encoded bytes
UnicodeAPI->>PyStr: downcast and obtain string text
UnicodeAPI->>codec_registry: encode_text or decode_text
codec_registry-->>UnicodeAPI: encoded or decoded object
UnicodeAPI-->>CCaller: return result
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
crates/capi/src/unicodeobject.rs (1)
98-171: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winCollapse the six near-identical
PyUnicode_As*Stringhelpers onto a shared inner function.Each of the six exported helpers has an identical body differing only by the encoding-name literal. Keep the individual
#[no_mangle]symbols (required for the ABI) but delegate to one private helper to avoid drift.♻️ Suggested shared helper
fn encode_as(unicode: *mut PyObject, encoding: &str) -> *mut PyObject { with_vm(|vm| { let unicode = unsafe { &*unicode }.try_downcast_ref::<PyStr>(vm)?.to_owned(); vm.state.codec_registry.encode_text(unicode, encoding, None, vm) }) } #[unsafe(no_mangle)] pub unsafe extern "C" fn PyUnicode_AsASCIIString(unicode: *mut PyObject) -> *mut PyObject { encode_as(unicode, "ascii") } // ...and so on for latin-1, raw-unicode-escape, utf-16, utf-32, unicode-escapeAs per coding guidelines: "When branches differ only in a value but share common logic, extract the differing value first, then call the common logic once to avoid duplicate code."
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@crates/capi/src/unicodeobject.rs` around lines 98 - 171, Extract the duplicated with_vm/downcast/encode_text logic from PyUnicode_AsASCIIString, PyUnicode_AsLatin1String, PyUnicode_AsRawUnicodeEscapeString, PyUnicode_AsUTF16String, PyUnicode_AsUTF32String, and PyUnicode_AsUnicodeEscapeString into one private encode_as helper accepting the encoding name, then have each exported ABI function delegate to it with its existing literal while preserving all #[unsafe(no_mangle)] symbols.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@crates/capi/src/unicodeobject.rs`:
- Around line 50-60: Update PyUnicode_FromObject to reject objects that are not
PyStr or PyStr subclasses instead of calling obj.to_owned().str(vm). Return the
existing C-API error representation for a TypeError, while preserving the
successful conversion path for accepted string objects.
- Around line 277-293: Update PyUnicode_CompareWithASCIIString to avoid calling
PyStr::expect_str(), which can panic for surrogate-containing strings. Compare
the PyStr value using its WTF-8 view, matching the behavior of
PyUnicode_Compare, while preserving the existing ordering-to-integer mapping and
UTF-8 validation for the C string.
---
Nitpick comments:
In `@crates/capi/src/unicodeobject.rs`:
- Around line 98-171: Extract the duplicated with_vm/downcast/encode_text logic
from PyUnicode_AsASCIIString, PyUnicode_AsLatin1String,
PyUnicode_AsRawUnicodeEscapeString, PyUnicode_AsUTF16String,
PyUnicode_AsUTF32String, and PyUnicode_AsUnicodeEscapeString into one private
encode_as helper accepting the encoding name, then have each exported ABI
function delegate to it with its existing literal while preserving all
#[unsafe(no_mangle)] symbols.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
Run ID: a08d9e91-1068-4efd-8524-7620fca90951
📒 Files selected for processing (1)
crates/capi/src/unicodeobject.rs
|
@coderabbitai review |
✅ Action performedReview finished.
|
12667c2 to
1aa1771
Compare
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@crates/capi/src/unicodeobject.rs`:
- Around line 578-591: Update PyUnicode_Translate to honor the _errors parameter
by converting and passing it through to PyStr::translate, preserving the
requested codec error-handler behavior; if PyStr::translate cannot support it,
explicitly document the intentional API divergence instead.
- Around line 258-331: Update PyUnicode_DecodeUTF16 and PyUnicode_DecodeUTF32 to
honor the _byteorder pointer: when it is non-NULL, select the corresponding
forced little-endian or big-endian codec based on its input value, while
preserving BOM/native-order decoding when it is NULL. After decoding, write the
detected byte order back through the pointer as required by the API.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
Run ID: c01ec87c-2d24-4be3-b3ab-de829f116906
📒 Files selected for processing (2)
crates/capi/src/unicodeobject.rscrates/vm/src/builtins/str.rs
1aa1771 to
3a63323
Compare
|
@coderabbitai review |
✅ Action performedReview finished.
|
Review Add unicode decode functions Add more functions
acbcaaf to
37fd030
Compare
Summary by CodeRabbit
New Features
Bug Fixes