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); } }
PyType_GenericAlloc(IntPtr typePtr, int nItems) { int size = CPyMarshal.ReadIntField(typePtr, typeof(PyTypeObject), "tp_basicsize"); if (nItems > 0) { int itemsize = CPyMarshal.ReadIntField(typePtr, typeof(PyTypeObject), "tp_itemsize"); size += (nItems * itemsize); } IntPtr newInstance = this.allocator.Alloc((uint)size); CPyMarshal.Zero(newInstance, size); CPyMarshal.WriteUIntField(newInstance, typeof(PyObject), "ob_refcnt", 1); CPyMarshal.WritePtrField(newInstance, typeof(PyObject), "ob_type", typePtr); if (nItems > 0) { CPyMarshal.WriteIntField(newInstance, typeof(PyVarObject), "ob_size", nItems); } return(newInstance); }