CMS 3D CMS Logo

PythonManager.h

Go to the documentation of this file.
00001 //
00002 // Smart reference + singleton to handle Python interpreter
00003 //
00004 //
00005 // Original Author:  Chris D Jones & Benedikt Hegner
00006 //         Created:  Sun Jul 22 11:03:53 CEST 2006
00007 //
00008 
00009 #ifndef FWCore_Python_PythonManager_h
00010 #define FWCore_Python_PythonManager_h
00011 
00012 #include "FWCore/ParameterSet/interface/BoostPython.h"
00013 #include "FWCore/Utilities/interface/Exception.h"
00014 
00015 extern "C" {
00016    //this is the entry point into the libFWCorePython python module
00017    void initlibFWCorePython();
00018    //void initROOT();
00019 }
00020 
00021 namespace {
00022     void
00023     pythonToCppException(const std::string& iType)
00024     {
00025        using namespace boost::python;
00026        PyObject *exc, *val, *trace;
00027        PyErr_Fetch(&exc,&val,&trace);
00028        handle<> hExc(allow_null(exc));
00029        if(hExc) {
00030           object oExc(hExc);
00031        }
00032        handle<> hVal(allow_null(val));
00033        handle<> hTrace(allow_null(trace));
00034        if(hTrace) {
00035           object oTrace(hTrace);
00036        }
00037 
00038        if(hVal) {
00039           object oVal(hVal);
00040           handle<> hStringVal(PyObject_Str(oVal.ptr()));
00041           object stringVal( hStringVal );
00042 
00043           //PyErr_Print();
00044           throw cms::Exception(iType) <<"python encountered the error: "<< PyString_AsString(stringVal.ptr())<<"\n";
00045        } else {
00046           throw cms::Exception(iType)<<" unknown python problem occurred.\n";
00047        }
00048     }
00049 
00050     class PythonManagerHandle;
00051         
00052     class PythonManager {
00053       public:
00054             friend class PythonManagerHandle;
00055             static PythonManagerHandle handle();
00056 
00057       private:
00058              PythonManager();
00059              ~PythonManager() { Py_Finalize(); }
00060              void increment() { ++refCount_; }
00061          void decrement() { --refCount_; if(0==refCount_) delete this; }
00062              unsigned long refCount_;
00063              std::string initCommand_;
00064     };
00065 
00066     class PythonManagerHandle {
00067       public:
00068             ~PythonManagerHandle() { manager_.decrement(); }
00069 
00070             PythonManagerHandle(PythonManager& iM):
00071               manager_(iM) {
00072               manager_.increment();
00073             }
00074 
00075             PythonManagerHandle(const PythonManagerHandle& iRHS) :
00076               manager_(iRHS.manager_) {
00077               manager_.increment();
00078             }
00079                 
00080       private:
00081             const PythonManagerHandle& operator=(const PythonManagerHandle&);
00082             PythonManager& manager_;
00083     };
00084 
00085     PythonManagerHandle PythonManager::handle() {
00086         static PythonManager* s_manager( new PythonManager() );
00087         return PythonManagerHandle( *s_manager);
00088     }
00089 
00090     PythonManager::PythonManager() :
00091         refCount_(0),
00092         initCommand_(
00093             "import sys\n"
00094             "sys.path.append('./')\n"
00095             "import ROOT\n"
00096             "ROOT.gSystem.Load(\"libFWCoreFWLite\")\n"
00097             "ROOT.AutoLibraryLoader.enable()\n"
00098             "import libFWCorePython as edm\n")
00099     {
00100        Py_InitializeEx(0);
00101        using namespace boost::python;
00102 
00103        if(PyImport_AppendInittab("libFWCorePython",initlibFWCorePython)==-1) {
00104          throw cms::Exception("InitializationFailure" )
00105           <<"failed to add libFWCorePython python module to python interpreter";
00106        }
00107        object main_module((
00108                            boost::python::handle<PyObject>(borrowed(PyImport_AddModule("__main__")))));
00109        object main_namespace = main_module.attr("__dict__");
00110        try {
00111            object result((boost::python::handle<>(PyRun_String(initCommand_.c_str(),
00112                                            Py_file_input,
00113                                            main_namespace.ptr(),
00114                                            main_namespace.ptr()))));
00115 
00116        } catch(...  ) {
00117          throw cms::Exception("Configuration") << "test";
00118        }
00119 
00120     }
00121 }
00122 
00123 #endif // FWCore_Python_PythonManager_h

Generated on Tue Jun 9 17:36:32 2009 for CMSSW by  doxygen 1.5.4