-// Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
//
// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
/*!
* 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()
{
}
_global_context = PyModule_GetDict(m); // get interpreter global variable context
Py_INCREF(_global_context);
- _local_context = PyDict_New();
- return true;
+ _local_context = _global_context;
+
+ int ret = PyRun_SimpleString("import salome_iapp;salome_iapp.IN_SALOME_GUI=True");
+
+ return ret == 0;
}
/*!
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);
}
/*!
int PyInterp_Interp::run(const char *command)
{
beforeRun();
- return simpleRun(command);
+ int ret = simpleRun(command);
+ afterRun();
+ return ret;
}
/**
return 0;
}
+/**
+ * Called after a command is run (when calling run() method). Not thread-safe. Caller's responsability
+ * to acquire GIL if needed.
+ */
+int PyInterp_Interp::afterRun()
+{
+ return 0;
+}
+
/*!
\brief Run Python command (used internally). Not thread-safe. GIL acquisition is caller's responsability.
\param command Python command