X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FBasics%2FPythonCppUtils.hxx;h=8a54b86bb7013ea88693b769e8160dcaae036702;hb=d1070d09cfbba6d9b72d8f2ca8d07d23f296686c;hp=93a3cfb5939dfd720ba2698ded99d77064f0fe14;hpb=461f7bd0f18eda1bf99df8598c12b967f302c479;p=modules%2Fkernel.git diff --git a/src/Basics/PythonCppUtils.hxx b/src/Basics/PythonCppUtils.hxx index 93a3cfb59..8a54b86bb 100644 --- a/src/Basics/PythonCppUtils.hxx +++ b/src/Basics/PythonCppUtils.hxx @@ -1,4 +1,4 @@ -// Copyright (C) 2021-2023 CEA, EDF +// Copyright (C) 2021-2024 CEA, EDF // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -43,8 +43,24 @@ public: 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; } +protected: + void release() { if(_pyobj) Py_XDECREF(_pyobj); _pyobj=nullptr; } private: PyObject *_pyobj = nullptr; }; + +class AutoPyRefGilSafe : public AutoPyRef +{ +public: + AutoPyRefGilSafe(PyObject *pyobj=nullptr):AutoPyRef(pyobj) { } + ~AutoPyRefGilSafe() { AutoGIL agil; release(); } +}; + +class AutoPyYielder +{ +private: + PyThreadState *_save = nullptr; +public: + AutoPyYielder() { _save = PyEval_SaveThread(); } + ~AutoPyYielder() { PyEval_RestoreThread(_save); } +};