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 namespace cmspython3 {
13  PyBind11ProcessDesc::PyBind11ProcessDesc() : theProcessPSet(), theMainModule(), theOwnsInterpreter(false) {}
14 
16  : theProcessPSet(), theMainModule(), theOwnsInterpreter(true) {
17  pybind11::initialize_interpreter();
19  prepareToRead();
20  read(config);
21  }
22 
24  : theProcessPSet(),
25  theMainModule(),
26  theOwnsInterpreter(true)
27 
28  {
29  pybind11::initialize_interpreter();
31  prepareToRead();
32  {
33 #if PY_MAJOR_VERSION >= 3
34  typedef std::unique_ptr<wchar_t[], decltype(&PyMem_RawFree)> WArgUPtr;
35  std::vector<WArgUPtr> v_argv;
36  std::vector<wchar_t*> vp_argv;
37  v_argv.reserve(argc);
38  vp_argv.reserve(argc);
39  for (int i = 0; i < argc; i++) {
40  v_argv.emplace_back(Py_DecodeLocale(argv[i], nullptr), &PyMem_RawFree);
41  vp_argv.emplace_back(v_argv.back().get());
42  }
43 
44  wchar_t** argvt = vp_argv.data();
45 #else
46  char** argvt = argv;
47 #endif
48 
49  PySys_SetArgv(argc, argvt);
50  }
51  read(config);
52  }
53 
55  if (theOwnsInterpreter) {
57  pybind11::finalize_interpreter();
58  }
59  }
60 
62  // pybind11::scoped_interpreter guard{};
64  theMainModule.attr("processDesc") = this;
65  theMainModule.attr("processPSet") = &theProcessPSet;
66  }
67 
69  try {
70  // if it ends with py, it's a file
71  if (config.substr(config.size() - 3) == ".py") {
73  } else {
75  }
76  } catch (pybind11::error_already_set const& e) {
77  cmspython3::pythonToCppException("Configuration", e.what());
78  }
79  }
80 
82  pybind11::eval_file(fileName);
83  std::string command("process.fillProcessDesc(processPSet)");
84  pybind11::exec(command);
85  }
86 
88  std::string command = pyConfig;
89  command += "\nprocess.fillProcessDesc(processPSet)";
90  pybind11::exec(command.c_str());
91  }
92 
93  std::unique_ptr<edm::ParameterSet> PyBind11ProcessDesc::parameterSet() const {
94  return std::make_unique<edm::ParameterSet>(theProcessPSet.pset());
95  }
96 
98  std::ostringstream os;
99  os << theProcessPSet.dump();
100  return os.str();
101  }
102 
103  // For backward compatibility only. Remove when no longer used.
104  std::unique_ptr<edm::ProcessDesc> PyBind11ProcessDesc::processDesc() const {
105  return std::make_unique<edm::ProcessDesc>(parameterSet());
106  }
107 } // namespace cmspython3
cmsBatch.argv
argv
Definition: cmsBatch.py:279
resolutioncreator_cfi.object
object
Definition: resolutioncreator_cfi.py:4
mps_fire.i
i
Definition: mps_fire.py:428
cmspython3::PyBind11ProcessDesc::readString
void readString(std::string const &pyConfig)
Definition: PyBind11ProcessDesc.cc:87
funct::false
false
Definition: Factorize.h:29
dir2webdir.argc
argc
Definition: dir2webdir.py:39
PyBind11ProcessDesc.h
cmspython3::PyBind11ProcessDesc::theOwnsInterpreter
bool theOwnsInterpreter
Definition: PyBind11ProcessDesc.h:55
cmspython3::PyBind11ProcessDesc::read
void read(std::string const &config)
Definition: PyBind11ProcessDesc.cc:68
cmspython3::PyBind11ProcessDesc::processDesc
std::unique_ptr< edm::ProcessDesc > processDesc() const
Definition: PyBind11ProcessDesc.cc:104
MillePedeFileConverter_cfg.fileName
fileName
Definition: MillePedeFileConverter_cfg.py:32
mps_check.command
list command
Definition: mps_check.py:25
cmspython3::Python11ParameterSet::dump
std::string dump() const
Definition: Python11ParameterSet.h:116
cmspython3::PyBind11ProcessDesc::theProcessPSet
Python11ParameterSet theProcessPSet
Definition: PyBind11ProcessDesc.h:53
config
Definition: config.py:1
cmspython3
Definition: PyBind11ProcessDesc.h:16
initializePyBind11Module.h
ProcessDesc.h
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
cmspython3::PyBind11ProcessDesc::parameterSet
std::unique_ptr< edm::ParameterSet > parameterSet() const
Definition: PyBind11ProcessDesc.cc:93
funct::true
true
Definition: Factorize.h:173
edm::python3::initializePyBind11Module
void initializePyBind11Module()
Definition: initializePyBind11Module.cc:14
cmspython3::PyBind11ProcessDesc::PyBind11ProcessDesc
PyBind11ProcessDesc()
Definition: PyBind11ProcessDesc.cc:13
cmspython3::pythonToCppException
void pythonToCppException(const std::string &iType, const std::string &error)
Definition: PyBind11Wrapper.cc:6
cmspython3::PyBind11ProcessDesc::theMainModule
pybind11::object theMainModule
Definition: PyBind11ProcessDesc.h:54
PyBind11Wrapper.h
cmspython3::PyBind11ProcessDesc::~PyBind11ProcessDesc
~PyBind11ProcessDesc()
Definition: PyBind11ProcessDesc.cc:54
cms::cuda::device::unique_ptr
std::unique_ptr< T, impl::DeviceDeleter > unique_ptr
Definition: device_unique_ptr.h:33
cmspython3::PyBind11ProcessDesc::prepareToRead
void prepareToRead()
Definition: PyBind11ProcessDesc.cc:61
cmspython3::PyBind11ProcessDesc::dump
std::string dump() const
Definition: PyBind11ProcessDesc.cc:97
cond::persistency::import
cond::Hash import(Session &source, const cond::Hash &sourcePayloadId, const std::string &inputTypeName, const void *inputPtr, Session &destination)
Definition: CondDBImport.cc:39
ParameterSet.h
cmspython3::PyBind11ProcessDesc::readFile
void readFile(std::string const &fileName)
Definition: PyBind11ProcessDesc.cc:81
cmspython3::Python11ParameterSet::pset
edm::ParameterSet & pset()
Definition: Python11ParameterSet.h:112
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37