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 
12 PyBind11ProcessDesc::PyBind11ProcessDesc() : theProcessPSet(), theMainModule(), theOwnsInterpreter(false) {}
13 
16  pybind11::initialize_interpreter();
18  prepareToRead();
19  read(config);
20 }
21 
23  : theProcessPSet(),
24  theMainModule(),
26 
27 {
28  pybind11::initialize_interpreter();
30  prepareToRead();
31  {
32 #if PY_MAJOR_VERSION >= 3
33  typedef std::unique_ptr<wchar_t[], decltype(&PyMem_RawFree)> WArgUPtr;
34  std::vector<WArgUPtr> v_argv;
35  std::vector<wchar_t*> vp_argv;
36  v_argv.reserve(argc);
37  vp_argv.reserve(argc);
38  for (int i = 0; i < argc; i++) {
39  v_argv.emplace_back(Py_DecodeLocale(argv[i], NULL), &PyMem_RawFree);
40  vp_argv.emplace_back(v_argv.back().get());
41  }
42 
43  wchar_t** argvt = vp_argv.data();
44 #else
45  char** argvt = argv;
46 #endif
47 
48  PySys_SetArgv(argc, argvt);
49  }
50  read(config);
51 }
52 
54  if (theOwnsInterpreter) {
56  pybind11::finalize_interpreter();
57  }
58 }
59 
61  // pybind11::scoped_interpreter guard{};
63  theMainModule.attr("processDesc") = this;
64  theMainModule.attr("processPSet") = &theProcessPSet;
65 }
66 
68  try {
69  // if it ends with py, it's a file
70  if (config.substr(config.size() - 3) == ".py") {
71  readFile(config);
72  } else {
73  readString(config);
74  }
75  } catch (pybind11::error_already_set const& e) {
76  edm::pythonToCppException("Configuration", e.what());
77  }
78 }
79 
81  pybind11::eval_file(fileName);
82  std::string command("process.fillProcessDesc(processPSet)");
83  pybind11::exec(command);
84 }
85 
87  std::string command = pyConfig;
88  command += "\nprocess.fillProcessDesc(processPSet)";
89  pybind11::exec(command.c_str());
90 }
91 
92 std::unique_ptr<edm::ParameterSet> PyBind11ProcessDesc::parameterSet() const {
93  return std::make_unique<edm::ParameterSet>(theProcessPSet.pset());
94 }
95 
97  std::ostringstream os;
98  os << theProcessPSet.dump();
99  return os.str();
100 }
101 
102 // For backward compatibility only. Remove when no longer used.
103 std::unique_ptr<edm::ProcessDesc> PyBind11ProcessDesc::processDesc() const {
104  return std::make_unique<edm::ProcessDesc>(parameterSet());
105 }
std::string dump() const
std::string dump() const
std::unique_ptr< edm::ParameterSet > parameterSet() const
void pythonToCppException(const std::string &iType, const std::string &error)
Python11ParameterSet theProcessPSet
edm::ParameterSet & pset()
#define NULL
Definition: scimark2.h:8
std::unique_ptr< edm::ProcessDesc > processDesc() const
Definition: config.py:1
pybind11::object theMainModule
void readFile(std::string const &fileName)
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:36
void readString(std::string const &pyConfig)
list command
Definition: mps_check.py:25