示例#1
0
        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;
        }
示例#3
0
        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);
        }
示例#4
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);
     }
 }
示例#5
0
        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);
        }
示例#6
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)));
                }
            }
        }
示例#7
0
 public void Dispose()
 {
     self?.Dispose();
 }
示例#8
0
 public void Dispose()
 {
     threading.Dispose();
     PythonEngine.Shutdown();
 }
示例#9
0
 public void Dispose()
 {
     PyErr.Dispose();
 }
示例#10
0
文件: Utils.cs 项目: shuyi3/AIPJ
 public static void AppendRecycle(PyList list, PyObject obj)
 {
     list.Append(obj);
     obj.Dispose();
 }
示例#11
0
 private void DisposePythonObject()
 {
     PyObject?.Dispose();
     PyObject = null;
 }
示例#12
0
 public void Dispose()
 {
     _pobj?.Dispose();
 }