From 79809b6f29abe8cb1e95d1d08f9e2ea50ed49be7 Mon Sep 17 00:00:00 2001 From: abn Date: Mon, 13 Oct 2014 14:03:40 +0200 Subject: [PATCH] Clearly identify when we execute Python code from SALOME embedded console. --- src/PyConsole/PyConsole_Interp.cxx | 18 +++++++++++++++--- src/PyConsole/PyConsole_Interp.h | 3 +++ src/PyInterp/PyInterp_Interp.cxx | 18 ++++++++++++++++-- src/PyInterp/PyInterp_Interp.h | 1 + 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/PyConsole/PyConsole_Interp.cxx b/src/PyConsole/PyConsole_Interp.cxx index c200a85c0..01c94fe20 100644 --- a/src/PyConsole/PyConsole_Interp.cxx +++ b/src/PyConsole/PyConsole_Interp.cxx @@ -48,15 +48,27 @@ Creates new python interpreter. */ PyConsole_Interp::PyConsole_Interp(): PyInterp_Interp() -{ -} +{} /*! \brief Destructor. Does nothing for the moment. */ -PyConsole_Interp::~PyConsole_Interp() +PyConsole_Interp::~PyConsole_Interp() { } + +/*! Sets the variable "__IN_SALOME_GUI_CONSOLE" to True. +* This is not attached to a module (like salome_iapp.IN_SALOME_GUI_CONSOLE) +* since modules are shared across all interpreters in SALOME. +* +* (GIL is already acquired here) +*/ +int PyConsole_Interp::beforeRun() { + return PyRun_SimpleString("__builtins__.__IN_SALOME_GUI_CONSOLE=True"); } +int PyConsole_Interp::afterRun() +{ + return PyRun_SimpleString("__builtins__.__IN_SALOME_GUI_CONSOLE=False"); +} diff --git a/src/PyConsole/PyConsole_Interp.h b/src/PyConsole/PyConsole_Interp.h index 0b929332e..5434e4813 100644 --- a/src/PyConsole/PyConsole_Interp.h +++ b/src/PyConsole/PyConsole_Interp.h @@ -37,6 +37,9 @@ class PYCONSOLE_EXPORT PyConsole_Interp : public PyInterp_Interp public: PyConsole_Interp(); ~PyConsole_Interp(); + + virtual int afterRun(); + virtual int beforeRun(); }; #endif // PYCONSOLE_INTERP_H diff --git a/src/PyInterp/PyInterp_Interp.cxx b/src/PyInterp/PyInterp_Interp.cxx index f76c3c69e..df95ecfb5 100644 --- a/src/PyInterp/PyInterp_Interp.cxx +++ b/src/PyInterp/PyInterp_Interp.cxx @@ -293,7 +293,10 @@ bool PyInterp_Interp::initContext() _global_context = PyModule_GetDict(m); // get interpreter global variable context Py_INCREF(_global_context); _local_context = _global_context; - return true; + + int ret = PyRun_SimpleString("import salome_iapp;salome_iapp.IN_SALOME_GUI=True"); + + return ret == 0; } /*! @@ -412,7 +415,9 @@ static int compile_command(const char *command, PyObject * global_ctxt, PyObject int PyInterp_Interp::run(const char *command) { beforeRun(); - return simpleRun(command); + int ret = simpleRun(command); + afterRun(); + return ret; } /** @@ -424,6 +429,15 @@ int PyInterp_Interp::beforeRun() 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 diff --git a/src/PyInterp/PyInterp_Interp.h b/src/PyInterp/PyInterp_Interp.h index 33c6a2d47..7883cd4ec 100644 --- a/src/PyInterp/PyInterp_Interp.h +++ b/src/PyInterp/PyInterp_Interp.h @@ -89,6 +89,7 @@ protected: std::list::iterator _ith; virtual int beforeRun(); + virtual int afterRun(); int simpleRun(const char* command, const bool addToHistory = true); virtual void initPython(); -- 2.39.2