◐ Shell
clean mode source ↗

Add unicode & bytes c-api support by bschoenmaeckers · Pull Request #7904 · RustPython/RustPython

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 9b69a26b-4ae8-47b4-b059-6b9ad16a5fad

📥 Commits

Reviewing files that changed from the base of the PR and between 4447635 and 78ba5e0.

📒 Files selected for processing (2)
  • crates/capi/src/bytesobject.rs
  • crates/capi/src/unicodeobject.rs
🚧 Files skipped from review as they are similar to previous changes (2)
  • crates/capi/src/bytesobject.rs
  • crates/capi/src/unicodeobject.rs

📝 Walkthrough

Walkthrough

Adds C-API bindings for Python bytes and unicode objects: type-check helpers, bytes constructors/accessors, unicode constructors/accessors/encoding/interning/comparison, public module exports, and crate-visible macro re-export.

Changes

C-API Bytes and Unicode Object Bindings

Layer / File(s) Summary
C-API Type Check Macro Re-export and Module Declaration
crates/capi/src/object.rs, crates/capi/src/lib.rs
define_py_check is re-exported with crate visibility and bytesobject and unicodeobject are declared public.
Bytes Object C-API Functions
crates/capi/src/bytesobject.rs
Adds PyBytes_Check/PyBytes_CheckExact, PyBytes_FromStringAndSize (handles null pointer/uninitialized buffer and negative lengths), PyBytes_Size, PyBytes_AsString, and disabled pyo3 tests.
Unicode Object C-API Functions
crates/capi/src/unicodeobject.rs
Adds PyUnicode_Check/PyUnicode_CheckExact, PyUnicode_FromStringAndSize, PyUnicode_AsUTF8AndSize, PyUnicode_AsEncodedString, PyUnicode_InternInPlace, PyUnicode_EqualToUTF8AndSize, and disabled pyo3 tests.

Sequence Diagram(s)

sequenceDiagram
  participant CCaller as C caller
  participant PyBytes_FromStringAndSize
  participant VM as RustPython VM
  participant Ctx as VM Context
  CCaller->>PyBytes_FromStringAndSize: (bytes: *mut c_char, len: isize)
  PyBytes_FromStringAndSize->>VM: with_vm_context
  alt bytes is NULL
    VM->>Ctx: allocate uninitialized Vec<u8>
  else bytes not NULL
    VM->>Ctx: copy from pointer slice into Vec<u8>
  end
  Ctx-->>VM: new PyBytes PyObject*
  VM-->>PyBytes_FromStringAndSize: PyObject*
  PyBytes_FromStringAndSize-->>CCaller: PyObject*
Loading
sequenceDiagram
  participant CCaller as C caller
  participant PyUnicode_InternInPlace
  participant VM as RustPython VM
  participant Ctx as VM Context
  CCaller->>PyUnicode_InternInPlace: string: *mut *mut PyObject
  PyUnicode_InternInPlace->>VM: downcast *string to PyStr
  VM->>Ctx: intern string
  Ctx-->>VM: interned PyObject*
  VM-->>PyUnicode_InternInPlace: interned PyObject*
  PyUnicode_InternInPlace-->>CCaller: write interned pointer back to *string
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~40 minutes

Possibly related PRs

  • RustPython/RustPython#7871: Introduces define_py_check macro-generated C-API type-check functions; related to this PR's usage and re-export of that macro.

Suggested reviewers

  • youknowone
  • ShaharNaveh

Poem

🐰 I hopped through bytes and strings today,
From raw C pointers to UTF-8 play.
I copied, interned, and checked with care,
Rust bridges C so Python can share.
Hooray for bindings — nibble, hop, hooray! 🥕

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately captures the main objective of the changeset: adding C-API support for unicode and bytes types through new FFI functions in bytesobject.rs and unicodeobject.rs.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.