public static string CallEchoString2(string arg) { IntPtr gs = PythonEngine.AcquireLock(); try { if (module == null) { module = PythonEngine.ModuleFromString("tt", testmod); } PyObject func = module.GetAttr("echostring2"); var parg = new PyString(arg); PyObject temp = func.Invoke(parg); var result = (string)temp.AsManagedObject(typeof(string)); func.Dispose(); parg.Dispose(); temp.Dispose(); return(result); } finally { PythonEngine.ReleaseLock(gs); } }
private void DisposePythonObject() { //Pythonnet added a check on refcount and when RefCount==1 sometimes throws a Memory access violation error from Python engine, validation that was not in previous pythonnet versions // if (PyObject?.Refcount != 1) { PyObject?.Dispose(); } PyObject = null; }
public void ImportClassShutdownRefcount() { PythonEngine.Initialize(); PyObject ns = Py.Import(typeof(ImportClassShutdownRefcountClass).Namespace); PyObject cls = ns.GetAttr(nameof(ImportClassShutdownRefcountClass)); ns.Dispose(); Assert.Less(cls.Refcount, 256); PythonEngine.Shutdown(); Assert.Greater(cls.Refcount, 0); }
/// <summary> /// This method calls back into the CPython runtime - tests /// call this from Python to check that we don't hang on /// nested transitions from managed to Python code and back. /// </summary> public static string CallEchoString(string arg) { using (Py.GIL()) { if (module == null) { module = PyModule.FromString("tt", testmod); } PyObject func = module.GetAttr("echostring"); var parg = new PyString(arg); PyObject temp = func.Invoke(parg); var result = (string)temp.AsManagedObject(typeof(string)); func.Dispose(); parg.Dispose(); temp.Dispose(); return(result); } }
public void ImportClassShutdownRefcount() { PythonEngine.Initialize(); PyObject ns = Py.Import(typeof(ImportClassShutdownRefcountClass).Namespace); PyObject cls = ns.GetAttr(nameof(ImportClassShutdownRefcountClass)); BorrowedReference clsRef = cls.Reference; #pragma warning disable CS0618 // Type or member is obsolete cls.Leak(); #pragma warning restore CS0618 // Type or member is obsolete ns.Dispose(); Assert.Less(Runtime.Runtime.Refcount32(clsRef), 256); PythonEngine.Shutdown(); Assert.Greater(Runtime.Runtime.Refcount32(clsRef), 0); }
public IEnumerator <T> GetEnumerator() { PyObject iterObject = null; using (Py.GIL()) iterObject = new PyObject(Runtime.PyObject_GetIter(pyObject.Handle)); while (true) { using (Py.GIL()) { var item = Runtime.PyIter_Next(iterObject.Handle); if (item == IntPtr.Zero) { Runtime.CheckExceptionOccurred(); iterObject.Dispose(); break; } yield return((T) new PyObject(item).AsManagedObject(typeof(T))); } } }
public void Dispose() { self?.Dispose(); }
public void Dispose() { threading.Dispose(); PythonEngine.Shutdown(); }
public void Dispose() { PyErr.Dispose(); }
public static void AppendRecycle(PyList list, PyObject obj) { list.Append(obj); obj.Dispose(); }
private void DisposePythonObject() { PyObject?.Dispose(); PyObject = null; }
public void Dispose() { _pobj?.Dispose(); }