Salome HOME
Merge remote-tracking branch 'origin/vsr/fix_single_study_pb' into pre/fix_single_study
[modules/gui.git] / src / PyConsole / PyConsole_Interp.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  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, or (at your option) any later version.
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 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
24 //  File   : PyConsole_Interp.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SALOME
27 //
28 #include "PyConsole_Interp.h"
29
30 /*!
31   \class PyConsole_Interp
32   \brief Python interpreter to be embedded to the SALOME study's GUI.
33
34   There is only one Python interpreter for the whole SALOME environment.
35
36   Call the initialize() method defined in the base class PyInterp_Interp,
37   to initialize the interpreter after instance creation.
38
39   The method initialize() calls virtuals methods
40   - initPython()  to initialize global Python interpreter
41   //- initState()   to initialize embedded interpreter state (OBSOLETE)
42   - initContext() to initialize interpreter internal context
43   - initRun()     to prepare interpreter for running commands
44 */
45
46 /*!
47   \brief Constructor.
48
49   Creates new python interpreter.
50 */
51 PyConsole_Interp::PyConsole_Interp(): PyInterp_Interp()
52 {
53 }
54
55 /*!
56   \brief Destructor.
57
58   Does nothing for the moment.
59 */
60 PyConsole_Interp::~PyConsole_Interp()
61 {
62 }
63  
64 /*!
65   \brief Initialize python interpeter context.
66
67   The GIL is assumed to be held.
68   It is the caller responsability to acquire the GIL.
69   It must still be held on initContext() exit.
70
71   \return \c true on success
72 */
73 bool PyConsole_Interp::initContext()
74 {
75   PyObject *m = PyImport_AddModule("__main__");  // interpreter main module (module context)
76   if(!m){
77     PyErr_Print();
78     return false;
79   }  
80   PyObject* global_dict = PyModule_GetDict(m);          // get interpreter dictionnary context
81   _context = PyDict_Copy(global_dict);
82   return true;
83 }