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 — Allow c functions in tp_new type slot by bschoenmaeckers · Pull Request #8346 · RustPython/RustPython

Conversation

@bschoenmaeckers

@bschoenmaeckers bschoenmaeckers commented Jul 21, 2026 •
edited by coderabbitai Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

This is my first draft of allowing C functions in type slots. I started with the NewFunc slot and would love some feedback on my approach before converting the other slots.

@youknowone Could you please share your opinion.

Summary by CodeRabbit

  • Bug Fixes

    • Improved __new__ constructor dispatch across built-in types (bool, int, float, str, tuple, set) by invoking the resolved constructor target more reliably.
    • Refined the “same/equivalent constructor” detection used for __init fast-path specialization, improving when the optimized path is selected.
    • Improved handling of Rust-backed __new__ wrappers and inherited constructor behavior for classes/types.
  • Compatibility

    • Standardized constructor wiring to support consistent Rust-backed behavior and lay groundwork for additional native __new__ variants.

Sorry, something went wrong.

Uh oh!

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

youknowone reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@coderabbitai

coderabbitai Bot commented Jul 21, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

NewFunc changes from a function-pointer alias to an enum with identity and invocation methods. Generated constructors store typed Rust variants, while built-in and type dispatch paths use .invoke() and identity-based comparisons.

Changes

New slot dispatch

Layer / File(s) Summary
NewFunc contract and producers
crates/vm/src/types/slot.rs, crates/derive-impl/src/pyclass.rs
NewFunc supports Rust and C variants with identity and invocation methods; generated constructors store typed Rust variants.
Slot storage and identity checks
crates/vm/src/types/slot.rs, crates/vm/src/class.rs, crates/vm/src/frame.rs, crates/vm/src/builtins/type.rs
Slot wrapper, inheritance, specialization, and safety checks compare __new__ implementations through identity().
Constructor invocation paths
crates/vm/src/builtins/{bool,float,int,set,str,tuple,type}.rs
Built-in vectorcall and type dispatch paths invoke loaded __new__ slots through .invoke(...).

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant BuiltinVectorcall
  participant NewFunc
  participant TypeDispatch
  BuiltinVectorcall->>NewFunc: Load slots.new and invoke(cls, args, vm)
  TypeDispatch->>NewFunc: Invoke resolved __new__ slot
  NewFunc-->>BuiltinVectorcall: Return PyResult
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 15.38% 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 clearly matches the main change: adding C-function support to the tp_new slot.
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.
✨ 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.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bschoenmaeckers
bschoenmaeckers force-pushed the c-api-type-slots branch from 00b1ca8 to 69d97b0 Compare July 21, 2026 15:56
coderabbitai[bot]
coderabbitai Bot reviewed Jul 21, 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/vm/src/types/slot.rs (1)

316-329: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Derive PartialEq, Eq to simplify identity checks.

Function pointers natively implement PartialEq and Eq. Deriving these traits on NewFunc allows direct equality comparisons (== and !=) on the variants, entirely eliminating the need for the manual identity() method and the verbose mapping at all call sites.

  • crates/vm/src/types/slot.rs#L316-L329: Add #[derive(PartialEq, Eq)] to NewFunc and remove the identity method entirely.
  • crates/vm/src/types/slot.rs#L909-L911: Replace the comparison with cls.slots.new.load() == Some(NewFunc::Rust(new_wrapper as _)).
  • crates/vm/src/builtins/type.rs#L2835-L2835: Replace the condition with slot_new != crate::types::NewFunc::Rust(crate::types::new_wrapper as _).
  • crates/vm/src/builtins/type.rs#L3072-L3072: Replace the check with if typ_new != staticbase_new {.
  • crates/vm/src/class.rs#L207-L208: Replace the inheritance check with !is_object_itself && object_new == Some(slot_new).
  • crates/vm/src/frame.rs#L9243-L9243: Replace the identity comparison with cls_new_fn == obj_new_fn.
🤖 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/vm/src/types/slot.rs` around lines 316 - 329, Derive PartialEq and Eq
for NewFunc and remove its identity method, then replace all identity-based
comparisons with direct NewFunc equality checks: update
crates/vm/src/types/slot.rs lines 316-329 and 909-911,
crates/vm/src/builtins/type.rs lines 2835 and 3072, crates/vm/src/class.rs lines
207-208, and crates/vm/src/frame.rs line 9243 as specified, preserving the
existing comparison semantics.
🤖 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/vm/src/types/slot.rs`:
- Around line 316-320: Preserve lock-free access for PyTypeSlots::new by
avoiding the two-variant NewFunc representation in AtomicCell<Option<NewFunc>>.
Update NewFunc and the associated new-slot storage/access paths to use a
pointer-sized representation compatible with the target atomic width, while
retaining support for both Rust and C constructors.

---

Nitpick comments:
In `@crates/vm/src/types/slot.rs`:
- Around line 316-329: Derive PartialEq and Eq for NewFunc and remove its
identity method, then replace all identity-based comparisons with direct NewFunc
equality checks: update crates/vm/src/types/slot.rs lines 316-329 and 909-911,
crates/vm/src/builtins/type.rs lines 2835 and 3072, crates/vm/src/class.rs lines
207-208, and crates/vm/src/frame.rs line 9243 as specified, preserving the
existing comparison semantics.
🪄 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: b97f0fc2-eccf-40ed-878e-0572a321a7a8

📥 Commits

Reviewing files that changed from the base of the PR and between cc30cd5 and 00b1ca8.

📒 Files selected for processing (11)
  • crates/derive-impl/src/pyclass.rs
  • crates/vm/src/builtins/bool.rs
  • crates/vm/src/builtins/float.rs
  • crates/vm/src/builtins/int.rs
  • crates/vm/src/builtins/set.rs
  • crates/vm/src/builtins/str.rs
  • crates/vm/src/builtins/tuple.rs
  • crates/vm/src/builtins/type.rs
  • crates/vm/src/class.rs
  • crates/vm/src/frame.rs
  • crates/vm/src/types/slot.rs

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread crates/vm/src/types/slot.rs Resolved

Uh oh!

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

@bschoenmaeckers
Allow c functions in tp_new type slot
8c53827
@bschoenmaeckers
bschoenmaeckers force-pushed the c-api-type-slots branch from 69d97b0 to f884722 Compare July 22, 2026 11:49
youknowone
youknowone reviewed Jul 23, 2026
View reviewed changes
Comment thread crates/vm/src/types/slot.rs
Comment on lines +318 to +319
Rust(fn(PyTypeRef, FuncArgs, &VirtualMachine) -> PyResult),
C(unsafe extern "C" fn(*mut PyObject, *mut PyObject, *mut PyObject) -> *mut PyObject),

@youknowone youknowone Jul 23, 2026

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.

I'd like not to introduce an enum for this.
let me try do this by adding a bridge in capi side.

Sorry, something went wrong.

Uh oh!

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

All reactions

@bschoenmaeckers bschoenmaeckers Jul 23, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor Author

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.

We might need a Box<dyn Fn> for this. As in my testing I could not store ar c fn in a fn slot.

Sorry, something went wrong.

Uh oh!

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

All reactions

@bschoenmaeckers bschoenmaeckers Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

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.

If you need a testbed, please try to fix this TODO here: https://github.com/bschoenmaeckers/RustPython/blob/880999db85bd14f497e48e07e74b3745dd484994/crates/capi/src/object/pytype.rs#L194-L198

Sorry, something went wrong.

Uh oh!

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

youknowone reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@youknowone youknowone Jul 29, 2026

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.

this is more complicated than my first expectation. i am trying another way

Sorry, something went wrong.

Uh oh!

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

bschoenmaeckers reacted with laugh emoji
All reactions
  • 😄 1 reaction

@youknowone youknowone Aug 9, 2026

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.

could you check if this way work? #8435

Sorry, something went wrong.

Uh oh!

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

All reactions
@youknowone
youknowone force-pushed the main branch from d1c4925 to 3191eca Compare September 16, 2026 17:13
@youknowone
youknowone force-pushed the c-api-type-slots branch from f884722 to 8c53827 Compare September 16, 2026 17:17
@bschoenmaeckers bschoenmaeckers closed this Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@youknowone youknowone youknowone left review comments
@coderabbitai coderabbitai[bot] coderabbitai[bot] left review comments

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 01402520 — 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.