Skip to content

feat(vm/slot): implement Py_TPFLAGS_MANAGED_DICT for class objects #5949

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

Merged
merged 2 commits into from
Jul 20, 2025

Conversation

ever0de
Copy link
Contributor

@ever0de ever0de commented Jul 11, 2025

Reference

Waiting for CPython Issue(PR)
python/cpython#136535

#5949 (comment)

Summary by CodeRabbit

  • New Features

    • Improved handling of type attributes for custom classes, ensuring correct support for types with and without __slots__.
  • Bug Fixes

    • Fixed an issue where types with __slots__ were incorrectly assigned dictionary-related flags.
  • Chores

    • Updated internal flags to better represent type characteristics.

Copy link
Contributor

coderabbitai bot commented Jul 11, 2025

Walkthrough

The changes introduce a new MANAGED_DICT flag to the type flags system and update type construction logic to conditionally set the HAS_DICT and MANAGED_DICT flags only when the __slots__ attribute is absent. This refines the way the runtime tracks dictionary management capabilities of Python types.

Changes

File(s) Change Summary
vm/src/builtins/type.rs Adjusted type construction: HAS_DICT and new MANAGED_DICT flags set only if __slots__ absent. Removed old TODO comment.
vm/src/types/slot.rs Added MANAGED_DICT constant to PyTypeFlags bitflags struct.

Poem

In the warren of types, a new flag appears,
For managed dicts, the intent now clears.
If slots are defined, no dicts to be found,
But without them, both flags abound!
With bits set just right, the VM hops on,
Type logic refined, and the bugs are all gone.
🐇✨


📜 Recent review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5284b73 and 32b24c1.

⛔ Files ignored due to path filters (4)
  • Lib/test/test_array.py is excluded by !Lib/**
  • Lib/test/test_importlib/test_metadata_api.py is excluded by !Lib/**
  • Lib/test/test_property.py is excluded by !Lib/**
  • Lib/test/test_weakref.py is excluded by !Lib/**
📒 Files selected for processing (2)
  • vm/src/builtins/type.rs (1 hunks)
  • vm/src/types/slot.rs (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

Instructions used from:

Sources:
📄 CodeRabbit Inference Engine

  • .github/copilot-instructions.md
⏰ 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). (9)
  • GitHub Check: Check Rust code with rustfmt and clippy
  • GitHub Check: Run rust tests (ubuntu-latest)
  • GitHub Check: Ensure compilation on various targets
  • GitHub Check: Run rust tests (macos-latest)
  • GitHub Check: Check the WASM package and demo
  • GitHub Check: Run snippets and cpython tests (windows-latest)
  • GitHub Check: Run snippets and cpython tests (ubuntu-latest)
  • GitHub Check: Run rust tests (windows-latest)
  • GitHub Check: Run snippets and cpython tests (macos-latest)
🔇 Additional comments (2)
vm/src/types/slot.rs (1)

125-125: LGTM - Correct implementation of MANAGED_DICT flag.

The new MANAGED_DICT flag is properly implemented following the existing pattern. The bit position (1 << 4 = 16) is appropriate and doesn't conflict with existing flags.

vm/src/builtins/type.rs (1)

1072-1077: LGTM - Correct conditional flag setting logic.

The implementation properly conditionalizes the HAS_DICT and MANAGED_DICT flags based on the presence of __slots__. This aligns with CPython's behavior where types defining __slots__ don't use managed dictionaries for attribute storage. The logic is clear and well-commented.

✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@ever0de ever0de force-pushed the feat/type-managed-dict-flag branch from b6f4667 to be42c2b Compare July 11, 2025 16:18
Comment on lines +1053 to +1077
let mut flags = PyTypeFlags::heap_type_flags();
// Only add HAS_DICT and MANAGED_DICT if __slots__ is not defined.
if heaptype_slots.is_none() {
flags |= PyTypeFlags::HAS_DICT | PyTypeFlags::MANAGED_DICT;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about splitting the patch one to contains this part and the other one contains the test_class part? I'd like to merge the test_class part as the same form of CPython patch, but this one doesn't need to be.

Copy link
Contributor Author

@ever0de ever0de Jul 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll split the test_class part into a new PR.

@ever0de ever0de force-pushed the feat/type-managed-dict-flag branch 2 times, most recently from a0cef6d to 6427732 Compare July 20, 2025 04:17
@ever0de ever0de force-pushed the feat/type-managed-dict-flag branch from 6427732 to 32b24c1 Compare July 20, 2025 04:26
@ever0de
Copy link
Contributor Author

ever0de commented Jul 20, 2025

This implementation targets test_flags in test_class, but the test should be enabled only after the corresponding CPython PR is merged.

@ever0de ever0de marked this pull request as ready for review July 20, 2025 04:28
Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@youknowone youknowone merged commit ff35dcd into RustPython:main Jul 20, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants