-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Prevent __class__ reassignment across incompatible layouts #6521
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
📝 WalkthroughWalkthroughThe PR adds layout compatibility checks for Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (4 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 |
|
Code has been automatically formatted The code in this PR has been formatted using git pull origin copilot/fix-class-attribute-panic |
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.
Instead of adding test in rust code, add test test in builtin_type.py
|
@copilot try again |
|
@copilot investigate regressions in test_descr test_importlib test_venv |
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. |
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>
9376bcd to
e0090ff
Compare
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
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)
extra_tests/snippets/builtin_type.py (1)
280-291: Consider adding an assertion to verify the class was actually changed.The test verifies that
slot_a.__class__ = SlotA2doesn't raise, but doesn't verify the assignment actually took effect.🔎 Proposed addition
slot_a = SlotA() slot_a.__class__ = SlotA2 # Should work +assert type(slot_a) is SlotA2 +assert slot_a.__class__ is SlotA2crates/vm/src/builtins/object.rs (1)
529-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.rsandcrates/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.
📜 Review details
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
crates/vm/src/builtins/object.rsextra_tests/snippets/builtin_type.py
🧰 Additional context used
📓 Path-based instructions (2)
**/*.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/object.rs
**/*.py
📄 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/builtin_type.py
🧬 Code graph analysis (1)
extra_tests/snippets/builtin_type.py (1)
extra_tests/snippets/testutils.py (1)
assert_raises(5-12)
⏰ 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)
- GitHub Check: Run rust tests (ubuntu-latest)
- GitHub Check: Run tests under miri
- GitHub Check: Run snippets and cpython tests (windows-latest)
- GitHub Check: Run snippets and cpython tests (macos-latest)
- GitHub Check: Run snippets and cpython tests (ubuntu-latest)
- GitHub Check: Run rust tests (windows-latest)
- GitHub Check: Run rust tests (macos-latest)
- GitHub Check: Ensure compilation on various targets
- GitHub Check: Check the WASM package and demo
- GitHub Check: Check Rust code with clippy
🔇 Additional comments (4)
crates/vm/src/builtins/object.rs (2)
500-502: Good refactoring to cachecurrent_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 usingzipis 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.
extra_tests/snippets/builtin_type.py (2)
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 aTypeErrorwith the appropriate message. The use ofassert_raisescontext 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.
extra_tests/snippets/builtin_type.pyand remove the Rust-side unit test per review feedback.rustpython-vmtest 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.