| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
📝 Walkthrough
WalkthroughThe PR adds layout compatibility checks for __class__ reassignment in RustPython's object implementation. When assigning a new class to an instance's __class__ attribute, the code now validates that both types have compatible memory layouts by comparing basicsize, slot definitions, and dict-slot presence. If layouts differ, it raises a descriptive TypeError instead of panicking. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
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. ❤️ ShareComment @coderabbitai help to get the list of available commands and usage tips. |
Sorry, something went wrong.
|
Code has been automatically formatted The code in this PR has been formatted using cargo fmt --all. git pull origin copilot/fix-class-attribute-panic |
Sorry, something went wrong.
There was a problem hiding this comment.
Instead of adding test in rust code, add test test in builtin_type.py
Sorry, something went wrong.
|
@copilot investigate regressions in test_descr test_importlib test_venv |
Sorry, something went wrong.
CI failures came from module class reassignment (_LazyModule/RequestModule) tripping the new layout guard. Relaxed the payload check so module subclasses can swap classes when layouts match in 9376bcd; ensurepip/importlib/venv now proceed. |
Sorry, something went wrong.
Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
|
✅ Actions performed
Review triggered.
|
Sorry, something went wrong.
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (2)extra_tests/snippets/builtin_type.py (1)📜 Review detailscrates/vm/src/builtins/object.rs (1)280-291: Consider adding an assertion to verify the class was actually changed.
The test verifies that slot_a.__class__ = SlotA2 doesn't raise, but doesn't verify the assignment actually took effect.
🔎 Proposed additionslot_a = SlotA() slot_a.__class__ = SlotA2 # Should work +assert type(slot_a) is SlotA2 +assert slot_a.__class__ is SlotA2529-538: Itemsize check is missing—already noted in TODO comments.
The layout compatibility check covers basicsize, slot names/count, and HAS_DICT differences but omits itemsize comparison. This gap is already documented in TODO comments elsewhere in the codebase (crates/vm/src/builtins/object.rs and crates/vm/src/builtins/type.rs). Variable-size object handling is relevant for RustPython's buffer protocol and variable-size types, though the scope of user-defined variable-size classes may differ from CPython. Weakref layout differences are also not checked; consider whether RustPython's weakref handling requires explicit layout validation for class assignment.
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📥 CommitsReviewing files that changed from the base of the PR and between b704f42 and e0090ff.
📒 Files selected for processing (2)📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.rs: Follow the default rustfmt code style by running cargo fmt to 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:
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.py: In most cases, Python code should not be edited; bug fixes should be made through Rust code modifications only
Follow PEP 8 style for custom Python code
Use ruff for linting Python code
Files:
extra_tests/snippets/testutils.py (1)⏰ 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). (10)
- assert_raises (5-12)
crates/vm/src/builtins/object.rs (2)extra_tests/snippets/builtin_type.py (2)500-502: Good refactoring to cache current_cls.
Caching instance.class() ensures consistency throughout the method, avoiding potential issues if the class were to change between checks (though unlikely in practice).
510-528: The positional slot comparison using zip is overly restrictive and does not match CPython's actual behavior.
CPython allows class reassignment even when slots are in different orders (e.g., ("x", "y") and ("y", "x")), but the current implementation rejects such cases because it compares slots positionally. This should either be fixed to allow reordered slots (like CPython does) or have a clear documented reason for the stricter validation.
Likely an incorrect or invalid review comment.
243-260: Good coverage for dict vs slots layout mismatch.
This test correctly verifies that assigning a class with __slots__ to an instance of a class with __dict__ raises a TypeError with the appropriate message. The use of assert_raises context manager with message validation is good practice.
263-277: Nice edge case: same slot count, different slot names.
This test is valuable because it validates that the implementation compares slot names, not just slot counts. This catches a potential bug where only len() comparison might have been used.
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
- Guard class setter to reject incompatible layouts while still permitting module-style class swaps (e.g., lazy modules and request modules) when their layouts match, avoiding panics and keeping import/ensurepip flows working.
- Move the regression coverage for layout-mismatch errors into extra_tests/snippets/builtin_type.py and remove the Rust-side unit test per review feedback.
- Ran targeted rustpython-vm test to verify the build after the guard adjustment.
Original prompt💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.
Summary by CodeRabbit
Bug Fixes
Tests
✏️ Tip: You can customize this high-level summary in your review settings.