Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/gui.git] / src / PythonConsole / PythonConsole_PyInterp.cxx
1 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : PythonConsole_PyInterp.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SALOME
27 //  $Header$
28
29 #include "PythonConsole_PyInterp.h"
30 //#include "utilities.h"
31
32 using namespace std;
33
34
35 //#ifdef _DEBUG_
36 //static int MYDEBUG = 0;
37 //#else
38 //static int MYDEBUG = 0;
39 //#endif
40
41
42 /*!
43  * constructor : multi Python interpreter, one per SALOME study.
44  * calls initialize method defined in base class, which calls virtual methods
45  * initstate & initcontext redefined here.
46  */
47 PythonConsole_PyInterp::PythonConsole_PyInterp(): PyInterp_base()
48 {
49 }
50
51 /*!
52   Destructor
53 */
54 PythonConsole_PyInterp::~PythonConsole_PyInterp()
55 {
56 }
57  
58 /*!
59  * EDF-CCAR
60  * When SALOME uses multi Python interpreter feature,
61  * Every study has its own interpreter and thread state (_tstate = Py_NewInterpreter())
62  * This is fine because every study has its own modules (sys.modules) stdout and stderr
63  * BUT some Python modules must be imported only once. In multi interpreter context Python
64  * modules (*.py) are imported several times.
65  * The pyqt module must be imported only once because it registers classes in a C module.
66  * It's quite the same with omniorb modules (internals and generated with omniidl)
67  * This problem is handled with "shared modules" defined in salome_shared_modules.py
68  * These "shared modules" are imported only once and only copied in all the other interpreters
69  * BUT it's not the only problem. Every interpreter has its own __builtin__ module. That's fine
70  * but if we have copied some modules and imported others problems may arise with operations that
71  * are not allowed in restricted execution environment. So we must impose that all interpreters
72  * have identical __builtin__ module.
73  * That's all, for the moment ...
74  */
75
76 bool PythonConsole_PyInterp::initState()
77 {
78   /*
79    * The GIL is acquired on input and released on output
80    */
81     /*PyEval_AcquireLock();
82 #ifdef WNT 
83   _tstate = PyGILState_GetThisThreadState();
84   // if no thread state defined
85   if ( _tstate )
86     PyThreadState_Swap(_tstate);
87   else
88 #endif
89   {
90     _tstate = Py_NewInterpreter(); // create an interpreter and save current state
91     PySys_SetArgv(PyInterp_base::_argc,PyInterp_base::_argv); // initialize sys.argv
92     //if(MYDEBUG) MESSAGE("PythonConsole_PyInterp::initState - this = "<<this<<"; _tstate = "<<_tstate);
93   }*/
94
95   /*
96    * The GIL is acquired and will be held on initState output
97    * It is the caller responsability to release the lock if needed
98    */
99   PyEval_AcquireLock();
100
101   _tstate = Py_NewInterpreter(); // create an interpreter and save current state
102   PySys_SetArgv(PyInterp_base::_argc,PyInterp_base::_argv); // initialize sys.argv
103 //  if(MYDEBUG) MESSAGE("PythonConsole_PyInterp::initState - this = "<<this<<"; _tstate = "<<_tstate);
104
105
106   /*
107    * If builtinmodule has been initialized all the sub interpreters
108    * will have the same __builtin__ module
109    */
110  
111   if(!builtinmodule) // PAL18041: deepcopy function don't work in Salome
112   {
113     //builtinmodule is static member of PyInterp class
114     //If it is not NULL (initialized to the builtin module of the main interpreter
115     //all the sub interpreters will have the same builtin
116     //_interp is a static member and is the main interpreter
117     //The first time we initialized it to the builtin of main interpreter
118     builtinmodule=PyDict_GetItemString(_interp->modules, "__builtin__");
119   }
120
121   if(builtinmodule)
122     { 
123     PyObject *m = PyImport_GetModuleDict();
124     PyDict_SetItemString(m, "__builtin__", builtinmodule);
125 //    SCRUTE(builtinmodule->ob_refcnt); // builtinmodule reference counter
126     _tstate->interp->builtins = PyModule_GetDict(builtinmodule);
127     Py_INCREF(_tstate->interp->builtins);
128   }    
129   PyEval_ReleaseThread(_tstate);
130   return true;
131 }
132
133 /*!
134    The GIL is assumed to be held
135    It is the caller responsability caller to acquire the GIL
136    It will still be held on initContext output
137 */
138 bool PythonConsole_PyInterp::initContext()
139 {
140   PyObject *m = PyImport_AddModule("__main__");  // interpreter main module (module context)
141   if(!m){
142 //    if(MYDEBUG) MESSAGE("problem...");
143     PyErr_Print();
144 //    ASSERT(0);
145     return false;
146   }  
147   _g = PyModule_GetDict(m);          // get interpreter dictionnary context
148 //  if(MYDEBUG) MESSAGE("PythonConsole_PyInterp::initContext - this = "<<this<<"; _g = "<<_g);
149
150   if(builtinmodule){
151     PyDict_SetItemString(_g, "__builtins__", builtinmodule); // assign singleton __builtin__ module
152   }
153   return true;
154 }