From 10c11886e66ffefb2256d55ecf6485aaaef6d1d8 Mon Sep 17 00:00:00 2001 From: abn Date: Mon, 22 Sep 2014 09:36:10 +0200 Subject: [PATCH] Python console, bug fix: global and local contexts have to be the same dictionary for code run at the module level (the case of code that is run in Salome console). --- src/PyInterp/PyInterp_Interp.cxx | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/PyInterp/PyInterp_Interp.cxx b/src/PyInterp/PyInterp_Interp.cxx index 4664009ef..f76c3c69e 100644 --- a/src/PyInterp/PyInterp_Interp.cxx +++ b/src/PyInterp/PyInterp_Interp.cxx @@ -278,6 +278,10 @@ bool PyInterp_Interp::initRun() /*! * Initialize context dictionaries. GIL is held already. + * The code executed in an embedded interpreter is expected to be run at the module + * level, in which case local and global context have to be the same dictionary. + * See: http://stackoverflow.com/questions/12265756/c-python-running-python-code-within-a-context + * for an explanation. */ bool PyInterp_Interp::initContext() { @@ -288,7 +292,7 @@ bool PyInterp_Interp::initContext() } _global_context = PyModule_GetDict(m); // get interpreter global variable context Py_INCREF(_global_context); - _local_context = PyDict_New(); + _local_context = _global_context; return true; } @@ -298,7 +302,8 @@ bool PyInterp_Interp::initContext() void PyInterp_Interp::closeContext() { Py_XDECREF(_global_context); - Py_XDECREF(_local_context); + // both _global and _local point to the same Python object: + // Py_XDECREF(_local_context); } /*! -- 2.39.2