CMS 3D CMS Logo

PyBind11ProcessDesc.cc
Go to the documentation of this file.
6 #include <pybind11/embed.h>
7 #include <pybind11/pybind11.h>
8 
9 #include <sstream>
10 #include <iostream>
11 
13  : mainModule(), ownsInterpreter_(ownsInterpreter) {
14  if (ownsInterpreter_) {
15  pybind11::initialize_interpreter();
16  }
17 }
18 
20  if (ownsInterpreter_) {
22  pybind11::finalize_interpreter();
23  }
24 }
25 
26 PyBind11ProcessDesc::PyBind11ProcessDesc() : theProcessPSet(), theInterpreter(false) {}
27 
29  : theProcessPSet(), theInterpreter(true) {
31  prepareToRead();
32  read(config, isFile);
33 }
34 
35 PyBind11ProcessDesc::PyBind11ProcessDesc(std::string const& config, bool isFile, const std::vector<std::string>& args)
36  : theProcessPSet(), theInterpreter(true) {
38  prepareToRead();
39  {
40  typedef std::unique_ptr<wchar_t[], decltype(&PyMem_RawFree)> WArgUPtr;
41  std::vector<WArgUPtr> v_argv;
42  std::vector<wchar_t*> vp_argv;
43  v_argv.reserve(args.size());
44  vp_argv.reserve(args.size());
45  for (size_t i = 0; i < args.size(); i++) {
46  v_argv.emplace_back(Py_DecodeLocale(args[i].c_str(), nullptr), &PyMem_RawFree);
47  vp_argv.emplace_back(v_argv.back().get());
48  }
49 
50  wchar_t** argvt = vp_argv.data();
51 
52  PySys_SetArgv(args.size(), argvt);
53  }
54  read(config, isFile);
55 }
56 
58 
60  // pybind11::scoped_interpreter guard{};
62  theInterpreter.mainModule.attr("processDesc") = this;
63  theInterpreter.mainModule.attr("processPSet") = &theProcessPSet;
64 }
65 
67  try {
68  if (isFile)
70  else
72  } catch (pybind11::error_already_set const& e) {
73  edm::pythonToCppException("Configuration", e.what());
74  }
75 }
76 
78  pybind11::eval_file(fileName);
79  std::string command("process.fillProcessDesc(processPSet)");
80  pybind11::exec(command);
81 }
82 
84  std::string command = pyConfig;
85  command += "\nprocess.fillProcessDesc(processPSet)";
86  pybind11::exec(command.c_str());
87 }
88 
89 std::unique_ptr<edm::ParameterSet> PyBind11ProcessDesc::parameterSet() const {
90  return std::make_unique<edm::ParameterSet>(theProcessPSet.pset());
91 }
92 
94  std::ostringstream os;
95  os << theProcessPSet.dump();
96  return os.str();
97 }
98 
99 // For backward compatibility only. Remove when no longer used.
100 std::unique_ptr<edm::ProcessDesc> PyBind11ProcessDesc::processDesc() const {
101  return std::make_unique<edm::ProcessDesc>(parameterSet());
102 }
void pythonToCppException(const std::string &iType, const std::string &error)
Python11ParameterSet theProcessPSet
std::string dump() const
std::string dump() const
edm::ParameterSet & pset()
std::unique_ptr< edm::ParameterSet > parameterSet() const
Definition: config.py:1
PyBind11InterpreterSentry theInterpreter
void readFile(std::string const &fileName)
PyBind11InterpreterSentry(bool ownsInterpreter)
void read(std::string const &config, bool isFile)
cond::Hash import(Session &source, const cond::Hash &sourcePayloadId, const std::string &inputTypeName, const void *inputPtr, Session &destination)
Definition: CondDBImport.cc:39
void readString(std::string const &pyConfig)
list command
Definition: mps_check.py:25
def isFile(path)
Definition: eostools.py:274
std::unique_ptr< edm::ProcessDesc > processDesc() const