Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 25, 2025

The VM lacked a consistent module-level state mechanism analogous to CPython’s PyModule_GetState.

  • API: Added PyModule::get_state and get_or_try_init_state, storing state behind a PyRwLock<Option<PyObjectRef>>, initializing under a write lock, and raising TypeError on incompatible existing state.
  • Behavior: Documented semantics (returns None if absent or wrong type; init invoked only once). Improved error messaging for mismatched state types.
  • Tests: Added a unit test ensuring per-module isolation, reuse of initialized state, and type-mismatch error behavior.

Example:

let state = module.get_or_try_init_state(vm, |vm| Ok(vm.ctx.new_int(1)))?; // init once
assert!(module.get_state::<PyInt>().is_some());                            // retrieve
Original prompt

This section details on the original issue you should resolve

<issue_title>[RFC] module state for PyModule or builtin module</issue_title>
<issue_description>## Summary

related CPython function: PyModule_GetState
We are handling them with sometimes module attr and the other times static variable.

Detailed Explanation

Drawbacks, Rationale, and Alternatives

Unresolved Questions

</issue_description>

<agent_instructions>Investigate how CPython handle module state and try to add one</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 25, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI and others added 2 commits December 25, 2025 10:37
Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
Copilot AI changed the title [WIP] Add module state handling for PyModule Add per-module state handling and API to PyModule Dec 25, 2025
Copilot AI requested a review from youknowone December 25, 2025 11:03
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.

[RFC] module state for PyModule or builtin module

2 participants