Salome HOME
[EDF30062] : Steering proxy threshold/directory without environement considerations...
[modules/kernel.git] / src / Basics / PythonCppUtils.hxx
index 41a286dc0f36c0574426db2c30b4f4873ff2ec1d..8a54b86bb7013ea88693b769e8160dcaae036702 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2021-2022  CEA/DEN, EDF R&D
+// 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
@@ -36,14 +36,31 @@ public:
   AutoPyRef(PyObject *pyobj=nullptr):_pyobj(pyobj) { }
   ~AutoPyRef() { release(); }
   AutoPyRef(const AutoPyRef& other):_pyobj(other._pyobj) { if(_pyobj) Py_XINCREF(_pyobj); }
+  AutoPyRef(AutoPyRef&& other) = default;
   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; }
+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); }
+};