From 92e92cf782eb143a7c6a78303b6dea78c890995b Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Tue, 24 Aug 2021 16:58:05 +0200 Subject: [PATCH] AutoGIL.hxx has been factorized to KERNEL PythonCppUtils.hxx --- src/evalyfx/YACSEvalSession.cxx | 38 ++++++++++++------------- src/evalyfx/YACSEvalSessionInternal.cxx | 6 ++-- src/evalyfx/YACSEvalYFXPattern.cxx | 14 ++++----- src/py2yacs/py2yacs.cxx | 6 ++-- src/runtime/CMakeLists.txt | 1 - src/runtime/DistributedPythonNode.cxx | 2 +- src/runtime/PyStdout.cxx | 2 +- src/runtime/PythonNode.cxx | 2 +- src/ydfx_gui/YDFXGUISeqInit.cxx | 22 ++------------ 9 files changed, 37 insertions(+), 56 deletions(-) diff --git a/src/evalyfx/YACSEvalSession.cxx b/src/evalyfx/YACSEvalSession.cxx index 0d2f1c828..819e4aba4 100644 --- a/src/evalyfx/YACSEvalSession.cxx +++ b/src/evalyfx/YACSEvalSession.cxx @@ -21,7 +21,7 @@ #include "YACSEvalSession.hxx" #include "YACSEvalSessionInternal.hxx" -#include "AutoGIL.hxx" +#include "PythonCppUtils.hxx" #include "Exception.hxx" #include @@ -37,18 +37,18 @@ const char YACSEvalSession::NSPORT_VAR_NAME[]="NSPORT"; YACSEvalSession::YACSEvalSession():_isAttached(false),_isLaunched(false),_isForcedPyThreadSaved(false),_port(-1),_salomeInstanceModule(0),_salomeInstance(0),_internal(new YACSEvalSessionInternal) { - YACS::ENGINE::AutoGIL gal; + AutoGIL gal; _salomeInstanceModule=PyImport_ImportModule(const_cast("salome_instance")); } YACSEvalSession::~YACSEvalSession() { delete _internal; - YACS::ENGINE::AutoGIL gal; + AutoGIL gal; if(isLaunched() && !isAttached()) { - YACS::ENGINE::AutoPyRef terminateSession(PyObject_GetAttrString(_salomeInstance,const_cast("stop")));//new - YACS::ENGINE::AutoPyRef res(PyObject_CallObject(terminateSession,0)); + AutoPyRef terminateSession(PyObject_GetAttrString(_salomeInstance,const_cast("stop")));//new + AutoPyRef res(PyObject_CallObject(terminateSession,0)); } Py_XDECREF(_salomeInstance); Py_XDECREF(_salomeInstanceModule); @@ -58,16 +58,16 @@ void YACSEvalSession::launch() { if(isLaunched()) return ; - YACS::ENGINE::AutoGIL gal; + AutoGIL gal; PyObject *salomeInstance(PyObject_GetAttrString(_salomeInstanceModule,const_cast("SalomeInstance")));//new PyObject *startMeth(PyObject_GetAttrString(salomeInstance,const_cast("start"))); Py_XDECREF(salomeInstance); - YACS::ENGINE::AutoPyRef myArgs(PyTuple_New(0));//new - YACS::ENGINE::AutoPyRef myKWArgs(PyDict_New());//new + AutoPyRef myArgs(PyTuple_New(0));//new + AutoPyRef myKWArgs(PyDict_New());//new PyDict_SetItemString(myKWArgs,"shutdown_servers",Py_True);//Py_True ref not stolen _salomeInstance=PyObject_Call(startMeth,myArgs,myKWArgs);//new - YACS::ENGINE::AutoPyRef getPortMeth(PyObject_GetAttrString(_salomeInstance,const_cast("get_port")));//new - YACS::ENGINE::AutoPyRef portPy(PyObject_CallObject(getPortMeth,0));//new + AutoPyRef getPortMeth(PyObject_GetAttrString(_salomeInstance,const_cast("get_port")));//new + AutoPyRef portPy(PyObject_CallObject(getPortMeth,0));//new _port=PyLong_AsLong(portPy); // int dummy; @@ -79,7 +79,7 @@ void YACSEvalSession::launchUsingCurrentSession() { if(isLaunched()) return ; - YACS::ENGINE::AutoGIL gal; + AutoGIL gal; _corbaConfigFileName=GetConfigAndPort(_port); _isAttached=true; _isLaunched=true; } @@ -110,23 +110,23 @@ std::string YACSEvalSession::getCorbaConfigFileName() const std::string YACSEvalSession::GetPathToAdd() { std::string ret; - YACS::ENGINE::AutoGIL gal; - YACS::ENGINE::AutoPyRef osPy(PyImport_ImportModule(const_cast("os")));//new + AutoGIL gal; + AutoPyRef osPy(PyImport_ImportModule(const_cast("os")));//new PyObject *kernelRootDir(0);// os.environ["KERNEL_ROOT_DIR"] { - YACS::ENGINE::AutoPyRef environPy(PyObject_GetAttrString(osPy,const_cast("environ")));//new - YACS::ENGINE::AutoPyRef kernelRootDirStr(PyBytes_FromString(const_cast(KERNEL_ROOT_DIR)));//new + AutoPyRef environPy(PyObject_GetAttrString(osPy,const_cast("environ")));//new + AutoPyRef kernelRootDirStr(PyBytes_FromString(const_cast(KERNEL_ROOT_DIR)));//new kernelRootDir=PyObject_GetItem(environPy,kernelRootDirStr);//new } { - YACS::ENGINE::AutoPyRef pathPy(PyObject_GetAttrString(osPy,const_cast("path")));//new - YACS::ENGINE::AutoPyRef joinPy(PyObject_GetAttrString(pathPy,const_cast("join")));//new - YACS::ENGINE::AutoPyRef myArgs(PyTuple_New(4)); + AutoPyRef pathPy(PyObject_GetAttrString(osPy,const_cast("path")));//new + AutoPyRef joinPy(PyObject_GetAttrString(pathPy,const_cast("join")));//new + AutoPyRef myArgs(PyTuple_New(4)); Py_XINCREF(kernelRootDir); PyTuple_SetItem(myArgs,0,kernelRootDir); PyTuple_SetItem(myArgs,1,PyBytes_FromString(const_cast("bin"))); PyTuple_SetItem(myArgs,2,PyBytes_FromString(const_cast("salome"))); PyTuple_SetItem(myArgs,3,PyBytes_FromString(const_cast("appliskel"))); - YACS::ENGINE::AutoPyRef res(PyObject_CallObject(joinPy,myArgs)); + AutoPyRef res(PyObject_CallObject(joinPy,myArgs)); ret=PyBytes_AsString(res); } Py_XDECREF(kernelRootDir); diff --git a/src/evalyfx/YACSEvalSessionInternal.cxx b/src/evalyfx/YACSEvalSessionInternal.cxx index 45da13bf1..2ef842bdc 100644 --- a/src/evalyfx/YACSEvalSessionInternal.cxx +++ b/src/evalyfx/YACSEvalSessionInternal.cxx @@ -22,7 +22,7 @@ #include "YACSEvalSession.hxx" #include "PyStdout.hxx" -#include "AutoGIL.hxx" +#include "PythonCppUtils.hxx" #include "Exception.hxx" YACSEvalSession::YACSEvalSessionInternal::YACSEvalSessionInternal():_orb(CORBA::ORB::_nil()),_sl(Engines::SalomeLauncher::_nil()) @@ -46,8 +46,8 @@ Engines::SalomeLauncher_var YACSEvalSession::YACSEvalSessionInternal::goFetching // const char methName[]="goFetchingSalomeLauncherInNS"; const char fetchPyCmd[]="import salome,CORBA\nsalome.salome_init()\nsl=salome.naming_service.Resolve(\"/SalomeLauncher\")\nif not CORBA.is_nil(sl):\n return salome.orb.object_to_string(sl)\nelse:\n raise Exception(\"Impossible to locate salome launcher !\")"; - YACS::ENGINE::AutoPyRef func(YACS::ENGINE::evalPy(methName,fetchPyCmd)); - YACS::ENGINE::AutoPyRef val(YACS::ENGINE::evalFuncPyWithNoParams(func)); + AutoPyRef func(YACS::ENGINE::evalPy(methName,fetchPyCmd)); + AutoPyRef val(YACS::ENGINE::evalFuncPyWithNoParams(func)); std::string ior; if (PyUnicode_Check(val)) ior = PyUnicode_AsUTF8(val); diff --git a/src/evalyfx/YACSEvalYFXPattern.cxx b/src/evalyfx/YACSEvalYFXPattern.cxx index 8b5b3b44b..677963adb 100644 --- a/src/evalyfx/YACSEvalYFXPattern.cxx +++ b/src/evalyfx/YACSEvalYFXPattern.cxx @@ -43,7 +43,7 @@ #include "InlineNode.hxx" #include "ServiceNode.hxx" #include "PyStdout.hxx" -#include "AutoGIL.hxx" +#include "PythonCppUtils.hxx" #include "ResourcesManager.hxx" @@ -886,14 +886,14 @@ std::vector YACSEvalYFXGraphGenInteractive::getResults() const void YACSEvalYFXGraphGenCluster::generateGraph() { - YACS::ENGINE::AutoGIL agil; + AutoGIL agil; // const char EFXGenFileName[]="EFXGenFileName"; const char EFXGenContent[]="import getpass,datetime,os\nn=datetime.datetime.now()\nreturn os.path.join(os.path.sep,\"tmp\",\"EvalYFX_%s_%s_%s.xml\"%(getpass.getuser(),n.strftime(\"%d%m%y\"),n.strftime(\"%H%M%S\")))"; const char EFXGenContent2[]="import getpass,datetime\nn=datetime.datetime.now()\nreturn \"EvalYFX_%s_%s_%s\"%(getpass.getuser(),n.strftime(\"%d%m%y\"),n.strftime(\"%H%M%S\"))"; // - YACS::ENGINE::AutoPyRef func(YACS::ENGINE::evalPy(EFXGenFileName,EFXGenContent)); - YACS::ENGINE::AutoPyRef val(YACS::ENGINE::evalFuncPyWithNoParams(func)); + AutoPyRef func(YACS::ENGINE::evalPy(EFXGenFileName,EFXGenContent)); + AutoPyRef val(YACS::ENGINE::evalFuncPyWithNoParams(func)); if (PyUnicode_Check(val)) _locSchemaFile = PyUnicode_AsUTF8(val); else @@ -927,7 +927,7 @@ void YACSEvalYFXGraphGenCluster::generateGraph() bool YACSEvalYFXGraphGenCluster::go(const YACSEvalExecParams& params, YACSEvalSession *session) const { - YACS::ENGINE::AutoGIL agil; + AutoGIL agil; _errors = ""; getUndergroundGeneratedGraph()->saveSchema(_locSchemaFile); YACSEvalListOfResources *rss(getBoss()->getResourcesInternal()); @@ -1017,8 +1017,8 @@ bool YACSEvalYFXGraphGenCluster::go(const YACSEvalExecParams& params, YACSEvalSe oss << "f=open(p,\"r\")" << std::endl; oss << "return eval(f.read())"; std::string zeInput(oss.str()); - YACS::ENGINE::AutoPyRef func(YACS::ENGINE::evalPy("fetch",zeInput)); - YACS::ENGINE::AutoPyRef val(YACS::ENGINE::evalFuncPyWithNoParams(func)); + AutoPyRef func(YACS::ENGINE::evalPy("fetch",zeInput)); + AutoPyRef val(YACS::ENGINE::evalFuncPyWithNoParams(func)); if(!PyList_Check(val)) throw YACS::Exception("Fetched file does not contain a list !"); Py_ssize_t sz(PyList_Size(val)); diff --git a/src/py2yacs/py2yacs.cxx b/src/py2yacs/py2yacs.cxx index e4436ec53..9d77835c1 100644 --- a/src/py2yacs/py2yacs.cxx +++ b/src/py2yacs/py2yacs.cxx @@ -22,7 +22,7 @@ #include "RuntimeSALOME.hxx" #include "Proc.hxx" #include "InlineNode.hxx" -#include "AutoGIL.hxx" +#include "PythonCppUtils.hxx" #include "InputPort.hxx" #include "Container.hxx" @@ -180,7 +180,7 @@ void Py2yacs::load(const std::string& python_code) _global_errors.clear(); // Py_Initialize(); - YACS::ENGINE::AutoGIL agil; + AutoGIL agil; pValue = PyUnicode_FromString(_python_parser_module.c_str()); pModule = PyImport_Import(pValue); Py_DECREF(pValue); @@ -456,4 +456,4 @@ std::string Py2yacs::getFunctionErrors(const std::string& functionName)const buffer << "Function " << functionName << " not found." << std::endl; } return buffer.str(); -} \ No newline at end of file +} diff --git a/src/runtime/CMakeLists.txt b/src/runtime/CMakeLists.txt index b2f86dafb..c0e74f647 100644 --- a/src/runtime/CMakeLists.txt +++ b/src/runtime/CMakeLists.txt @@ -84,7 +84,6 @@ SET(_link_LIBRARIES SET(YACSRuntimeSALOME_HEADERS YACSRuntimeSALOMEExport.hxx - AutoGIL.hxx CalStreamPort.hxx CORBAComponent.hxx CORBACORBAConv.hxx diff --git a/src/runtime/DistributedPythonNode.cxx b/src/runtime/DistributedPythonNode.cxx index 8a26db683..a912dd92a 100644 --- a/src/runtime/DistributedPythonNode.cxx +++ b/src/runtime/DistributedPythonNode.cxx @@ -23,7 +23,7 @@ #include "PythonNode.hxx" #include "SalomeHPContainer.hxx" #include "SalomeContainerTmpForHP.hxx" -#include "AutoGIL.hxx" +#include "PythonCppUtils.hxx" #include "PythonPorts.hxx" #include "YacsTrace.hxx" diff --git a/src/runtime/PyStdout.cxx b/src/runtime/PyStdout.cxx index fcb29e6a8..0bdee3b99 100644 --- a/src/runtime/PyStdout.cxx +++ b/src/runtime/PyStdout.cxx @@ -19,7 +19,7 @@ #include "PyStdout.hxx" #include "Exception.hxx" -#include "AutoGIL.hxx" +#include "PythonCppUtils.hxx" #include diff --git a/src/runtime/PythonNode.cxx b/src/runtime/PythonNode.cxx index 42eae7446..fbc60bb49 100644 --- a/src/runtime/PythonNode.cxx +++ b/src/runtime/PythonNode.cxx @@ -21,7 +21,7 @@ #include "PythonNode.hxx" #include "PythonPorts.hxx" #include "TypeCode.hxx" -#include "AutoGIL.hxx" +#include "PythonCppUtils.hxx" #include "Container.hxx" #include "SalomeContainer.hxx" #include "SalomeHPContainer.hxx" diff --git a/src/ydfx_gui/YDFXGUISeqInit.cxx b/src/ydfx_gui/YDFXGUISeqInit.cxx index a451d6fce..3f8663cf0 100644 --- a/src/ydfx_gui/YDFXGUISeqInit.cxx +++ b/src/ydfx_gui/YDFXGUISeqInit.cxx @@ -33,7 +33,7 @@ #include -#include "AutoGIL.hxx" +#include "PythonCppUtils.hxx" #include "YDFXGUIWrap.hxx" #include "YDFXGUIPyThreadSaver.hxx" @@ -46,24 +46,6 @@ const char YDFXGUIStatus::OK_STR[]="OK !"; -class AutoPyRef -{ -public: - AutoPyRef(PyObject *pyobj=0):_pyobj(pyobj) { } - ~AutoPyRef() { release(); } - AutoPyRef(const AutoPyRef& other):_pyobj(other._pyobj) { if(_pyobj) Py_XINCREF(_pyobj); } - AutoPyRef& operator=(const AutoPyRef& other) { if(_pyobj==other._pyobj) return *this; release(); _pyobj=other._pyobj; Py_XINCREF(_pyobj); return *this; } - operator PyObject *() { return _pyobj; } - void set(PyObject *pyobj) { if(pyobj==_pyobj) return ; release(); _pyobj=pyobj; } - PyObject *get() { return _pyobj; } - bool isNull() const { return _pyobj==0; } - PyObject *retn() { if(_pyobj) Py_XINCREF(_pyobj); return _pyobj; } -private: - void release() { if(_pyobj) Py_XDECREF(_pyobj); _pyobj=0; } -private: - PyObject *_pyobj; -}; - //////////////////////////// void YDFXGUIDoubleVectHolder::applyOnInput(YACSEvalInputPort *inp) const @@ -199,7 +181,7 @@ bool YDFXGUISeqSetterT::executeScript(int& sz) std::string txt(toPlainText().toStdString()); YDFXGUIPyThreadSaver::SaveContext(QApplication::instance()->thread()); { - YACS::ENGINE::AutoGIL gal; + AutoGIL gal; AutoPyRef code(Py_CompileString(txt.c_str(),TMP_FILENAME, Py_file_input)); if(code.get() == NULL) { -- 2.39.2