Skip to content

Add more dict functions to c-api#8043

Open
bschoenmaeckers wants to merge 1 commit into
RustPython:mainfrom
bschoenmaeckers:c-api-dict
Open

Add more dict functions to c-api#8043
bschoenmaeckers wants to merge 1 commit into
RustPython:mainfrom
bschoenmaeckers:c-api-dict

Conversation

@bschoenmaeckers
Copy link
Copy Markdown
Contributor

@bschoenmaeckers bschoenmaeckers commented Jun 4, 2026

Summary by CodeRabbit

  • New Features

    • Expanded Python C API compatibility with additional dictionary operations: membership testing, copying, item deletion, accessing keys/values/items, and flexible dictionary merging with configurable override semantics.
  • Tests

    • Comprehensive test coverage for new dictionary operations including membership checks, copy/delete actions, key/value/item retrieval, dictionary updates, and merge behavior verification.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 4, 2026

Review Change Stack

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: 14c46398-1646-41fa-b930-3318324c5b5a

📥 Commits

Reviewing files that changed from the base of the PR and between 51c97b9 and e743254.

📒 Files selected for processing (2)
  • crates/capi/src/dictobject.rs
  • crates/vm/src/builtins/dict.rs

📝 Walkthrough

Walkthrough

This PR extends RustPython's C-API with nine new dict operations and refactors the underlying PyDict merge logic to support conditional key overwrites. The C-API functions downcast PyObject pointers to PyDict and delegate to refactored merge methods that now accept an override_existing flag.

Changes

Dict C-API and merge refactoring

Layer / File(s) Summary
PyDict merge refactoring with override parameter
crates/vm/src/builtins/dict.rs
Internal helper merge_object_with_override unifies dict-and-mapping merging; public merge_object enables overwrites, new merge_object_if_missing disables them. merge_dict and merge_from_seq2 accept override_existing flag and conditionally skip key inserts when false.
Dict-or operator updates
crates/vm/src/builtins/dict.rs
__or__ and __ror__ operator implementations explicitly pass override=true to merge_dict for Python dict union semantics.
C-API FFI dict operations
crates/capi/src/dictobject.rs
Nine new extern "C" functions (PyDict_Contains, PyDict_Copy, PyDict_DelItem, PyDict_Items, PyDict_Keys, PyDict_Values, PyDict_Merge, PyDict_Update, PyDict_MergeFromSeq2) downcast inputs to PyDict and delegate to refactored methods. Tests validate membership, copy/delete, collection views, update semantics, and sequence-pair merging with override handling.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • RustPython/RustPython#7929: Adds complementary C-API dict operations (PyDict_New, PyDict_SetItem, PyDict_GetItem, PyDict_Size, PyDict_Next) to the same dictobject.rs module.

Suggested reviewers

  • youknowone
  • ShaharNaveh

Poem

🐰 Nine new pathways through the dict, so fine,
Override flags that merge with careful line,
C-API doors now open to the Python sea,
Keys skip or tumble—your choice shall be!
twitches nose

🚥 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 'Add more dict functions to c-api' directly describes the main change: adding new FFI functions for Python dict operations to the C API layer.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.

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.

1 participant