Skip to content

[cpyrt] Import the cppjit module when gThisModule is null - #97

Merged
aaronj0 merged 1 commit into
compiler-research:mainfrom
aaronj0:import-on-null-gthismodule
Sep 15, 2026
Merged

aaronj0 merged 1 commit into
compiler-research:mainfrom
aaronj0:import-on-null-gthismodule

Conversation

@aaronj0

@aaronj0 aaronj0 commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

Entering through the public API (Instance_FromVoidPtr) without importing cppjit leaves gThisModule null. With Python already initialized, Initialize() skipped the import and reported success anyway, so CreateScopeProxy() dereferenced null. Import the extension module whenever gThisModule is null.

Entering through the public API (Instance_FromVoidPtr) without importing
cppjit leaves gThisModule null. With Python already initialized,
Initialize() skipped the import and reported success anyway, so
CreateScopeProxy() dereferenced null. Import the extension module
whenever gThisModule is null.
Comment thread src/cpyrt/API.cxx
PyRun_SimpleString(const_cast<char*>("import cppjit"));
// Importing the extension module is what runs the cpyrt initialization
// that sets gThisModule.
if (!cpyrt::gThisModule) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we open an issue tracking the progress of removing global state?

@guitargeek guitargeek Sep 13, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sure, please go ahead! Would be useful if the issue said which globals you have in mind (gThisModule, gPyTypeMap, gMainDict, the PyString globals? ..) and what problem they cause (subinterpreters, teardown, etc.), so it's actionable rather than a general cleanup item. Because in general, global "immortal" state is not an anti-pattern in CPython.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I'm currently investigating this and will open an issue with more details

@guitargeek guitargeek left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM!

@aaronj0
aaronj0 merged commit 7febc63 into compiler-research:main Sep 15, 2026
26 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.

3 participants