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 
15  : theProcessPSet(), theMainModule(), theOwnsInterpreter(true) {
16  pybind11::initialize_interpreter();
18  prepareToRead();
19  read(config);
20 }
21 
23  : theProcessPSet(),
24  theMainModule(),
25  theOwnsInterpreter(true)
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") {
72  } else {
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 }
edm::python::initializePyBind11Module
void initializePyBind11Module()
Definition: initializePyBind11Module.cc:14
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
PyBind11ProcessDesc::read
void read(std::string const &config)
Definition: PyBind11ProcessDesc.cc:67
funct::false
false
Definition: Factorize.h:29
edm::pythonToCppException
void pythonToCppException(const std::string &iType, const std::string &error)
Definition: PyBind11Wrapper.cc:6
dir2webdir.argc
argc
Definition: dir2webdir.py:39
MillePedeFileConverter_cfg.fileName
fileName
Definition: MillePedeFileConverter_cfg.py:32
mps_check.command
list command
Definition: mps_check.py:25
PyBind11ProcessDesc::processDesc
std::unique_ptr< edm::ProcessDesc > processDesc() const
Definition: PyBind11ProcessDesc.cc:103
config
Definition: config.py:1
Python11ParameterSet::pset
edm::ParameterSet & pset()
Definition: Python11ParameterSet.h:111
Python11ParameterSet::dump
std::string dump() const
Definition: Python11ParameterSet.h:115
ProcessDesc.h
PyBind11ProcessDesc::theProcessPSet
Python11ParameterSet theProcessPSet
Definition: PyBind11ProcessDesc.h:51
PyBind11ProcessDesc.h
initializePyBind11Module.h
PyBind11ProcessDesc::prepareToRead
void prepareToRead()
Definition: PyBind11ProcessDesc.cc:60
PyBind11ProcessDesc::PyBind11ProcessDesc
PyBind11ProcessDesc()
Definition: PyBind11ProcessDesc.cc:12
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
funct::true
true
Definition: Factorize.h:173
PyBind11ProcessDesc::theOwnsInterpreter
bool theOwnsInterpreter
Definition: PyBind11ProcessDesc.h:53
PyBind11ProcessDesc::~PyBind11ProcessDesc
~PyBind11ProcessDesc()
Definition: PyBind11ProcessDesc.cc:53
PyBind11ProcessDesc::readFile
void readFile(std::string const &fileName)
Definition: PyBind11ProcessDesc.cc:80
PyBind11ProcessDesc::parameterSet
std::unique_ptr< edm::ParameterSet > parameterSet() const
Definition: PyBind11ProcessDesc.cc:92
PyBind11ProcessDesc::dump
std::string dump() const
Definition: PyBind11ProcessDesc.cc:96
PyBind11ProcessDesc::readString
void readString(std::string const &pyConfig)
Definition: PyBind11ProcessDesc.cc:86
NULL
#define NULL
Definition: scimark2.h:8
cms::cuda::device::unique_ptr
std::unique_ptr< T, impl::DeviceDeleter > unique_ptr
Definition: device_unique_ptr.h:33
PyBind11ProcessDesc::theMainModule
pybind11::object theMainModule
Definition: PyBind11ProcessDesc.h:52
PyBind11Wrapper.h
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
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37