GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add mapping c-api functions by bschoenmaeckers · Pull Request #8003 · RustPython/RustPython

Conversation

@bschoenmaeckers

@bschoenmaeckers bschoenmaeckers commented Jun 1, 2026 •
edited by coderabbitai Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Used AI to implement the mapping protocol c-api.

Summary by CodeRabbit

  • New Features
    • Added mapping protocol support to the C API so native code can query mapping size and retrieve keys, values, and items as Python objects, improving interoperability with native extensions and tooling.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@coderabbitai

coderabbitai Bot commented Jun 1, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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: 43d75432-ddb4-4e97-a455-a9697e170767

📥 Commits

Reviewing files that changed from the base of the PR and between 5bc9850 and a7c74a9.

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

📝 Walkthrough

Walkthrough

Adds a new mapping submodule and four C-API FFI functions (PyMapping_Size, PyMapping_Keys, PyMapping_Values, PyMapping_Items) that call into the VM to obtain mapping size or return new Python lists of keys/values/items; includes a disabled pyo3 test.

Changes

Mapping Protocol FFI Surface

Layer / File(s) Summary
Mapping protocol FFI functions and module wiring
crates/capi/src/abstract_.rs, crates/capi/src/abstract_/mapping.rs
New mapping module declared and re-exported from abstract_.rs. Four #[no_mangle] unsafe extern "C" functions (PyMapping_Size, PyMapping_Keys, PyMapping_Values, PyMapping_Items) each run inside with_vm, dereference the input PyObject, call try_mapping(...) to obtain the operation result, and return either an isize length or a new *mut PyObject list.
Mapping protocol validation tests
crates/capi/src/abstract_/mapping.rs
Disabled (#[cfg(false)]) test module constructs a PyDict, validates that len(), keys(), values(), and items() return correct sizes, and confirms each item is a tuple.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • RustPython/RustPython#7882: Extends crates/capi C-API's abstract_ module with additional protocol entrypoints.

Suggested reviewers

  • youknowone
  • ShaharNaveh

Poem

🐇 I hopped through VM and mapping light,
Four tiny bridges to C took flight,
Keys, values, items in tidy rows,
Size counted true as the rabbit knows,
A little rustle of FFI delight.

🚥 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 mapping c-api functions' directly and clearly describes the main change: adding C-API functions for the mapping protocol.
Docstring Coverage ✅ Passed Docstring coverage is 83.33% 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
  • X
  • Mastodon
  • Reddit
  • LinkedIn

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

coderabbitai[bot]
coderabbitai Bot reviewed Jun 1, 2026
View reviewed changes

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
crates/capi/src/abstract_/mapping.rs (1)

10-38: 💤 Low value

Consider extracting shared logic into a helper to reduce duplication.

All three functions follow the same pattern: get mapping, call method, iterate to list. Per coding guidelines, when branches differ only in a method call but share common logic, extracting the common parts improves maintainability.

♻️ Possible helper extraction
+fn mapping_to_list(
+    obj: *mut PyObject,
+    get_items: impl FnOnce(&rustpython_vm::protocol::PyMapping, &rustpython_vm::VirtualMachine) -> rustpython_vm::PyResult<rustpython_vm::PyObjectRef>,
+) -> *mut PyObject {
+    with_vm(|vm| {
+        let obj = unsafe { &*obj };
+        let items = get_items(&obj.try_mapping(vm)?, vm)?;
+        let iter = items.get_iter(vm)?;
+        Ok(vm.ctx.new_list(iter.try_to_value(vm)?))
+    })
+}
+
 #[unsafe(no_mangle)]
 pub unsafe extern "C" fn PyMapping_Keys(obj: *mut PyObject) -> *mut PyObject {
-    with_vm(|vm| {
-        let obj = unsafe { &*obj };
-        let keys = obj.try_mapping(vm)?.keys(vm)?;
-        let iter = keys.get_iter(vm)?;
-        Ok(vm.ctx.new_list(iter.try_to_value(vm)?))
-    })
+    mapping_to_list(obj, |m, vm| m.keys(vm))
 }

Apply similarly to PyMapping_Values and PyMapping_Items.

🤖 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/abstract_/mapping.rs` around lines 10 - 38, Extract the
repeated logic in PyMapping_Keys, PyMapping_Values, and PyMapping_Items into a
small helper (e.g., fn map_to_list(obj: *mut PyObject, f: impl FnOnce(&dyn
Mapping, &VirtualMachine) -> PyResult<PyObjectRef>) -> *mut PyObject or a helper
taking an enum/closure) that: calls with_vm, dereferences obj, calls
obj.try_mapping(vm)?, invokes the specific mapping method (keys/values/items)
via the passed closure or selector, gets the iterator via get_iter(vm)?,
converts it with try_to_value(vm) and returns vm.ctx.new_list(...); then replace
the bodies of PyMapping_Keys, PyMapping_Values and PyMapping_Items to call this
helper with the appropriate mapping method selector so the only differing part
is the method passed in.
🤖 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/abstract_/mapping.rs`:
- Around line 40-66: Change the test module attribute from the disabled
#[cfg(false)] to #[cfg(test)] (or #[cfg(test)] plus #[ignore] if you want it
compiled but skipped), and update the test function size_keys_values_items to
call the RustPython C-API wrappers instead of only exercising PyO3: keep
Python::attach and creation of the PyDict/PyMapping, then invoke the C-API
functions (e.g., PyMapping_Size, PyMapping_Keys, PyMapping_Values,
PyMapping_Items or their crate wrapper functions) against the mapping and assert
their return values/lengths and that items are tuples, so the test actually
validates the C-API implementation rather than only PyO3 helpers.

---

Nitpick comments:
In `@crates/capi/src/abstract_/mapping.rs`:
- Around line 10-38: Extract the repeated logic in PyMapping_Keys,
PyMapping_Values, and PyMapping_Items into a small helper (e.g., fn
map_to_list(obj: *mut PyObject, f: impl FnOnce(&dyn Mapping, &VirtualMachine) ->
PyResult<PyObjectRef>) -> *mut PyObject or a helper taking an enum/closure)
that: calls with_vm, dereferences obj, calls obj.try_mapping(vm)?, invokes the
specific mapping method (keys/values/items) via the passed closure or selector,
gets the iterator via get_iter(vm)?, converts it with try_to_value(vm) and
returns vm.ctx.new_list(...); then replace the bodies of PyMapping_Keys,
PyMapping_Values and PyMapping_Items to call this helper with the appropriate
mapping method selector so the only differing part is the method passed in.
🪄 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: 1d45ea1b-0816-4cf2-9aa0-8956af0e0836

📥 Commits

Reviewing files that changed from the base of the PR and between 1ce37bf and 5cb4197.

📒 Files selected for processing (2)
  • crates/capi/src/abstract_.rs
  • crates/capi/src/abstract_/mapping.rs

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread crates/capi/src/abstract_/mapping.rs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bschoenmaeckers
bschoenmaeckers force-pushed the c-api-mapping branch from 5cb4197 to 5bc9850 Compare June 1, 2026 15:06
@youknowone
youknowone enabled auto-merge (squash) June 1, 2026 15:37
@bschoenmaeckers
Add mapping c-api functions
a7c74a9
auto-merge was automatically disabled June 1, 2026 16:43

Head branch was pushed to by a user without write access

@bschoenmaeckers
bschoenmaeckers force-pushed the c-api-mapping branch from 5bc9850 to a7c74a9 Compare June 1, 2026 16:43
youknowone
youknowone approved these changes Jun 2, 2026
View reviewed changes

@youknowone youknowone left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

👍

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@youknowone
youknowone merged commit 74b4707 into RustPython:main Jun 2, 2026
26 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bschoenmaeckers
bschoenmaeckers deleted the c-api-mapping branch June 2, 2026 07:24
@coderabbitai coderabbitai Bot mentioned this pull request Jun 25, 2026
Add more mapping functions to c-api #8169
Merged
youknowone pushed a commit that referenced this pull request Sep 16, 2026
@bschoenmaeckers
Add mapping c-api functions (#8003)
8af0f8a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@coderabbitai coderabbitai[bot] coderabbitai[bot] left review comments
@youknowone youknowone youknowone approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@bschoenmaeckers @youknowone
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.