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
139143PyRun::~PyRun ()
140144{
145+ Py_XDECREF (globals_);
146+ Py_XDECREF (locals_);
141147}
142148
143149void PyRun::setRunString (string statement)
@@ -162,7 +168,7 @@ string PyRun::getInitString() const
162168
163169void 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
174180void 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)
230236void 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
302308void 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 }
0 commit comments