Skip to content

Commit

Permalink
Merge pull request #34217 from davidlange6/dl210622pb
Browse files Browse the repository at this point in the history
convert to unit test in FWCore/PythonFramework to pybind11
  • Loading branch information
cmsbuild authored Jun 23, 2021
2 parents 5aa4fde + 264aa58 commit 2f32ccf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
8 changes: 6 additions & 2 deletions FWCore/PythonFramework/test/BuildFile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<flags EDM_PLUGIN="1"/>
<use name="FWCore/ParameterSet"/>
<use name="FWCore/Framework"/>
<use name="boost"/>
<use name="boost_python"/>
<use name="py3-pybind11"/>
<ifrelease name="_DEVEL_|_PY3_">
<use name="python3"/>
<else/>
<use name="python"/>
</ifrelease>
</library>
16 changes: 5 additions & 11 deletions FWCore/PythonFramework/test/PythonTestProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
#include "FWCore/Utilities/interface/InputTag.h"
#include "FWCore/Utilities/interface/EDGetToken.h"
#include "FWCore/Utilities/interface/EDPutToken.h"

#include "DataFormats/TestObjects/interface/ToyProducts.h"
#include <pybind11/pybind11.h>

#include <boost/python.hpp>
#include <iostream>

namespace edmtest {
Expand All @@ -22,29 +21,24 @@ namespace edmtest {
edm::EDGetTokenT<IntProduct> get_;
edm::EDPutTokenT<int> put_;
int value_;
boost::python::list outputList_;
pybind11::list outputList_;
};

PythonTestProducer::PythonTestProducer(edm::ParameterSet const& iPS)
: get_(consumes<IntProduct>(iPS.getParameter<edm::InputTag>("source"))) {
using namespace boost::python;
object main_module{
boost::python::handle<>(boost::python::borrowed(PyImport_AddModule(const_cast<char*>("__main__"))))};
pybind11::module main_module = pybind11::module::import("__main__");
auto main_namespace = main_module.attr("__dict__");

//NOTE attempts to hold the object directly and read it in `produce` lead to segmentation faults
value_ = extract<int>(main_namespace[iPS.getParameter<std::string>("inputVariable")]);

outputList_ = extract<list>(main_namespace[iPS.getParameter<std::string>("outputListVariable")]);
value_ = main_namespace[(iPS.getParameter<std::string>("inputVariable")).c_str()].cast<int>();
outputList_ = main_namespace[(iPS.getParameter<std::string>("outputListVariable")).c_str()].cast<pybind11::list>();

put_ = produces<int>();

usesResource("python");
}

void PythonTestProducer::produce(edm::Event& iEvent, edm::EventSetup const&) {
using namespace boost::python;

edm::Handle<IntProduct> h;
iEvent.getByToken(get_, h);

Expand Down

0 comments on commit 2f32ccf

Please sign in to comment.