You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PyObjectpyList=PyObject.Create(CPythonAPI.PyList_New(0));// TODO: preallocate based on items.Length and use PyList_SetItem
usingCSnakes.Runtime.CPython;usingSystem.Runtime.InteropServices.Marshalling;namespaceCSnakes.Runtime.Python;/// <summary>/// These methods are used internally to create a PyObject where the Dispose() call will dispose all items in /// the collection inside the same call stack. This avoids the .NET GC Finalizer thread from disposing the items/// that were created and creating a GIL contention issue when other code is running./// </summary>internalstaticclassPack{internalstaticPyObjectCreateTuple(Span<PyObject>items){List<SafeHandleMarshaller<PyObject>.ManagedToUnmanagedIn>marshallers=new(items.Length);try{varhandles=items.Length<18// .NET tuples are max 17 items. This is a performance optimization.?stackallocIntPtr[items.Length]:newIntPtr[items.Length];for(inti=0;i<items.Length;i++){SafeHandleMarshaller<PyObject>.ManagedToUnmanagedInm=default;m.FromManaged(items[i]);marshallers.Add(m);handles[i]=m.ToUnmanaged();}returnPyObject.Create(CPythonAPI.PackTuple(handles));}finally{foreach(varminmarshallers){m.Free();}}}internalstaticPyObjectCreateList(Span<PyObject>items){PyObjectpyList=PyObject.Create(CPythonAPI.PyList_New(0));// TODO: preallocate based on items.Length and use PyList_SetItemforeach(variteminitems){intresult=CPythonAPI.PyList_Append(pyList,item);if(result==-1){throwPyObject.ThrowPythonExceptionAsClrException();}}returnpyList;}internalstaticPyObjectCreateDictionary(IEnumerable<PyObject>keys,IEnumerable<PyObject>values){PyObjectpyDict=PyObject.Create(CPythonAPI.PyDict_New());IEnumerator<PyObject>keyEnumerator=keys.GetEnumerator();IEnumerator<PyObject>valueEnumerator=values.GetEnumerator();while(keyEnumerator.MoveNext()&&valueEnumerator.MoveNext()){intresult=CPythonAPI.PyDict_SetItem(pyDict,keyEnumerator.Current,valueEnumerator.Current);if(result==-1){throwPyObject.ThrowPythonExceptionAsClrException();}}returnpyDict;}}
The text was updated successfully, but these errors were encountered:
CSnakes/src/CSnakes.Runtime/Python/Pack.cs
Line 42 in 4090d5b
The text was updated successfully, but these errors were encountered: