25#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
26#include <numpy/arrayobject.h>
37PyObject *PyMethodBase::fModuleBuiltin = NULL;
41PyObject *PyMethodBase::fModulePickle = NULL;
42PyObject *PyMethodBase::fPickleDumps = NULL;
43PyObject *PyMethodBase::fPickleLoads = NULL;
46PyObject *PyMethodBase::fGlobalNS = NULL;
49 PyGILState_STATE m_GILState;
51 PyGILRAII():m_GILState(PyGILState_Ensure()){}
52 ~PyGILRAII(){PyGILState_Release(m_GILState);}
57PyMethodBase::PyMethodBase(
const TString &jobName,
59 const TString &methodTitle,
61 const TString &theOption ):
MethodBase(jobName, methodType, methodTitle, dsi, theOption),
71 Log() << kFATAL <<
"Can't init local namespace" <<
Endl;
79 const TString &weightFile):
MethodBase(methodType, dsi, weightFile),
89 Log() << kFATAL <<
"Can't init local namespace" <<
Endl;
130 if (!pyIsInitialized) {
134 PyGILRAII thePyGILRAII;
136 if (!pyIsInitialized) {
140 fMain = PyImport_AddModule(
"__main__");
142 Log << kFATAL <<
"Can't import __main__" <<
Endl;
148 Log << kFATAL <<
"Can't init global namespace" <<
Endl;
152 #if PY_MAJOR_VERSION < 3
154 PyObject *bName = PyUnicode_FromString(
"__builtin__");
158 Log << kFATAL <<
"Can't import __builtin__" <<
Endl;
163 PyObject *bName = PyUnicode_FromString(
"builtins");
167 Log << kFATAL <<
"Can't import builtins" <<
Endl;
173 fEval = PyDict_GetItemString(mDict,
"eval");
174 fOpen = PyDict_GetItemString(mDict,
"open");
179 PyObject *pName = PyUnicode_FromString(
"pickle");
183 Log << kFATAL <<
"Can't import pickle" <<
Endl;
214 #if PY_MAJOR_VERSION < 3
215 Py_SetProgramName(
const_cast<char*
>(
name.Data()));
217 Py_SetProgramName((
wchar_t *)
name.Data());
238 return std::string(progName, progName +
mystrlen(progName));
248 if (!Py_IsInitialized())
return kFALSE;
269 PyObject *file_arg = Py_BuildValue(
"(ss)", path.Data(),
"wb");
276 Py_DECREF(model_arg);
277 Py_DECREF(model_data);
290 PyObject *file_arg = Py_BuildValue(
"(ss)", path.Data(),
"rb");
301 Py_DECREF(model_arg);
321 Log() << kWARNING <<
"Failed to run python code: " << code <<
Endl;
322 Log() << kWARNING <<
"Python error message:" <<
Endl;
324 Log() << kFATAL << errorMessage <<
Endl;
size_t mystrlen(const char *s)
Class that contains all the data information.
Virtual base Class for all MVA method.
ostringstream derivative to redirect and format output
static int PyIsInitialized()
Check Python interpreter initialization status.
static PyObject * fPickleDumps
PyObject * Eval(TString code)
Evaluate Python code.
static TString Py_GetProgramName()
Get program name from Python interpreter.
static void PyInitialize()
Initialize Python interpreter.
static void Serialize(TString file, PyObject *classifier)
Serialize Python object.
static Int_t UnSerialize(TString file, PyObject **obj)
Unserialize Python object.
static PyObject * fPickleLoads
static void PySetProgramName(TString name)
Set program name for Python interpeter.
static PyObject * fGlobalNS
static PyObject * fModulePickle
static PyObject * fModuleBuiltin
void PyRunString(TString code, TString errorMessage="Failed to run python code", int start=Py_single_input)
Execute Python code from string.
PyMethodBase(const TString &jobName, Types::EMVA methodType, const TString &methodTitle, DataSetInfo &dsi, const TString &theOption="")
static constexpr double s
Abstract ClassifierFactory template that handles arbitrary types.
MsgLogger & Endl(MsgLogger &ml)