From 51a8bbef10d2e582cd6fd42e41760881e1a1f1bb Mon Sep 17 00:00:00 2001 From: Aaron Jomy Date: Sat, 29 Aug 2026 12:59:26 +0200 Subject: [PATCH] [cpyrt] Import the cppjit module when 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. --- src/cpyrt/API.cxx | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/cpyrt/API.cxx b/src/cpyrt/API.cxx index c0f4651..b0da815 100644 --- a/src/cpyrt/API.cxx +++ b/src/cpyrt/API.cxx @@ -59,9 +59,15 @@ static bool Initialize() { << std::endl; return false; } + } - // force loading of the cppjit module - PyRun_SimpleString(const_cast("import cppjit")); + // Importing the extension module is what runs the cpyrt initialization + // that sets gThisModule. + if (!cpyrt::gThisModule) { + PyObject* cppjitmod = PyImport_ImportModule("cppjit"); + if (!cppjitmod) + return false; + Py_DECREF(cppjitmod); } if (!gMainDict) {