Belle II Software  release-08-01-10
RegisterPythonModule.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include <boost/python.hpp>
10 
11 #include <framework/utilities/RegisterPythonModule.h>
12 #include <framework/logging/Logger.h>
13 
14 #include <string>
15 
16 using namespace Belle2;
17 
18 BoostPythonModuleProxy::BoostPythonModuleProxy(const char* name, PyObject * (*initFunc)(), bool auto_import)
19 {
20  if (PyImport_AppendInittab(const_cast<char*>(name), initFunc) == -1) {
21  B2FATAL("REGISTER_PYTHON_MODULE(" << name << ") failed.");
22  }
23 
24  if (auto_import) {
25  if (!Py_IsInitialized()) {
26  B2FATAL("REGISTER_PYTHON_MODULE_AUTOIMPORT(" << name << "): Python is not initialized!");
27  }
28 
29  std::string importString = "import ";
30  importString += name;
31  if (PyRun_SimpleString(importString.c_str()) == -1) {
32  B2FATAL("'import " << name << "' failed.");
33  }
34  }
35 }
BoostPythonModuleProxy(const char *name, PyObject *(*initFunc)(), bool auto_import=false)
Don't construct this object yourself, use the REGISTER_PYTHON_MODULE macro instead.
Abstract base class for different kinds of events.