Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:04

0001 #ifndef FWCore_PythonFramework_PythonModule_h
0002 #define FWCore_PythonFramework_PythonModule_h
0003 
0004 #include "FWCore/PythonParameterSet/interface/PyBind11ProcessDesc.h"
0005 
0006 #include "FWCore/PythonFramework/interface/PythonEventProcessor.h"
0007 #include "FWCore/Utilities/interface/Exception.h"
0008 
0009 #include <pybind11/pybind11.h>
0010 
0011 // This is to give some special handling to cms::Exceptions thrown
0012 // in C++ code called by python. Only at the very top level do
0013 // we need the exception message returned by the function "what".
0014 // We only need the central message here as this will get converted
0015 // back into a cms::Exception again when control rises back into
0016 // the C++ code.  If necessary it would probably be possible to
0017 // improve these messages even more by adding something in the python
0018 // to add module type and label context to the messages being caught
0019 // here. At this point we did not think it worth the time to implement.
0020 
0021 PYBIND11_MODULE(libFWCorePythonFramework, m) {
0022   pybind11::register_exception_translator([](std::exception_ptr p) {
0023     try {
0024       if (p)
0025         std::rethrow_exception(p);
0026     } catch (const cms::Exception &e) {
0027       PyErr_SetString(PyExc_RuntimeError, e.what());
0028     }
0029   });
0030 
0031   pybind11::class_<PythonEventProcessor>(m, "PythonEventProcessor")
0032       .def(pybind11::init<PyBind11ProcessDesc const &>())
0033       .def("run", &PythonEventProcessor::run)
0034       .def("totalEvents", &PythonEventProcessor::totalEvents)
0035       .def("totalEventsPassed", &PythonEventProcessor::totalEventsPassed)
0036       .def("totalEventsFailed", &PythonEventProcessor::totalEventsFailed);
0037 }
0038 #endif