Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move PythonFramework to PyDevParameterSet #26353

Merged
merged 1 commit into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion FWCore/PyDevParameterSet/interface/PyBind11ProcessDesc.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class PyBind11ProcessDesc {

Python11ParameterSet theProcessPSet;
pybind11::object theMainModule;
// pybind11::object theMainNamespace;
bool theOwnsInterpreter;
};

#endif
9 changes: 7 additions & 2 deletions FWCore/PyDevParameterSet/src/PyBind11Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@

PYBIND11_MODULE(libFWCorePyDevParameterSet,m)
{

// pybind11::register_exception_translator<cms::Exception>(translatorlibFWCorePythonParameterSet);
pybind11::register_exception_translator([](std::exception_ptr p) {
try {
if (p) std::rethrow_exception(p);
} catch (const cms::Exception &e) {
PyErr_SetString(PyExc_RuntimeError, e.what());
}
});

pybind11::class_<edm::InputTag>(m,"InputTag")
.def(pybind11::init<>())
Expand Down
18 changes: 11 additions & 7 deletions FWCore/PyDevParameterSet/src/PyBind11ProcessDesc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@

PyBind11ProcessDesc::PyBind11ProcessDesc() :
theProcessPSet(),
theMainModule()
theMainModule(),
theOwnsInterpreter(false)
{
pybind11::initialize_interpreter();
}

PyBind11ProcessDesc::PyBind11ProcessDesc(std::string const& config) :
theProcessPSet(),
theMainModule()
theMainModule(),
theOwnsInterpreter(true)
{
pybind11::initialize_interpreter();
edm::python::initializePyBind11Module();
Expand All @@ -29,7 +30,9 @@ PyBind11ProcessDesc::PyBind11ProcessDesc(std::string const& config) :

PyBind11ProcessDesc::PyBind11ProcessDesc(std::string const& config, int argc, char* argv[]) :
theProcessPSet(),
theMainModule()
theMainModule(),
theOwnsInterpreter(true)

{
pybind11::initialize_interpreter();
edm::python::initializePyBind11Module();
Expand Down Expand Up @@ -58,9 +61,10 @@ PyBind11ProcessDesc::PyBind11ProcessDesc(std::string const& config, int argc, ch


PyBind11ProcessDesc::~PyBind11ProcessDesc() {
theMainModule=pybind11::object();
pybind11::finalize_interpreter();

if ( theOwnsInterpreter ) {
theMainModule=pybind11::object();
pybind11::finalize_interpreter();
}
}

void PyBind11ProcessDesc::prepareToRead() {
Expand Down
4 changes: 2 additions & 2 deletions FWCore/PythonFramework/BuildFile.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<use name="FWCore/Framework"/>
<use name="FWCore/PythonParameterSet"/>
<use name="FWCore/PyDevParameterSet"/>
<use name="boost"/>
<use name="boost_python"/>
<use name="py2-pybind11"/>
<export>
<lib name="1"/>
</export>
3 changes: 2 additions & 1 deletion FWCore/PythonFramework/interface/PythonEventProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

// system include files
#include "FWCore/Framework/interface/EventProcessor.h"
#include "FWCore/PyDevParameterSet/interface/PyBind11ProcessDesc.h"

// user include files

Expand All @@ -30,7 +31,7 @@ class PythonEventProcessor
{

public:
PythonEventProcessor(PythonProcessDesc const&);
PythonEventProcessor(PyBind11ProcessDesc const&);
~PythonEventProcessor();
// ---------- const member functions ---------------------
/// Return the number of events this EventProcessor has tried to process
Expand Down
3 changes: 2 additions & 1 deletion FWCore/PythonFramework/python/CmsRun.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import libFWCorePythonFramework as _pf
import libFWCorePythonParameterSet as _pp
import libFWCorePyDevParameterSet as _pp

class CmsRun(object):
def __init__(self,process):
Expand All @@ -8,6 +8,7 @@ def __init__(self,process):
procDesc = _pp.ProcessDesc()
process.fillProcessDesc(procDesc.pset())
self._cppProcessor = _pf.PythonEventProcessor(procDesc)

def run(self):
"""Process all the events
"""
Expand Down
4 changes: 2 additions & 2 deletions FWCore/PythonFramework/src/PythonEventProcessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

// user include files
#include "FWCore/PythonFramework/interface/PythonEventProcessor.h"
#include "FWCore/PythonParameterSet/interface/PythonProcessDesc.h"
#include "FWCore/PyDevParameterSet/interface/PyBind11ProcessDesc.h"

#include "FWCore/Framework/interface/defaultCmsRunServices.h"
#include "FWCore/ParameterSet/interface/ProcessDesc.h"
Expand Down Expand Up @@ -58,7 +58,7 @@ namespace {
//
// constructors and destructor
//
PythonEventProcessor::PythonEventProcessor(PythonProcessDesc const& iDesc)
PythonEventProcessor::PythonEventProcessor(PyBind11ProcessDesc const& iDesc)
: forcePluginSetupFirst_(setupPluginSystem())
,processor_(addDefaultServicesToProcessDesc(iDesc.processDesc()),createJobReport(),edm::serviceregistry::kOverlapIsError)
{
Expand Down
28 changes: 16 additions & 12 deletions FWCore/PythonFramework/src/PythonModule.cc
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#ifndef FWCore_PythonFramework_PythonModule_h
#define FWCore_PythonFramework_PythonModule_h

#include "FWCore/PythonParameterSet/interface/BoostPython.h"
#include "FWCore/PythonParameterSet/interface/PythonProcessDesc.h"

#include "FWCore/PyDevParameterSet/interface/PyBind11ProcessDesc.h"

#include "FWCore/PythonFramework/interface/PythonEventProcessor.h"
#include "FWCore/Utilities/interface/Exception.h"

#include <pybind11/pybind11.h>

// This is to give some special handling to cms::Exceptions thrown
// in C++ code called by python. Only at the very top level do
// we need the exception message returned by the function "what".
Expand All @@ -16,21 +18,23 @@
// improve these messages even more by adding something in the python
// to add module type and label context to the messages being caught
// here. At this point we did not think it worth the time to implement.
namespace {
void translator(cms::Exception const& ex) {
PyErr_SetString(PyExc_RuntimeError, ex.message().c_str());
}
}

BOOST_PYTHON_MODULE(libFWCorePythonFramework)
PYBIND11_MODULE(libFWCorePythonFramework,m)
{
boost::python::register_exception_translator<cms::Exception>(translator);
pybind11::register_exception_translator([](std::exception_ptr p) {
try {
if (p) std::rethrow_exception(p);
} catch (const cms::Exception &e) {
PyErr_SetString(PyExc_RuntimeError, e.what());
}
});

boost::python::class_<PythonEventProcessor, boost::noncopyable>("PythonEventProcessor", boost::python::init<PythonProcessDesc const&>())
pybind11::class_<PythonEventProcessor>(m,"PythonEventProcessor")
.def(pybind11::init<PyBind11ProcessDesc const&>())
.def("run", &PythonEventProcessor::run)
.def("totalEvents", &PythonEventProcessor::totalEvents)
.def("totalEventsPassed", &PythonEventProcessor::totalEventsPassed)
.def("totalEventsFailed", &PythonEventProcessor::totalEventsFailed)
;
.def("totalEventsFailed", &PythonEventProcessor::totalEventsFailed);

}
#endif