Skip to content

Commit

Permalink
[CPyCppyy] Avoid holding reference to __main__ in the CPyCppyy API
Browse files Browse the repository at this point in the history
The `gMainDict` should be borrowed, i.e. we are not calling Py_INCREF(gMainDict).
Like this, we avoid unexpectedly affecting how long `__main__` is kept
alive. The `gMainDict` is only used in `Exec()`, `ExecScript()`, and `Eval()`,
which should not be called after `__main__` is garbage collected anyway.
  • Loading branch information
guitargeek committed Sep 11, 2024
1 parent 12a6158 commit 2b64c0f
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion bindings/pyroot/cppyy/CPyCppyy/src/API.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ static bool Initialize()
// retrieve the main dictionary
gMainDict = PyModule_GetDict(
PyImport_AddModule(const_cast<char*>("__main__")));
Py_INCREF(gMainDict);
// The gMainDict is borrowed, i.e. we are not calling Py_INCREF(gMainDict).
// Like this, we avoid unexpectedly affecting how long __main__ is kept
// alive. The gMainDict is only used in Exec(), ExecScript(), and Eval(),
// which should not be called after __main__ is garbage collected anyway.
}

// declare success ...
Expand Down

0 comments on commit 2b64c0f

Please sign in to comment.