CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PythonManager.cc
Go to the documentation of this file.
1 #include "PythonManager.h"
3 
4  void
5  pythonToCppException(const std::string& iType)
6  {
7  using namespace boost::python;
8  PyObject *exc, *val, *trace;
9  PyErr_Fetch(&exc,&val,&trace);
10  handle<> hExc(allow_null(exc));
11  if(hExc) {
12  object oExc(hExc);
13  }
14  handle<> hVal(allow_null(val));
15  handle<> hTrace(allow_null(trace));
16  if(hTrace) {
17  object oTrace(hTrace);
18  }
19 
20  if(hVal) {
21  object oVal(hVal);
22  handle<> hStringVal(PyObject_Str(oVal.ptr()));
23  object stringVal( hStringVal );
24 
25  //PyErr_Print();
26  throw cms::Exception(iType) <<"python encountered the error: "<< PyString_AsString(stringVal.ptr())<<"\n";
27  } else {
28  throw cms::Exception(iType)<<" unknown python problem occurred.\n";
29  }
30  }
31 
33  static PythonManager s_manager;
34  return PythonManagerHandle( s_manager);
35  }
36 
38  refCount_(0),
39  initCommand_(
40  "import sys\n"
41  "sys.path.append('./')\n"
42  "import ROOT\n"
43  "ROOT.gSystem.Load(\"libFWCoreFWLite\")\n"
44  "ROOT.AutoLibraryLoader.enable()\n"
45  "import libFWCorePython as edm\n")
46  {
47  Py_InitializeEx(0);
48  using namespace boost::python;
49 
50  if(PyImport_AppendInittab(const_cast<char *>("libFWCorePython"),initlibFWCorePython)==-1) {
51  throw cms::Exception("InitializationFailure" )
52  <<"failed to add libFWCorePython python module to python interpreter";
53  }
54  object main_module((
55  boost::python::handle<PyObject>(borrowed(PyImport_AddModule(const_cast<char *>("__main__"))))));
56  object main_namespace = main_module.attr("__dict__");
57  try {
58  object result((boost::python::handle<>(PyRun_String(initCommand_.c_str(),
59  Py_file_input,
60  main_namespace.ptr(),
61  main_namespace.ptr()))));
62 
63  } catch(... ) {
64  throw cms::Exception("Configuration") << "test";
65  }
66 
67  }
68 
void pythonToCppException(const std::string &iType)
Definition: PythonManager.cc:5
friend class PythonManagerHandle
Definition: PythonManager.h:26
std::string initCommand_
Definition: PythonManager.h:35
tuple result
Definition: query.py:137
double trace(const ROOT::Math::SMatrix< double, N, N > &matrix)
static PythonManagerHandle handle()
void initlibFWCorePython()