X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FPlot2d%2FPlot2d_AnalyticalParser.cxx;h=89e3fac0bd785471893bfbe16249e50975d5d465;hb=d0b0c10e5649e0338079df4a53cf95044792d989;hp=78a1ba070c6b9a9c4ce024329e82e579c3e3d43c;hpb=034a705024b224972c148e1e3834c5ee38df184b;p=modules%2Fgui.git diff --git a/src/Plot2d/Plot2d_AnalyticalParser.cxx b/src/Plot2d/Plot2d_AnalyticalParser.cxx old mode 100755 new mode 100644 index 78a1ba070..89e3fac0b --- a/src/Plot2d/Plot2d_AnalyticalParser.cxx +++ b/src/Plot2d/Plot2d_AnalyticalParser.cxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE +// Copyright (C) 2007-2022 CEA/DEN, EDF R&D, OPEN CASCADE // // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS @@ -29,105 +29,111 @@ * =========== PYTHON ============== * ==================================*/ -typedef struct { - PyObject_HEAD - int softspace; - std::string *out; - } PyStdOut; +namespace { + typedef struct { + PyObject_HEAD + int softspace; + std::string *out; + } PyStdOut; -static void -PyStdOut_dealloc(PyStdOut *self) -{ - PyObject_Del(self); -} + static void + PyStdOut_dealloc(PyStdOut *self) + { + PyObject_Del(self); + } -static PyObject * -PyStdOut_write(PyStdOut *self, PyObject *args) -{ - char *c; - int l; - if (!PyArg_ParseTuple(args, "t#:write",&c, &l)) - return NULL; + static PyObject * + PyStdOut_write(PyStdOut *self, PyObject *args) + { + char *c; + if (!PyArg_ParseTuple(args, "s",&c)) + return NULL; - //std::cerr << c ; - *(self->out)=*(self->out)+c; + *(self->out)=*(self->out)+c; - Py_INCREF(Py_None); - return Py_None; -} + Py_INCREF(Py_None); + return Py_None; + } -static PyMethodDef PyStdOut_methods[] = { - {"write", (PyCFunction)PyStdOut_write, METH_VARARGS, - PyDoc_STR("write(string) -> None")}, - {NULL, NULL} /* sentinel */ -}; + static PyMethodDef PyStdOut_methods[] = { + {"write", (PyCFunction)PyStdOut_write, METH_VARARGS, + PyDoc_STR("write(string) -> None")}, + {0, 0, 0, 0} /* sentinel */ + }; -static PyMemberDef PyStdOut_memberlist[] = { - {(char*)"softspace", T_INT, offsetof(PyStdOut, softspace), 0, - (char*)"flag indicating that a space needs to be printed; used by print"}, - {NULL} /* Sentinel */ -}; + static PyMemberDef PyStdOut_memberlist[] = { + {(char*)"softspace", T_INT, offsetof(PyStdOut, softspace), 0, + (char*)"flag indicating that a space needs to be printed; used by print"}, + {0, 0, 0, 0, 0} /* sentinel */ + }; -static PyTypeObject PyStdOut_Type = { - /* The ob_type field must be initialized in the module init function - * to be portable to Windows without using C++. */ - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ - "PyOut", /*tp_name*/ - sizeof(PyStdOut), /*tp_basicsize*/ - 0, /*tp_itemsize*/ - /* methods */ - (destructor)PyStdOut_dealloc, /*tp_dealloc*/ - 0, /*tp_print*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ - 0, /*tp_compare*/ - 0, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash*/ - 0, /*tp_call*/ - 0, /*tp_str*/ - PyObject_GenericGetAttr, /*tp_getattro*/ - /* softspace is writable: we must supply tp_setattro */ - PyObject_GenericSetAttr, /* tp_setattro */ - 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT, /*tp_flags*/ - 0, /*tp_doc*/ - 0, /*tp_traverse*/ - 0, /*tp_clear*/ - 0, /*tp_richcompare*/ - 0, /*tp_weaklistoffset*/ - 0, /*tp_iter*/ - 0, /*tp_iternext*/ - PyStdOut_methods, /*tp_methods*/ - PyStdOut_memberlist, /*tp_members*/ - 0, /*tp_getset*/ - 0, /*tp_base*/ - 0, /*tp_dict*/ - 0, /*tp_descr_get*/ - 0, /*tp_descr_set*/ - 0, /*tp_dictoffset*/ - 0, /*tp_init*/ - 0, /*tp_alloc*/ - 0, /*tp_new*/ - 0, /*tp_free*/ - 0, /*tp_is_gc*/ -}; + static PyTypeObject PyStdOut_Type = { + /* The ob_type field must be initialized in the module init function + * to be portable to Windows without using C++. */ + PyVarObject_HEAD_INIT(NULL, 0) + /* 0, */ /*ob_size*/ + "PyOut", /*tp_name*/ + sizeof(PyStdOut), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + /* methods */ + (destructor)PyStdOut_dealloc, /*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + PyObject_GenericGetAttr, /*tp_getattro*/ + /* softspace is writable: we must supply tp_setattro */ + PyObject_GenericSetAttr, /* tp_setattro */ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT, /*tp_flags*/ + 0, /*tp_doc*/ + 0, /*tp_traverse*/ + 0, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + PyStdOut_methods, /*tp_methods*/ + PyStdOut_memberlist, /*tp_members*/ + 0, /*tp_getset*/ + 0, /*tp_base*/ + 0, /*tp_dict*/ + 0, /*tp_descr_get*/ + 0, /*tp_descr_set*/ + 0, /*tp_dictoffset*/ + 0, /*tp_init*/ + 0, /*tp_alloc*/ + 0, /*tp_new*/ + 0, /*tp_free*/ + 0, /*tp_is_gc*/ + 0, /*tp_bases*/ + 0, /*tp_mro*/ + 0, /*tp_cache*/ + 0, /*tp_subclasses*/ + 0, /*tp_weaklist*/ + 0, /*tp_del*/ + 0, /*tp_version_tag*/ + 0, /*tp_finalize*/ + }; -PyObject * newPyStdOut( std::string& out ) -{ - PyStdOut *self; - self = PyObject_New(PyStdOut, &PyStdOut_Type); - if (self == NULL) - return NULL; - self->softspace = 0; - self->out=&out; - return (PyObject*)self; + PyObject * newPyStdOut( std::string& out ) + { + PyStdOut* self = PyObject_New(PyStdOut, &PyStdOut_Type); + if (self) { + self->softspace = 0; + self->out=&out; + } + return (PyObject*)self; + } } - ////////////////////////END PYTHON/////////////////////////// @@ -176,11 +182,11 @@ int Plot2d_AnalyticalParser::calculate( const QString& theExpr, aPyScript = aPyScript.arg(theExpr); int result = -1; PyGILState_STATE gstate = PyGILState_Ensure(); - PyObject* obj = PyRun_String(qPrintable(aPyScript), Py_file_input, myMainDict, NULL); + PyObject* obj = PyRun_String(qUtf8Printable(aPyScript), Py_file_input, myMainDict, NULL); if(obj == NULL) { PyErr_Print(); - PyGILState_Release(gstate); + PyGILState_Release(gstate); return result; } else { @@ -197,33 +203,32 @@ int Plot2d_AnalyticalParser::calculate( const QString& theExpr, if(PyObject_HasAttrString(myMainMod, "coordCalculator")) { func = PyObject_GetAttrString(myMainMod, "coordCalculator"); } - - PyObject* new_stderr = NULL; if( f_y == NULL || func == NULL ) { fflush(stderr); std::string err_description=""; - new_stderr = newPyStdOut(err_description); + PyObject* new_stderr = newPyStdOut(err_description); + PyObject* old_stderr = PySys_GetObject((char*)"stderr"); + Py_INCREF(old_stderr); PySys_SetObject((char*)"stderr", new_stderr); PyErr_Print(); - PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__")); + PySys_SetObject((char*)"stderr", old_stderr); Py_DECREF(new_stderr); PyGILState_Release(gstate); return result; } - - PyObject* coords; - coords = PyObject_CallFunction(func,(char*)"(d, d, i)", theMin, theMax, theNbStep ); - new_stderr = NULL; - + PyObject* coords = PyObject_CallFunction(func,(char*)"(d, d, i)", theMin, theMax, theNbStep ); + if (coords == NULL){ fflush(stderr); std::string err_description=""; - new_stderr = newPyStdOut(err_description); + PyObject* new_stderr = newPyStdOut(err_description); + PyObject* old_stderr = PySys_GetObject((char*)"stderr"); + Py_INCREF(old_stderr); PySys_SetObject((char*)"stderr", new_stderr); PyErr_Print(); - PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__")); + PySys_SetObject((char*)"stderr", old_stderr); Py_DECREF(new_stderr); PyGILState_Release(gstate); return result; @@ -255,22 +260,21 @@ int Plot2d_AnalyticalParser::calculate( const QString& theExpr, */ void Plot2d_AnalyticalParser::initScript() { myScript.clear(); - myScript += "from math import * \n"; - myScript += "def Y(x): \n"; - myScript += " return "; - myScript += "%1\n"; + myScript += "from math import *\n"; + myScript += "def Y(x):\n"; + myScript += "\treturn %1\n"; - myScript += "def coordCalculator(xmin, xmax, nstep): \n"; - myScript += " coords = [] \n"; - myScript += " xstep = (xmax - xmin) / nstep \n"; - myScript += " n = 0 \n"; - myScript += " while n <= nstep : \n"; - myScript += " x = xmin + n*xstep \n"; - myScript += " try: \n"; - myScript += " y = Y(x) \n"; - myScript += " coords.append([x,y]) \n"; - myScript += " except ValueError, ZeroDivisionError: \n"; - myScript += " pass \n"; - myScript += " n = n+1 \n"; - myScript += " return coords \n"; + myScript += "def coordCalculator(xmin, xmax, nstep):\n"; + myScript += "\tcoords = []\n"; + myScript += "\txstep = (xmax - xmin) / nstep\n"; + myScript += "\tn = 0\n"; + myScript += "\twhile n <= nstep :\n"; + myScript += "\t\tx = xmin + n*xstep\n"; + myScript += "\t\ttry:\n"; + myScript += "\t\t\ty = Y(x)\n"; + myScript += "\t\t\tcoords.append([x,y])\n"; + myScript += "\t\texcept (ValueError, ZeroDivisionError):\n"; + myScript += "\t\t\tpass\n"; + myScript += "\t\tn = n+1\n"; + myScript += "\treturn coords\n"; }