23#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
24#include <numpy/arrayobject.h>
33 PyGILState_STATE m_GILState;
36 PyGILRAII() : m_GILState(PyGILState_Ensure()) {}
37 ~PyGILRAII() { PyGILState_Release(m_GILState); }
61 :
MethodBase(jobName, methodType, methodTitle, dsi, theOption),
71 Log() << kFATAL <<
"Can't init local namespace" <<
Endl;
89 Log() << kFATAL <<
"Can't init local namespace" <<
Endl;
130 if (!pyIsInitialized) {
134 TMVA::Internal::PyGILRAII raii;
135 if (!pyIsInitialized) {
139 fMain = PyImport_AddModule(
"__main__");
141 Log << kFATAL <<
"Can't import __main__" <<
Endl;
147 Log << kFATAL <<
"Can't init global namespace" <<
Endl;
151 #if PY_MAJOR_VERSION < 3
153 PyObject *bName = PyUnicode_FromString(
"__builtin__");
157 Log << kFATAL <<
"Can't import __builtin__" <<
Endl;
162 PyObject *bName = PyUnicode_FromString(
"builtins");
166 Log << kFATAL <<
"Can't import builtins" <<
Endl;
172 fEval = PyDict_GetItemString(mDict,
"eval");
173 fOpen = PyDict_GetItemString(mDict,
"open");
178 PyObject *pName = PyUnicode_FromString(
"pickle");
182 Log << kFATAL <<
"Can't import pickle" <<
Endl;
213 #if PY_MAJOR_VERSION < 3
214 Py_SetProgramName(
const_cast<char*
>(
name.Data()));
216 Py_SetProgramName((
wchar_t *)
name.Data());
223size_t mystrlen(
const char* s) {
return strlen(s); }
227size_t mystrlen(
const wchar_t* s) {
return wcslen(s); }
237 return std::string(progName, progName +
mystrlen(progName));
247 if (!Py_IsInitialized())
return kFALSE;
268 PyObject *file_arg = Py_BuildValue(
"(ss)", path.
Data(),
"wb");
275 Py_DECREF(model_arg);
276 Py_DECREF(model_data);
289 PyObject *file_arg = Py_BuildValue(
"(ss)", path.
Data(),
"rb");
300 Py_DECREF(model_arg);
320 Log() << kWARNING <<
"Failed to run python code: " << code <<
Endl;
321 Log() << kWARNING <<
"Python error message:" <<
Endl;
323 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="")
const char * Data() const
create variable transformations
MsgLogger & Endl(MsgLogger &ml)