◐ Shell
reader mode source ↗
Skip to content

Add set object functions to c-api#8002

Merged
youknowone merged 1 commit into
RustPython:mainfrom
bschoenmaeckers:c-api-sets
Jun 1, 2026
Merged

Add set object functions to c-api#8002
youknowone merged 1 commit into
RustPython:mainfrom
bschoenmaeckers:c-api-sets

Conversation

@bschoenmaeckers

@bschoenmaeckers bschoenmaeckers commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

I've used the skill from #8001 to implement the missing set c-api functions using an AI agent.

Summary by CodeRabbit

  • New Features

    • Extended set and frozenset support with operations for creation, element management (add, discard, clear), containment checks, and size queries.
  • Chores

    • Added itertools as a workspace dependency.

@coderabbitai

coderabbitai Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds CPython-style C-API bindings for Python set and frozenset types to RustPython. It exposes a new setobject module with eight unsafe C-ABI functions (constructors, mutators, queries, and size retrieval), backed by visibility changes to five internal VM methods.

Changes

Set and FrozenSet C-API Support

Layer / File(s) Summary
C-API Module Setup
crates/capi/Cargo.toml, crates/capi/src/lib.rs
Added itertools workspace dependency and exposed new setobject module in the crate public surface.
VM Method Visibility Exposure
crates/vm/src/builtins/set.rs
Changed Rust visibility from private fn to pub fn for PySet::__contains__, discard, clear, pop and PyFrozenSet::__contains__ to enable C-API layer access.
Set/FrozenSet C-API Implementation
crates/capi/src/setobject.rs
Implemented eight C-ABI functions (PySet_New, PyFrozenSet_New, PySet_Add, PySet_Clear, PySet_Contains, PySet_Discard, PySet_Pop, PySet_Size) with type-check macros and disabled pyo3-based sanity tests.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • RustPython/RustPython#7871: Introduced the define_py_check! macro in capi/src/object.rs that is used to generate PySet_Check and PyFrozenSet_Check type-check helpers in this PR.

Suggested reviewers

  • youknowone
  • ShaharNaveh

Poem

🐰 Sets wrapped in C, oh what a sight,
With frozen friends and checks so tight,
From RustPython's heart they now take flight,
Through C-API gates, they shine so bright!
A rabbit's work, forever tight! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 78.95% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add set object functions to c-api' directly and clearly describes the main change: adding C-API functions for set objects, which is evidenced by the new setobject.rs file with multiple PySet/PyFrozenSet C-ABI functions.
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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hide 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/setobject.rs`:
- Around line 45-50: PySet_Add currently downcasts the `set` argument only to
`PySet`, rejecting valid `PyFrozenSet` inputs; update `PySet_Add` (in
`crates/capi/src/setobject.rs`) to accept either a `PySet` or a `PyFrozenSet`
(and subtypes) by detecting the concrete type after `with_vm` unwrap and calling
the appropriate mutator: call `PySet::add` for `PySet` and invoke the VM-side
frozenset-initialization/mutation path for `PyFrozenSet` (the same internal
routine used to populate brand-new frozensets) so that `set.add(key, vm)`
semantics are preserved; ensure you clear/reset any cached hash on the
`PyFrozenSet` after mutation so subsequent hash lookups are correct and preserve
existing `with_vm` error propagation.
- Around line 82-90: Replace the current two-step membership + removal in
PySet_Discard with a single discard call that returns whether an item was
removed: add a bool-returning helper method on PySet (e.g.,
PySet::discard_returning_bool) that wraps the existing
PySetInner::discard(...)->PyResult<bool>, then update PySet_Discard to call that
helper once and return its boolean result as c_int; keep using the existing
try_downcast_ref::<PySet> and the same key handling but remove the prior
__contains__ probe so lookup+removal happen in one operation.
🪄 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: 20296950-c1ea-46f7-a7d7-1ad6bdea14cd

📥 Commits

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

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (4)
  • crates/capi/Cargo.toml
  • crates/capi/src/lib.rs
  • crates/capi/src/setobject.rs
  • crates/vm/src/builtins/set.rs

Hide details View details @youknowone youknowone merged commit 885cf5c into RustPython:main Jun 1, 2026
26 checks passed
@bschoenmaeckers bschoenmaeckers deleted the c-api-sets branch June 1, 2026 16:53
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.

3 participants