Ejemplo n.º 1
0
        PySequence_GetItem(IntPtr objPtr, int idx)
        {
            try
            {
                if (CPyMarshal.ReadPtrField(objPtr, typeof(PyObject), "ob_type") == this.PyTuple_Type)
                {
                    IntPtr storagePtr = CPyMarshal.Offset(objPtr, Marshal.OffsetOf(typeof(PyTupleObject), "ob_item"));
                    int    size       = CPyMarshal.ReadIntField(objPtr, typeof(PyTupleObject), "ob_size");
                    if (idx >= size)
                    {
                        throw PythonOps.IndexError("PySequence_GetItem: tuple index {0} out of range", idx);
                    }

                    IntPtr slotPtr = CPyMarshal.Offset(storagePtr, idx * CPyMarshal.PtrSize);
                    IntPtr itemPtr = CPyMarshal.ReadPtr(slotPtr);
                    uint   refcnt  = CPyMarshal.ReadUIntField(itemPtr, typeof(PyObject), "ob_refcnt");
                    CPyMarshal.WriteUIntField(itemPtr, typeof(PyObject), "ob_refcnt", refcnt + 1);
                    return(itemPtr);
                }

                object sequence = this.Retrieve(objPtr);
                object getitem;
                if (PythonOps.TryGetBoundAttr(sequence, "__getitem__", out getitem))
                {
                    return(this.Store(PythonCalls.Call(getitem, idx)));
                }
                throw PythonOps.TypeError("PySequence_GetItem: failed to convert {0} to sequence", sequence);
            }
            catch (Exception e)
            {
                this.LastException = e;
                return(IntPtr.Zero);
            }
        }
Ejemplo n.º 2
0
        _PyObject_New(IntPtr typePtr)
        {
            uint   tp_basicsize = CPyMarshal.ReadUIntField(typePtr, typeof(PyTypeObject), "tp_basicsize");
            IntPtr objPtr       = this.allocator.Alloc(tp_basicsize);

            CPyMarshal.Zero(objPtr, tp_basicsize);
            return(this.PyObject_Init(objPtr, typePtr));
        }
Ejemplo n.º 3
0
        PyList_SetItem(IntPtr listPtr, int index, IntPtr itemPtr)
        {
            if (!this.HasPtr(listPtr))
            {
                this.DecRef(itemPtr);
                return(-1);
            }
            IntPtr typePtr = CPyMarshal.ReadPtrField(listPtr, typeof(PyObject), "ob_type");

            if (typePtr != this.PyList_Type)
            {
                this.DecRef(itemPtr);
                return(-1);
            }

            uint length = CPyMarshal.ReadUIntField(listPtr, typeof(PyListObject), "ob_size");

            if (index < 0 || index >= length)
            {
                this.DecRef(itemPtr);
                return(-1);
            }

            IntPtr dataPtr       = CPyMarshal.ReadPtrField(listPtr, typeof(PyListObject), "ob_item");
            IntPtr oldItemPtrPtr = CPyMarshal.Offset(dataPtr, (int)(index * CPyMarshal.PtrSize));
            IntPtr oldItemPtr    = CPyMarshal.ReadPtr(oldItemPtrPtr);

            if (oldItemPtr != IntPtr.Zero)
            {
                this.DecRef(oldItemPtr);
            }
            CPyMarshal.WritePtr(oldItemPtrPtr, itemPtr);

            if (this.map.HasPtr(listPtr))
            {
                object item = this.Retrieve(itemPtr);
                List   list = (List)this.Retrieve(listPtr);
                list[index] = item;
            }
            return(0);
        }