tp_itemsize by youknowone · Pull Request #6544 · RustPython/RustPython
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
Lib/test/test_builtin.pyis excluded by!Lib/**
📒 Files selected for processing (7)
crates/derive-impl/src/pyclass.rscrates/vm/src/builtins/bytes.rscrates/vm/src/builtins/int.rscrates/vm/src/builtins/tuple.rscrates/vm/src/builtins/type.rscrates/vm/src/class.rscrates/vm/src/types/slot.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/builtins/tuple.rscrates/vm/src/builtins/int.rscrates/vm/src/types/slot.rscrates/vm/src/builtins/bytes.rscrates/vm/src/class.rscrates/derive-impl/src/pyclass.rscrates/vm/src/builtins/type.rs
🧠 Learnings (4)
📓 Common learnings
Learnt from: CR
Repo: RustPython/RustPython PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-12-27T14:03:49.011Z
Learning: Applies to **/*.rs : Use the macro system (`pyclass`, `pymodule`, `pyfunction`, etc.) when implementing Python functionality in Rust
📚 Learning: 2025-08-26T05:20:54.540Z
Learnt from: youknowone
Repo: RustPython/RustPython PR: 6110
File: vm/src/frame.rs:1311-1316
Timestamp: 2025-08-26T05:20:54.540Z
Learning: In RustPython's pattern matching implementation, only certain builtin types should have the SEQUENCE flag: list and tuple are confirmed sequences. The user youknowone indicated that bytes, bytearray are not considered sequences in this context, even though they implement sequence-like protocols.
Applied to files:
crates/vm/src/builtins/int.rs
📚 Learning: 2025-08-26T05:20:54.540Z
Learnt from: youknowone
Repo: RustPython/RustPython PR: 6110
File: vm/src/frame.rs:1311-1316
Timestamp: 2025-08-26T05:20:54.540Z
Learning: In the RustPython codebase, only certain builtin types should be marked with the SEQUENCE flag for pattern matching. List and tuple are sequences, but bytes, bytearray, and range are not considered sequences in this context, even though they may implement sequence-like protocols.
Applied to files:
crates/vm/src/builtins/int.rs
📚 Learning: 2025-12-27T14:03:49.011Z
Learnt from: CR
Repo: RustPython/RustPython PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-12-27T14:03:49.011Z
Learning: Applies to **/*.rs : Use the macro system (`pyclass`, `pymodule`, `pyfunction`, etc.) when implementing Python functionality in Rust
Applied to files:
crates/derive-impl/src/pyclass.rs
🧬 Code graph analysis (1)
crates/vm/src/builtins/bytes.rs (2)
crates/vm/src/builtins/memory.rs (1)
itemsize(584-586)crates/stdlib/src/array.rs (1)
itemsize(738-740)
🔇 Additional comments (4)
crates/vm/src/builtins/int.rs (1)
322-326:itemsize = 4forintlooks correct but ties you to BigInt’s current layoutUsing
itemsize = 4makesinta variable-sized type for layout checks and__itemsize__introspection, matching a u32‑digit representation. If malachite’s internal digit size ever changes, this constant will need to be revisited (and tests aroundint.__itemsize__/type(int).slots.itemsizewould catch that).crates/vm/src/builtins/bytes.rs (1)
189-205:itemsize = 1onbytesis consistent with element size and flag usageMarking
byteswithitemsize = 1aligns tp_itemsize with the per‑byte element size and lets the type/slots machinery treat it as variable‑sized, without changing SEQUENCE flags (bytes still only has BASETYPE and_MATCH_SELF, which is consistent with earlier decisions about sequence flags).crates/vm/src/types/slot.rs (1)
120-187: Newitemsizefield cleanly exposestp_itemsizeinPyTypeSlotsAdding
pub itemsize: usizewith aDefaultof 0 and wiring it throughPyTypeSlots::new/heap_defaultis sound; types that care about variable-size layout now have an explicit slot to populate, while existing code that doesn’t touchitemsizecontinues to see a fixed-size (0) default.crates/vm/src/builtins/tuple.rs (1)
259-263: Tupleitemsizecorrectly reflects per-element pointer sizeSetting
itemsize = std::mem::size_of::<crate::PyObjectRef>()makestuple.__itemsize__line up with the pointer-sized element layout and lets the type system treat tuples as variable-sized for layout/slots checks. Flags still includeSEQUENCE, which matches the earlier guidance that only list and tuple should carry the SEQUENCE flag for pattern matching.