1 // Copyright (C) 2007-2023 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 #include <structmember.h>
30 #include "utilities.h"
31 #include "Container_init_python.hxx"
33 #if PY_VERSION_HEX < 0x03050000
35 Py_EncodeLocale(const wchar_t *arg, size_t *size)
37 return _Py_wchar2char(arg, size);
40 Py_DecodeLocale(const char *arg, size_t *size)
42 return _Py_char2wchar(arg, size);
47 The following functions are used to hook the Python
51 static void ContainerPyStdOut_dealloc(ContainerPyStdOut *self)
57 ContainerPyStdOut_write(ContainerPyStdOut *self, PyObject *args)
60 if (!PyArg_ParseTuple(args, "s",&c))
69 self->_cb(self->_data,c);
76 ContainerPyStdOut_flush(ContainerPyStdOut */*self*/, PyObject */*args*/)
82 static PyMethodDef ContainerPyStdOut_methods[] = {
83 {"write", (PyCFunction)ContainerPyStdOut_write, METH_VARARGS, PyDoc_STR("write(string) -> None")},
84 {"flush", (PyCFunction)ContainerPyStdOut_flush, METH_NOARGS, PyDoc_STR("flush() -> None")},
85 {0, 0, 0, 0} /* sentinel */
88 static PyMemberDef ContainerPyStdOut_memberlist[] = {
89 {(char*)"softspace", T_INT, offsetof(ContainerPyStdOut, softspace), 0, (char*)"flag indicating that a space needs to be printed; used by print"},
90 {0, 0, 0, 0, 0} /* sentinel */
93 static PyTypeObject ContainerPyStdOut_Type = {
94 /* The ob_type field must be initialized in the module init function
95 * to be portable to Windows without using C++. */
96 PyVarObject_HEAD_INIT(NULL, 0)
99 sizeof(ContainerPyStdOut), /*tp_basicsize*/
102 (destructor)ContainerPyStdOut_dealloc, /*tp_dealloc*/
109 0, /*tp_as_sequence*/
114 PyObject_GenericGetAttr, /*tp_getattro*/
115 /* softspace is writable: we must supply tp_setattro */
116 PyObject_GenericSetAttr, /* tp_setattro */
118 Py_TPFLAGS_DEFAULT, /*tp_flags*/
122 0, /*tp_richcompare*/
123 0, /*tp_weaklistoffset*/
126 ContainerPyStdOut_methods, /*tp_methods*/
127 ContainerPyStdOut_memberlist, /*tp_members*/
145 0, /*tp_version_tag*/
149 static ContainerPyStdOut* ContainerNewPyStdOut( bool iscerr )
151 ContainerPyStdOut *self;
152 self = PyObject_New(ContainerPyStdOut, &ContainerPyStdOut_Type);
157 self->_iscerr = iscerr;
161 void KERNEL_PYTHON::init_python(int argc, char **argv)
163 if (Py_IsInitialized())
165 MESSAGE("Python already initialized");
168 MESSAGE("=================================================================");
169 MESSAGE("Python Initialization...");
170 MESSAGE("=================================================================");
171 // set stdout to line buffering (aka C++ std::cout)
172 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
173 char* env_python=getenv("SALOME_PYTHON");
176 wchar_t* salome_python = Py_DecodeLocale(env_python, NULL);
177 Py_SetProgramName(salome_python);
179 Py_Initialize(); // Initialize the interpreter
180 if (Py_IsInitialized())
182 MESSAGE("Python initialized eh eh eh");
184 wchar_t **changed_argv = new wchar_t*[argc]; // Setting arguments
185 for (int i = 0; i < argc; i++)
187 changed_argv[i] = Py_DecodeLocale(argv[i], NULL);
189 PySys_SetArgv(argc, changed_argv);
191 PyRun_SimpleString("import threading\n");
192 // VSR (22/09/2016): This is a workaround to prevent invoking qFatal() from PyQt5
193 // causing application aborting
195 script += "def _custom_except_hook(exc_type, exc_value, exc_traceback):\n";
196 script += " import sys\n";
197 script += " sys.__excepthook__(exc_type, exc_value, exc_traceback)\n";
200 script += "import sys\n";
201 script += "sys.excepthook = _custom_except_hook\n";
202 script += "del _custom_except_hook, sys\n";
203 PyRun_SimpleString(script.c_str());
204 // VSR (22/09/2016): end of workaround
205 #if PY_VERSION_HEX < 0x03070000
206 PyEval_InitThreads(); // Create (and acquire) the interpreter lock
208 // Create python objects to capture stdout and stderr
209 PyObject* _vout=(PyObject*)ContainerNewPyStdOut( false ); // stdout
210 PyObject* _verr=(PyObject*)ContainerNewPyStdOut( true ); // stderr
211 PySys_SetObject((char*)"stderr",_verr);
212 PySys_SetObject((char*)"stdout",_vout);
214 PyThreadState *pts = PyGILState_GetThisThreadState();
215 PyEval_ReleaseThread(pts);
216 //delete[] changed_argv;