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 
28 PyBind11ProcessDesc::PyBind11ProcessDesc(std::string const& config) : theProcessPSet(), theInterpreter(true) {
30  prepareToRead();
31  read(config);
32 }
33 
35  : theProcessPSet(),
36  theInterpreter(true)
37 
38 {
40  prepareToRead();
41  {
42 #if PY_MAJOR_VERSION >= 3
43  typedef std::unique_ptr<wchar_t[], decltype(&PyMem_RawFree)> WArgUPtr;
44  std::vector<WArgUPtr> v_argv;
45  std::vector<wchar_t*> vp_argv;
46  v_argv.reserve(argc);
47  vp_argv.reserve(argc);
48  for (int i = 0; i < argc; i++) {
49  v_argv.emplace_back(Py_DecodeLocale(argv[i], nullptr), &PyMem_RawFree);
50  vp_argv.emplace_back(v_argv.back().get());
51  }
52 
53  wchar_t** argvt = vp_argv.data();
54 #else
55  char** argvt = argv;
56 #endif
57 
58  PySys_SetArgv(argc, argvt);
59  }
60  read(config);
61 }
62 
64 
66  // pybind11::scoped_interpreter guard{};
68  theInterpreter.mainModule.attr("processDesc") = this;
69  theInterpreter.mainModule.attr("processPSet") = &theProcessPSet;
70 }
71 
73  try {
74  // if it ends with py, it's a file
75  if (config.substr(config.size() - 3) == ".py") {
77  } else {
79  }
80  } catch (pybind11::error_already_set const& e) {
81  edm::pythonToCppException("Configuration", e.what());
82  }
83 }
84 
86  pybind11::eval_file(fileName);
87  std::string command("process.fillProcessDesc(processPSet)");
88  pybind11::exec(command);
89 }
90 
92  std::string command = pyConfig;
93  command += "\nprocess.fillProcessDesc(processPSet)";
94  pybind11::exec(command.c_str());
95 }
96 
97 std::unique_ptr<edm::ParameterSet> PyBind11ProcessDesc::parameterSet() const {
98  return std::make_unique<edm::ParameterSet>(theProcessPSet.pset());
99 }
100 
102  std::ostringstream os;
103  os << theProcessPSet.dump();
104  return os.str();
105 }
106 
107 // For backward compatibility only. Remove when no longer used.
108 std::unique_ptr<edm::ProcessDesc> PyBind11ProcessDesc::processDesc() const {
109  return std::make_unique<edm::ProcessDesc>(parameterSet());
110 }
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)
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
std::unique_ptr< edm::ProcessDesc > processDesc() const