PyList_Append(IntPtr listPtr, IntPtr itemPtr)
        {
            PyListObject listStruct = (PyListObject)Marshal.PtrToStructure(listPtr, typeof(PyListObject));

            if (listStruct.ob_type != this.PyList_Type)
            {
                this.LastException = PythonOps.TypeError("PyList_Append: not a list");
                return(-1);
            }

            if (listStruct.ob_item == IntPtr.Zero)
            {
                this.IC_PyList_Append_Empty(listPtr, ref listStruct, itemPtr);
            }
            else
            {
                this.IC_PyList_Append_NonEmpty(listPtr, ref listStruct, itemPtr);
            }

            List list = (List)this.Retrieve(listPtr);

            list.append(this.Retrieve(itemPtr));
            this.IncRef(itemPtr);
            return(0);
        }
        PyList_New(int length)
        {
            if (length == 0)
            {
                return(this.IC_PyList_New_Zero());
            }

            PyListObject list = new PyListObject();

            list.ob_refcnt = 1;
            list.ob_type   = this.PyList_Type;
            list.ob_size   = length;
            list.allocated = length;

            int bytes = length * CPyMarshal.PtrSize;

            list.ob_item = this.allocator.Alloc((uint)bytes);
            CPyMarshal.Zero(list.ob_item, bytes);

            IntPtr listPtr = this.allocator.Alloc((uint)Marshal.SizeOf(typeof(PyListObject)));

            Marshal.StructureToPtr(list, listPtr, false);
            this.incompleteObjects[listPtr] = UnmanagedDataMarker.PyListObject;
            return(listPtr);
        }
        StoreTyped(List list)
        {
            int          length     = list.__len__();
            PyListObject listStruct = new PyListObject();

            listStruct.ob_refcnt = 1;
            listStruct.ob_type   = this.PyList_Type;
            listStruct.ob_size   = length;
            listStruct.allocated = length;

            uint   bytes = (uint)length * CPyMarshal.PtrSize;
            IntPtr data  = this.allocator.Alloc(bytes);

            listStruct.ob_item = data;
            for (int i = 0; i < length; i++)
            {
                CPyMarshal.WritePtr(data, this.Store(list[i]));
                data = CPyMarshal.Offset(data, CPyMarshal.PtrSize);
            }

            IntPtr listPtr = this.allocator.Alloc((uint)Marshal.SizeOf(typeof(PyListObject)));

            Marshal.StructureToPtr(listStruct, listPtr, false);
            this.map.Associate(listPtr, list);
            return(listPtr);
        }
 IC_PyList_Append_Empty(IntPtr listPtr, ref PyListObject listStruct, IntPtr itemPtr)
 {
     listStruct.ob_size   = 1;
     listStruct.allocated = 1;
     listStruct.ob_item   = this.allocator.Alloc(CPyMarshal.PtrSize);
     CPyMarshal.WritePtr(listStruct.ob_item, itemPtr);
     Marshal.StructureToPtr(listStruct, listPtr, false);
 }
        IC_PyList_New_Zero()
        {
            PyListObject list = new PyListObject();
            list.ob_refcnt = 1;
            list.ob_type = this.PyList_Type;
            list.ob_size = 0;
            list.allocated = 0;
            list.ob_item = IntPtr.Zero;

            IntPtr listPtr = this.allocator.Alloc((uint)Marshal.SizeOf(typeof(PyListObject)));
            Marshal.StructureToPtr(list, listPtr, false);
            this.map.Associate(listPtr, new List());
            return listPtr;
        }
        IC_PyList_New_Zero()
        {
            PyListObject list = new PyListObject();

            list.ob_refcnt = 1;
            list.ob_type   = this.PyList_Type;
            list.ob_size   = 0;
            list.allocated = 0;
            list.ob_item   = IntPtr.Zero;

            IntPtr listPtr = this.allocator.Alloc((uint)Marshal.SizeOf(typeof(PyListObject)));

            Marshal.StructureToPtr(list, listPtr, false);
            this.map.Associate(listPtr, new List());
            return(listPtr);
        }
        IC_PyList_Append_NonEmpty(IntPtr listPtr, ref PyListObject listStruct, IntPtr itemPtr)
        {
            int oldAllocated      = listStruct.allocated;
            int oldAllocatedBytes = oldAllocated * CPyMarshal.PtrSize;

            listStruct.ob_size   += 1;
            listStruct.allocated += 1;
            IntPtr oldDataStore = listStruct.ob_item;

            int newAllocatedBytes = listStruct.allocated * CPyMarshal.PtrSize;

            listStruct.ob_item = this.allocator.Realloc(listStruct.ob_item, (uint)newAllocatedBytes);

            CPyMarshal.WritePtr(CPyMarshal.Offset(listStruct.ob_item, oldAllocatedBytes), itemPtr);
            Marshal.StructureToPtr(listStruct, listPtr, false);
        }
        IC_PyList_Dealloc(IntPtr listPtr)
        {
            PyListObject listStruct = (PyListObject)Marshal.PtrToStructure(listPtr, typeof(PyListObject));

            if (listStruct.ob_item != IntPtr.Zero)
            {
                IntPtr itemsPtr = listStruct.ob_item;
                for (int i = 0; i < listStruct.ob_size; i++)
                {
                    IntPtr itemPtr = CPyMarshal.ReadPtr(itemsPtr);
                    if (itemPtr != IntPtr.Zero)
                    {
                        this.DecRef(itemPtr);
                    }
                    itemsPtr = CPyMarshal.Offset(itemsPtr, CPyMarshal.PtrSize);
                }
                this.allocator.Free(listStruct.ob_item);
            }
            dgt_void_ptr freeDgt = (dgt_void_ptr)
                                   CPyMarshal.ReadFunctionPtrField(
                this.PyList_Type, typeof(PyTypeObject), "tp_free", typeof(dgt_void_ptr));

            freeDgt(listPtr);
        }
        StoreTyped(List list)
        {
            int length = list.__len__();
            PyListObject listStruct = new PyListObject();
            listStruct.ob_refcnt = 1;
            listStruct.ob_type = this.PyList_Type;
            listStruct.ob_size = length;
            listStruct.allocated = length;

            uint bytes = (uint)length * CPyMarshal.PtrSize;
            IntPtr data = this.allocator.Alloc(bytes);
            listStruct.ob_item = data;
            for (int i = 0; i < length; i++)
            {
                CPyMarshal.WritePtr(data, this.Store(list[i]));
                data = CPyMarshal.Offset(data, CPyMarshal.PtrSize);
            }

            IntPtr listPtr = this.allocator.Alloc((uint)Marshal.SizeOf(typeof(PyListObject)));
            Marshal.StructureToPtr(listStruct, listPtr, false);
            this.map.Associate(listPtr, list);
            return listPtr;

        }
Exemple #10
0
        private void IC_PyList_Append_NonEmpty(IntPtr listPtr, ref PyListObject listStruct, IntPtr itemPtr)
        {
            int oldAllocated = listStruct.allocated;
            int oldAllocatedBytes = oldAllocated * CPyMarshal.PtrSize;
            listStruct.ob_size += 1;
            listStruct.allocated += 1;
            IntPtr oldDataStore = listStruct.ob_item;

            int newAllocatedBytes = listStruct.allocated * CPyMarshal.PtrSize;
            listStruct.ob_item = this.allocator.Realloc(listStruct.ob_item, (uint)newAllocatedBytes);

            CPyMarshal.WritePtr(CPyMarshal.Offset(listStruct.ob_item, oldAllocatedBytes), itemPtr);
            Marshal.StructureToPtr(listStruct, listPtr, false);
        }
Exemple #11
0
 private void IC_PyList_Append_Empty(IntPtr listPtr, ref PyListObject listStruct, IntPtr itemPtr)
 {
     listStruct.ob_size = 1;
     listStruct.allocated = 1;
     listStruct.ob_item = this.allocator.Alloc(CPyMarshal.PtrSize);
     CPyMarshal.WritePtr(listStruct.ob_item, itemPtr);
     Marshal.StructureToPtr(listStruct, listPtr, false);
 }
Exemple #12
0
        public override IntPtr PyList_New(int length)
        {
            if (length == 0)
            {
                return this.IC_PyList_New_Zero();
            }

            PyListObject list = new PyListObject();
            list.ob_refcnt = 1;
            list.ob_type = this.PyList_Type;
            list.ob_size = length;
            list.allocated = length;

            int bytes = length * CPyMarshal.PtrSize;
            list.ob_item = this.allocator.Alloc((uint)bytes);
            CPyMarshal.Zero(list.ob_item, bytes);

            IntPtr listPtr = this.allocator.Alloc((uint)Marshal.SizeOf(typeof(PyListObject)));
            Marshal.StructureToPtr(list, listPtr, false);
            this.incompleteObjects[listPtr] = UnmanagedDataMarker.PyListObject;
            return listPtr;
        }
Exemple #13
0
 /// <summary>
 ///     Unwraps from <see cref="IPyObject"/> wrapper.
 /// </summary>
 /// <param name="pyList"></param>
 /// <returns></returns>
 public static IList <object> UnPy(this PyListObject pyList)
 => pyList.Value.Select(x => x.Value).ToList();