26 #if !defined(__MAKECINT__) && !defined(__CINT__) && !defined(G__DICTIONARY)
31 #ifdef _POSIX_C_SOURCE
32 #undef _POSIX_C_SOURCE
42 std::string loadScript(
const std::string& fname) {
43 std::ifstream file(fname.c_str());
44 std::stringstream str;
47 while( file.get(ch) ) str.put(ch);
55 static int _blockers = 0;
56 static pthread_t _mainThread = 0;
57 static int _refCount = 0;
58 static PyObject* _main_dict = 0;
59 static PyThreadState *_save_state = 0;
60 int _execPy(
const char* cmd) {
62 PyObject* ret = ::PyRun_String((
char*)cmd, Py_file_input,_main_dict,_main_dict);
63 if ( ::PyErr_Occurred() ) {
68 if ( ret && ret == Py_None ) {
79 int _evalPy(
const char* cmd) {
81 PyObject* ret = ::PyRun_String((
char*)cmd, Py_eval_input,_main_dict,_main_dict);
82 if ( ::PyErr_Occurred() ) {
87 if ( ret && ret == Py_None ) {
101 if ( ::Py_IsInitialized() ) {
102 PyGILState_STATE st = (PyGILState_STATE)::PyGILState_Ensure();
108 if ( ::Py_IsInitialized() ) {
109 PyGILState_STATE st = (PyGILState_STATE)state;
110 ::PyGILState_Release(st);
115 if ( _blockers == 0 ) {
123 if ( _blockers == 0 ) {
139 bool inited = ::Py_IsInitialized();
142 #if PY_MAJOR_VERSION <=2 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 7)
143 ::PyEval_InitThreads();
151 PyObject* module = ::PyImport_AddModule(
"__main__");
152 if ( !module || ::PyErr_Occurred() ) {
155 dd4hep::printout(WARNING,
"DDPython",
"Main dictionary pointer is NULL. Try to continue like this!");
158 _main_dict = ::PyModule_GetDict(module);
160 Py_INCREF( _main_dict );
162 dd4hep::printout(DEBUG,
"DDPython",
"Pointer to main dict:%p",(
void*)_main_dict);
167 if ( !_save_state ) {
175 if ( 0 == _refCount && ::Py_IsInitialized() ) {
176 dd4hep::printout(ALWAYS,
"DDPython",
"+++ Shutdown python interpreter......");
178 Py_DECREF(_main_dict);
182 ::PyEval_RestoreThread(_save_state);
191 if ( 0 == _instance ) _instance =
new DDPython();
197 if ( !_save_state && ::Py_IsInitialized() ) {
198 _save_state = ::PyEval_SaveThread();
204 ::PyEval_RestoreThread(_save_state);
210 std::vector<std::wstring> wargs;
211 std::vector<const wchar_t*> wargv;
212 for(
int i=0; i<argc;++i) {
215 const size_t size = strlen(argv[i]);
218 std::mbstowcs(&wstr[0], argv[i], size);
222 wargs.push_back(wstr);
224 for(
auto& s : wargs ) wargv.push_back(s.c_str());
229 PyConfig_InitPythonConfig( &config );
231 status = PyConfig_SetString( &config, &config.program_name, wargv[0] );
232 status = PyConfig_SetArgv( &config, 1, (
wchar_t**)&wargv[0] );
233 status = Py_InitializeFromConfig( &config );
234 PyConfig_Clear( &config );
240 if ( 0 != _instance ) {
241 if ( 1 == _refCount ) {
249 std::string cmd = loadScript(fname);
254 return _evalPy(cmd.c_str());
258 return _execPy(cmd.c_str());
263 if ( PyCallable_Check(method) ) {
264 PyObject* ret = ::PyObject_CallObject(method,args==Py_None ? NULL : args);
267 ::PyErr_SetString(PyExc_RuntimeError,
"DDPython::call: The argument is not a callable object!");
272 if ( PyCallable_Check(method) ) {
273 PyObject* arg = args==Py_None || args==0 ? 0 : args;
274 PyObject* ret = ::PyObject_CallObject(method,arg);
275 if ( ::PyErr_Occurred() ) {
281 return TPyReturn(ret);
284 throw std::runtime_error(
"dd4hep::DDPython::callC: Object is not callable!");
289 if ( obj && ::Py_IsInitialized() ) {
297 if ( ::Py_IsInitialized() ) {
298 if ( obj ) { Py_DECREF(obj); }
299 if ( new_obj ){ Py_INCREF(new_obj); }
306 ::PyRun_InteractiveLoop(stdin,
const_cast<char*
>(
"\0"));
310 if ( ::Py_IsInitialized() ) {
311 std::cout <<
"[INFO] Re-init python after forking....." << std::endl;
312 #if PY_VERSION_HEX < 0x03070000
315 ::PyOS_AfterFork_Child();
317 #if PY_MAJOR_VERSION <=2 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 7)
318 ::PyEval_InitThreads();
319 ::PyEval_ReleaseLock();
325 _mainThread = pthread_self();
329 return _mainThread == pthread_self();
334 std::vector<std::wstring> wargs;
335 std::vector<const wchar_t*> wargv;
336 for(
int i=0; i<argc;++i) {
339 const size_t size = strlen(argv[i]);
342 std::mbstowcs(&wstr[0], argv[i], size);
346 wargs.push_back(wstr);
348 for(
auto& s : wargs ) wargv.push_back(s.c_str());
349 return ::Py_Main(argc, (
wchar_t**)&wargv[0]);