Add tp_str#6495
Conversation
📝 WalkthroughWalkthroughThis PR adds Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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 |
Sorry, something went wrong.
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
crates/vm/src/types/slot.rs (1)
488-873: Critical: Missingupdate_slotcase for__str__.The
update_slotfunction handles dynamic slot updates when attributes are added/removed from types, but there's no case for__str__. This means when a heap type dynamically defines or removes__str__, thestrslot won't be updated, potentially breaking the slot mechanism for dynamically defined__str__methods.Add a case similar to
__repr__(lines 570-572):🔎 Proposed fix
_ if name == identifier!(ctx, __repr__) => { update_slot!(repr, repr_wrapper); } + _ if name == identifier!(ctx, __str__) => { + toggle_slot!(str, str_wrapper); + } _ if name == identifier!(ctx, __hash__) => {Note: You'll also need to define
str_wrapperfunction similar torepr_wrapper(line 351-359) if it doesn't already exist:fn str_wrapper(zelf: &PyObject, vm: &VirtualMachine) -> PyResult<PyRef<PyStr>> { let ret = vm.call_special_method(zelf, identifier!(vm, __str__), ())?; ret.downcast::<PyStr>().map_err(|obj| { vm.new_type_error(format!( "__str__ returned non-string (type {})", obj.class() )) }) }
📜 Review details
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
Lib/test/test_inspect/test_inspect.pyis excluded by!Lib/**
📒 Files selected for processing (4)
crates/vm/src/builtins/descriptor.rscrates/vm/src/builtins/object.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 Rust 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/descriptor.rscrates/vm/src/types/slot.rscrates/vm/src/builtins/object.rscrates/vm/src/class.rs
🧬 Code graph analysis (1)
crates/vm/src/class.rs (2)
crates/vm/src/types/slot.rs (4)
iter(1440-1440)iter(1476-1478)hash_not_implemented(374-376)hash(1103-1103)crates/vm/src/builtins/set.rs (2)
hash(438-461)hash(1155-1173)
⏰ 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). (2)
- GitHub Check: Run snippets and cpython tests (windows-latest)
- GitHub Check: Run snippets and cpython tests (ubuntu-latest)
🔇 Additional comments (5)
crates/vm/src/types/slot.rs (1)
143-143: LGTM:strslot field added correctly.The new
strfield follows the same pattern as the existingreprslot on line 144.crates/vm/src/builtins/descriptor.rs (2)
402-402: LGTM:Strvariant added correctly.The new
Str(StringifyFunc)variant properly extends theSlotFuncenum to support the__str__slot.
438-449: LGTM: Unified handling ofReprandStrimproves maintainability.The refactored approach consolidates the argument validation and dispatch logic for both
__repr__and__str__, reducing code duplication while correctly differentiating error messages.crates/vm/src/class.rs (2)
168-168: LGTM:__str__slot wrapper added correctly.The slot wrapper follows the same pattern as
__repr__on line 167 and properly integrates the newStrslot with the class implementation machinery.
173-177: LGTM: Cleaner__hash__handling.The refactored approach using
map_orand pointer comparison is more concise and readable than the previous implementation, while maintaining the same behavior.
Sorry, something went wrong.
aae6bf5
into
RustPython:main
Dec 25, 2025
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.