CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/FWCore/Python/src/PythonManager.cc

Go to the documentation of this file.
00001 #include "PythonManager.h"
00002 #include "FWCore/Utilities/interface/Exception.h"
00003 
00004     void
00005     pythonToCppException(const std::string& iType)
00006     {
00007        using namespace boost::python;
00008        PyObject *exc, *val, *trace;
00009        PyErr_Fetch(&exc,&val,&trace);
00010        handle<> hExc(allow_null(exc));
00011        if(hExc) {
00012           object oExc(hExc);
00013        }
00014        handle<> hVal(allow_null(val));
00015        handle<> hTrace(allow_null(trace));
00016        if(hTrace) {
00017           object oTrace(hTrace);
00018        }
00019 
00020        if(hVal) {
00021           object oVal(hVal);
00022           handle<> hStringVal(PyObject_Str(oVal.ptr()));
00023           object stringVal( hStringVal );
00024 
00025           //PyErr_Print();
00026           throw cms::Exception(iType) <<"python encountered the error: "<< PyString_AsString(stringVal.ptr())<<"\n";
00027        } else {
00028           throw cms::Exception(iType)<<" unknown python problem occurred.\n";
00029        }
00030     }
00031 
00032     PythonManagerHandle PythonManager::handle() {
00033         static PythonManager s_manager;
00034         return PythonManagerHandle( s_manager);
00035     }
00036 
00037     PythonManager::PythonManager() :
00038         refCount_(0),
00039         initCommand_(
00040             "import sys\n"
00041             "sys.path.append('./')\n"
00042             "import ROOT\n"
00043             "ROOT.gSystem.Load(\"libFWCoreFWLite\")\n"
00044             "ROOT.AutoLibraryLoader.enable()\n"
00045             "import libFWCorePython as edm\n")
00046     {
00047        Py_InitializeEx(0);
00048        using namespace boost::python;
00049 
00050        if(PyImport_AppendInittab(const_cast<char *>("libFWCorePython"),initlibFWCorePython)==-1) {
00051          throw cms::Exception("InitializationFailure" )
00052           <<"failed to add libFWCorePython python module to python interpreter";
00053        }
00054        object main_module((
00055                            boost::python::handle<PyObject>(borrowed(PyImport_AddModule(const_cast<char *>("__main__"))))));
00056        object main_namespace = main_module.attr("__dict__");
00057        try {
00058            object result((boost::python::handle<>(PyRun_String(initCommand_.c_str(),
00059                                            Py_file_input,
00060                                            main_namespace.ptr(),
00061                                            main_namespace.ptr()))));
00062 
00063        } catch(...  ) {
00064          throw cms::Exception("Configuration") << "test";
00065        }
00066 
00067     }
00068