Skip to content

Commit a2d6a38

Browse files
author
Dilawar Singh
committed
Use old PyRun code. Pybind11 does not have subterpreter support.
1 parent 2587c0d commit a2d6a38

File tree

4 files changed

+69
-53
lines changed

4 files changed

+69
-53
lines changed

pybind11/PyRun.cpp

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@
66
// Created: Sat Oct 11 14:47:22 2014 (+0530)
77

88
#include "Python.h"
9-
10-
#include "../external/pybind11/include/pybind11/pybind11.h"
11-
#include "../external/pybind11/include/pybind11/eval.h"
12-
13-
namespace py = pybind11;
14-
using namespace py::literals;
15-
169
#include "../basecode/header.h"
1710
#include "PyRun.h"
1811

@@ -125,19 +118,32 @@ PyRun::PyRun()
125118
: mode_(0),
126119
initstr_(""),
127120
runstr_(""),
121+
globals_(0),
122+
locals_(0),
128123
runcompiled_(0),
129124
initcompiled_(0),
130125
inputvar_("input_"),
131126
outputvar_("output")
132127
{
133-
py::float_ value(0.0);
134-
if (PyDict_SetItemString(l_.ptr(), inputvar_.c_str(), value.ptr())) {
128+
locals_ = PyDict_New();
129+
if (!locals_) {
130+
cerr << "Could not initialize locals dict" << endl;
131+
return;
132+
}
133+
PyObject *value = PyFloat_FromDouble(0.0);
134+
if (!value && PyErr_Occurred()) {
135+
PyErr_Print();
136+
return;
137+
}
138+
if (PyDict_SetItemString(locals_, inputvar_.c_str(), value)) {
135139
PyErr_Print();
136140
}
137141
}
138142

139143
PyRun::~PyRun()
140144
{
145+
Py_XDECREF(globals_);
146+
Py_XDECREF(locals_);
141147
}
142148

143149
void PyRun::setRunString(string statement)
@@ -162,7 +168,7 @@ string PyRun::getInitString() const
162168

163169
void PyRun::setInputVar(string name)
164170
{
165-
PyDict_DelItemString(l_.ptr(), inputvar_.c_str());
171+
PyDict_DelItemString(locals_, inputvar_.c_str());
166172
inputvar_ = name;
167173
}
168174

@@ -173,7 +179,7 @@ string PyRun::getInputVar() const
173179

174180
void PyRun::setOutputVar(string name)
175181
{
176-
PyDict_DelItemString(l_.ptr(), outputvar_.c_str());
182+
PyDict_DelItemString(locals_, outputvar_.c_str());
177183
outputvar_ = name;
178184
}
179185

@@ -201,22 +207,22 @@ void PyRun::trigger(const Eref &e, double input)
201207
return;
202208
}
203209

204-
PyObject *value = PyDict_GetItemString(l_.ptr(), inputvar_.c_str());
210+
PyObject *value = PyDict_GetItemString(locals_, inputvar_.c_str());
205211
if (value) {
206212
Py_DECREF(value);
207213
}
208214
value = PyFloat_FromDouble(input);
209215
if (!value && PyErr_Occurred()) {
210216
PyErr_Print();
211217
}
212-
if (PyDict_SetItemString(l_.ptr(), inputvar_.c_str(), value)) {
218+
if (PyDict_SetItemString(locals_, inputvar_.c_str(), value)) {
213219
PyErr_Print();
214220
}
215-
PyEval_EvalCode(runcompiled_, g_.ptr(), l_.ptr());
221+
PyEval_EvalCode(runcompiled_, globals_, locals_);
216222
if (PyErr_Occurred()) {
217223
PyErr_Print();
218224
}
219-
value = PyDict_GetItemString(l_.ptr(), outputvar_.c_str());
225+
value = PyDict_GetItemString(locals_, outputvar_.c_str());
220226
if (value) {
221227
double output = PyFloat_AsDouble(value);
222228
if (PyErr_Occurred()) {
@@ -230,7 +236,7 @@ void PyRun::trigger(const Eref &e, double input)
230236
void PyRun::run(const Eref &e, string statement)
231237
{
232238
PyRun_SimpleString(statement.c_str());
233-
PyObject *value = PyDict_GetItemString(l_.ptr(), outputvar_.c_str());
239+
PyObject *value = PyDict_GetItemString(locals_, outputvar_.c_str());
234240
if (value) {
235241
double output = PyFloat_AsDouble(value);
236242
if (PyErr_Occurred())
@@ -245,19 +251,19 @@ void PyRun::process(const Eref &e, ProcPtr p)
245251
// Make sure the get the GIL. Ksolve/Gsolve can be multithreaded.
246252
PyGILState_STATE gstate = PyGILState_Ensure();
247253

248-
// PyRun_String(runstr_.c_str(), 0, g_.ptr(), l_.ptr());
254+
// PyRun_String(runstr_.c_str(), 0, globals_, locals_);
249255
// PyRun_SimpleString(runstr_.c_str());
250256
if (!runcompiled_ || mode_ == 2) {
251257
return;
252258
}
253259

254-
PyEval_EvalCode(runcompiled_, g_.ptr(), l_.ptr());
260+
PyEval_EvalCode(runcompiled_, globals_, locals_);
255261
if (PyErr_Occurred()) {
256262
PyErr_Print();
257263
return;
258264
}
259265

260-
PyObject *value = PyDict_GetItemString(l_.ptr(), outputvar_.c_str());
266+
PyObject *value = PyDict_GetItemString(locals_, outputvar_.c_str());
261267
if (value) {
262268
double output = PyFloat_AsDouble(value);
263269
if (PyErr_Occurred()) {
@@ -301,16 +307,25 @@ void handleError(bool syntax)
301307

302308
void PyRun::reinit(const Eref &e, ProcPtr p)
303309
{
304-
g_ = py::module::import("__main__").attr("__dict__");
305-
// l_.ptr() = l_.ptr();
306-
310+
PyObject *main_module;
311+
if (globals_ == NULL) {
312+
main_module = PyImport_AddModule("__main__");
313+
globals_ = PyModule_GetDict(main_module);
314+
Py_XINCREF(globals_);
315+
}
316+
if (locals_ == NULL) {
317+
locals_ = PyDict_New();
318+
if (!locals_) {
319+
cerr << "Could not initialize locals dict" << endl;
320+
}
321+
}
307322
initcompiled_ = (PYCODEOBJECT *)Py_CompileString(
308323
initstr_.c_str(), get_program_name().c_str(), Py_file_input);
309324
if (!initcompiled_) {
310325
cerr << "Error compiling initString" << endl;
311326
handleError(true);
312327
} else {
313-
PyEval_EvalCode(initcompiled_, g_.ptr(), l_.ptr());
328+
PyEval_EvalCode(initcompiled_, globals_, locals_);
314329
if (PyErr_Occurred()) {
315330
PyErr_Print();
316331
}
@@ -324,7 +339,7 @@ void PyRun::reinit(const Eref &e, ProcPtr p)
324339
cerr << "Error compiling runString" << endl;
325340
handleError(true);
326341
} else {
327-
PyEval_EvalCode(runcompiled_, g_.ptr(), l_.ptr());
342+
PyEval_EvalCode(runcompiled_, globals_, locals_);
328343
if (PyErr_Occurred()) {
329344
PyErr_Print();
330345
}

pybind11/PyRun.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define _PYCALL_H
88

99
#include <climits>
10+
1011
#if PY_MAJOR_VERSION >= 3
1112
#define PYCODEOBJECT PyObject
1213

@@ -76,10 +77,8 @@ class PyRun
7677
int mode_; // flag to decide when to run the Python string
7778
string initstr_; // statement str for running at reinit
7879
string runstr_; // statement str for running in each process call
79-
80-
py::dict g_;
81-
py::dict l_;
82-
80+
PyObject * globals_; // global env dict
81+
PyObject * locals_; // local env dict
8382
PYCODEOBJECT * runcompiled_; // compiled form of procstr_
8483
PYCODEOBJECT * initcompiled_; // coimpiled form of initstr_
8584
string inputvar_; // identifier for input variable.

pybind11/pymoose.cpp

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -166,36 +166,18 @@ py::object getFieldGeneric(const ObjId &oid, const string &fieldName)
166166
/* ----------------------------------------------------------------------------*/
167167
PYBIND11_MODULE(_moose, m)
168168
{
169+
// py::options options;
170+
// options.disable_function_signatures();
171+
169172
m.doc() = R"moosedoc(
170173
pyMOOSE: Multiscale Object-Oriented Simulation Environment.
171174
)moosedoc";
172175

173176
initModule(m);
174177

175-
// This is a wrapper around Field::get and LookupField::get which may
176-
// return simple values or vector. Python scripts expect LookupField to
177-
// return either list of dict which can be queried by key and index. This
178-
// class bind both __getitem__ to the getter function call.
179-
// Note that both a.isA["Compartment"] and a.isA("Compartment") are valid
180-
// now.
181-
py::class_<__Finfo__>(m, "_Finfo", py::dynamic_attr())
182-
.def(py::init<const ObjId &, const Finfo *, const char *>())
183-
.def_property_readonly("type", &__Finfo__::type)
184-
.def_property_readonly("vec", [](const __Finfo__ &finfo) {
185-
return MooseVec(finfo.getObjId());
186-
})
187-
.def_property("num", &__Finfo__::getNumField,
188-
&__Finfo__::setNumField) // Only for FieldElementFinfos
189-
.def("__call__", &__Finfo__::operator())
190-
.def("__call__", &__Finfo__::operator())
191-
.def("__getitem__", &__Finfo__::getItem)
192-
.def("__setitem__", &__Finfo__::setItem)
193-
.def("__len__", &__Finfo__::getNumField);
194-
195-
#if 1
196178
// A thin wrapper around Id from ../basecode/Id.h . Usually this is shows
197179
// at moose.vec.
198-
py::class_<Id>(m, "_Id")
180+
py::class_<Id>(m, "mid")
199181
.def(py::init<>())
200182
.def(py::init<unsigned int>())
201183
.def(py::init<const string &>())
@@ -232,13 +214,33 @@ PYBIND11_MODULE(_moose, m)
232214
[](const Id &id, const string &key, const py::object &val) {
233215
return setFieldGeneric(ObjId(id), key, val);
234216
});
235-
#endif
217+
218+
219+
// This is a wrapper around Field::get and LookupField::get which may
220+
// return simple values or vector. Python scripts expect LookupField to
221+
// return either list of dict which can be queried by key and index. This
222+
// class bind both __getitem__ to the getter function call.
223+
// Note that both a.isA["Compartment"] and a.isA("Compartment") are valid
224+
// now.
225+
py::class_<__Finfo__>(m, "mfinfo", py::dynamic_attr())
226+
.def(py::init<const ObjId &, const Finfo *, const char *>())
227+
.def_property_readonly("type", &__Finfo__::type)
228+
.def_property_readonly("vec", [](const __Finfo__ &finfo) {
229+
return MooseVec(finfo.getObjId());
230+
})
231+
.def_property("num", &__Finfo__::getNumField,
232+
&__Finfo__::setNumField) // Only for FieldElementFinfos
233+
.def("__call__", &__Finfo__::operator())
234+
.def("__call__", &__Finfo__::operator())
235+
.def("__getitem__", &__Finfo__::getItem)
236+
.def("__setitem__", &__Finfo__::setItem)
237+
.def("__len__", &__Finfo__::getNumField);
236238

237239
/**
238240
* @name ObjId. It is a base of all other moose objects.
239241
* @{ */
240242
/** @} */
241-
py::class_<ObjId>(m, "_ObjId")
243+
py::class_<ObjId>(m, "melement")
242244
.def(py::init<>())
243245
.def(py::init<Id>())
244246
.def(py::init<Id, unsigned int>())

python/moose/moose.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
__class_types__ = {}
2121

2222

23-
class PyObjId(_moose._ObjId):
23+
class PyObjId(_moose.melement):
2424

2525
__class__ = 'Unknown'
2626

0 commit comments

Comments
 (0)