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

convert to unit test in FWCore/PythonFramework to pybind11 #34217

Merged
merged 3 commits into from Jun 23, 2021
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
8 changes: 6 additions & 2 deletions FWCore/PythonFramework/test/BuildFile.xml
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
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