-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Initiailzer for PyCStructure #6586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
📝 WalkthroughWalkthroughRefactored ctypes structure and union initialization to use Initializer trait implementations instead of slot_new-based initialization. PyCStructType and PyCStructure now implement Initializer to process fields and handle positional/keyword arguments. Added length field tracking to StgInfo for both structures and unions. Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~40 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✨ Finishing touches
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. Comment |
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: 0
🧹 Nitpick comments (2)
crates/vm/src/stdlib/ctypes/structure.rs (2)
573-596: Consider logging or erroring on malformed field tuples.When iterating
fields, if a field doesn't downcast to a tuple or lacks aPyStrname (lines 577-580), it's silently skipped without consuming a positional argument. While this may be intentional for flexibility, it could mask issues where_fields_contains unexpected entries.If the expectation is that all fields are well-formed (since
process_fieldsvalidates them earlier), this should be safe. However, a debug assertion or logging might help catch inconsistencies during development.
540-628: Consider extracting shared initialization logic to reduce duplication.The
init_pos_argsmethod andInitializer::initimplementation are nearly identical to their counterparts inunion.rs(lines 458-547). This ~90 lines of duplicated logic could be refactored into a shared helper inbase.rsor via a trait with default implementations.For example, a generic helper could be added to
PyCDataor a new trait:// In base.rs pub fn init_cdata_from_args<T: AsRef<PyCData>>( zelf: &Py<T>, cls: &Py<PyType>, args: &FuncArgs, vm: &VirtualMachine, ) -> PyResult<()> { /* shared logic */ }This is optional and can be deferred, as the current implementation is correct.
📜 Review details
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
crates/vm/src/stdlib/ctypes/structure.rscrates/vm/src/stdlib/ctypes/union.rs
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.rs: Follow the default rustfmt code style by runningcargo fmtto format Rust code
Always run clippy to lint code (cargo clippy) before completing tasks and fix any warnings or lints introduced by changes
Follow Rust best practices for error handling and memory management
Use the macro system (pyclass,pymodule,pyfunction, etc.) when implementing Python functionality in Rust
Files:
crates/vm/src/stdlib/ctypes/union.rscrates/vm/src/stdlib/ctypes/structure.rs
🧬 Code graph analysis (2)
crates/vm/src/stdlib/ctypes/union.rs (2)
crates/vm/src/stdlib/ctypes.rs (2)
stg_info(39-42)cls(697-697)crates/vm/src/stdlib/ctypes/base.rs (4)
fields(2284-2284)fields(2286-2286)new(149-165)new(1249-1257)
crates/vm/src/stdlib/ctypes/structure.rs (2)
crates/vm/src/stdlib/ctypes.rs (4)
stg_info(39-42)base(61-61)fields_attr(885-885)field(889-889)crates/vm/src/stdlib/ctypes/base.rs (20)
fields(2284-2284)fields(2286-2286)new(149-165)new(1249-1257)from_stg_info(441-450)value(862-863)value(871-871)value(871-871)value(893-893)value(907-907)value(922-922)value(932-932)value(1364-1364)value(1372-1372)value(1380-1380)value(1429-1429)value(1444-1444)value(1458-1458)value(1472-1472)value(1497-1497)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Run snippets and cpython tests (windows-latest)
🔇 Additional comments (6)
crates/vm/src/stdlib/ctypes/union.rs (2)
274-291: LGTM: Length tracking added to union StgInfo.The
stg_info.length = fields.len()correctly records the number of fields in the union type metadata. This aligns with theStgInfo::newconstructor that initializeslength: 0by default, and ensures the field count is properly propagated toPyCData::from_stg_infowhich stores it inAtomicCell<usize>.
433-451: LGTM: Length propagation in union slot_new.The
slot_newcorrectly extractslengthalongsidetotal_sizeandtotal_alignfrom the type'sStgInfo, then propagates it to the new instance vianew_stg_info. This ensures union instances have consistent field count metadata matching their type definition.crates/vm/src/stdlib/ctypes/structure.rs (4)
369-388: LGTM: Length tracking added to structure StgInfo.The
stg_info.length = fields.len()correctly records the field count, consistent with the union implementation. This enables proper length propagation through the structure type system.
515-538: LGTM: Structure slot_new correctly propagates length.The implementation mirrors the union's
slot_new, ensuring consistent length metadata propagation from type to instance. TheStgInfois properly constructed with size, alignment, and length before being passed toPyCData::from_stg_info.
603-628: LGTM: Initializer implementation follows CPython ctypes semantics.The implementation correctly:
- Processes positional arguments recursively through the inheritance chain
- Detects duplicate field specifications (positional + keyword for same field)
- Reports "too many initializers" when extra positional args are provided
- Applies keyword arguments via
set_attr, letting field descriptors handle validation
633-636: LGTM: pyclass macro updated to include Initializer.The
with(Constructor, Initializer, AsBuffer)correctly registers the newInitializertrait implementation forPyCStructure.
Summary by CodeRabbit
Bug Fixes
Enhancements
✏️ Tip: You can customize this high-level summary in your review settings.