| 338 | } |
| 339 | |
| 340 | int |
| 341 | PythonModule::evalDoubleStringExpression(const char* theExpression, double& current_val) |
| 342 | { |
| 343 | if (theExpression == 0) { |
| 344 | opserr << "OPS_EvalDoubleStringExpression Error: Expression not set\n"; |
| 345 | return -1; |
| 346 | } |
| 347 | |
| 348 | // run the string and get results |
| 349 | PyObject* py_main = PyImport_AddModule("__main__"); |
| 350 | if (py_main == NULL) { |
| 351 | opserr << "OPS_EvalDoubleStringExpression Error: cannot add module __main__\n"; |
| 352 | return -1; |
| 353 | } |
| 354 | PyObject* py_dict = PyModule_GetDict(py_main); |
| 355 | if (py_main == NULL) { |
| 356 | opserr << "OPS_EvalDoubleStringExpression Error: cannot get dict of module __main__\n"; |
| 357 | return -1; |
| 358 | } |
| 359 | PyObject* PyRes = PyRun_String(theExpression, Py_eval_input, py_dict, py_dict); |
| 360 | |
| 361 | if (PyRes == NULL) { |
| 362 | opserr << "OPS_EvalDoubleStringExpression Error: failed to evaluate expression\n"; |
| 363 | return -1; |
| 364 | } |
| 365 | |
| 366 | // get results |
| 367 | if (!(PyLong_Check(PyRes) || PyFloat_Check(PyRes) || PyBool_Check(PyRes))) { |
| 368 | opserr << "OPS_EvalDoubleStringExpression Error: the expression must return a float (or int or bool)\n"; |
| 369 | return -1; |
| 370 | } |
| 371 | current_val = PyFloat_AsDouble(PyRes); |
| 372 | |
| 373 | // done |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | void |
| 378 | PythonModule::resetInput(int cArg) { |
no outgoing calls
no test coverage detected