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); } }
ReadFunctionPtrField(IntPtr addr, Type type, string field, Type dgtType) { IntPtr readAddr = CPyMarshal.GetField(addr, type, field); IntPtr funcPtr = CPyMarshal.ReadPtr(readAddr); return(Marshal.GetDelegateForFunctionPointer(funcPtr, dgtType)); }
ReadCStringField(IntPtr addr, Type type, string field) { IntPtr strPtrAddr = CPyMarshal.GetField(addr, type, field); IntPtr readAddr = CPyMarshal.ReadPtr(strPtrAddr); if (readAddr != IntPtr.Zero) { return(Marshal.PtrToStringAnsi(readAddr)); } return(""); }
PyString_InternFromString(IntPtr stringData) { IntPtr newStrPtr = PyString_FromString(stringData); IntPtr newStrPtrPtr = this.allocator.Alloc((uint)Marshal.SizeOf(typeof(IntPtr))); CPyMarshal.WritePtr(newStrPtrPtr, newStrPtr); this.PyString_InternInPlace(newStrPtrPtr); IntPtr newNewStrPtr = CPyMarshal.ReadPtr(newStrPtrPtr); this.allocator.Free(newStrPtrPtr); return(newNewStrPtr); }
_PyString_Resize(IntPtr strPtrPtr, int newSize) { IntPtr strPtr = CPyMarshal.ReadPtr(strPtrPtr); int size = CPyMarshal.ReadIntField(strPtr, typeof(PyStringObject), "ob_size"); if (size < newSize) { return(this.IC__PyString_Resize_Grow(strPtrPtr, newSize)); } else { return(this.IC__PyString_Resize_NoGrow(strPtr, newSize)); } }
ActualiseTuple(IntPtr ptr) { int itemCount = CPyMarshal.ReadIntField(ptr, typeof(PyTupleObject), "ob_size"); IntPtr itemAddressPtr = CPyMarshal.Offset(ptr, Marshal.OffsetOf(typeof(PyTupleObject), "ob_item")); object[] items = new object[itemCount]; for (int i = 0; i < itemCount; i++) { IntPtr itemPtr = CPyMarshal.ReadPtr(itemAddressPtr); items[i] = this.Retrieve(itemPtr); itemAddressPtr = CPyMarshal.Offset(itemAddressPtr, CPyMarshal.PtrSize); } this.incompleteObjects.Remove(ptr); this.map.Associate(ptr, new PythonTuple(items)); }
Log(IntPtr start, int bytes) { if (start == IntPtr.Zero) { Console.WriteLine("I refuse to attempt to read from 0x00000000"); return; } for (int i = 0; i < bytes / CPyMarshal.IntSize; i++) { if (i % 4 == 0) { Console.WriteLine(); } Console.Write("{0} ", CPyMarshal.ReadPtr(start).ToString("x8")); start = CPyMarshal.Offset(start, CPyMarshal.IntSize); } Console.WriteLine(); }
IC__PyString_Resize_Grow(IntPtr strPtrPtr, int newSize) { IntPtr oldStr = CPyMarshal.ReadPtr(strPtrPtr); IntPtr newStr = IntPtr.Zero; try { newStr = this.allocator.Realloc(oldStr, (uint)(Marshal.SizeOf(typeof(PyStringObject)) + newSize)); } catch (OutOfMemoryException e) { this.LastException = e; this.PyObject_Free(oldStr); return(-1); } CPyMarshal.WritePtr(strPtrPtr, newStr); this.incompleteObjects.Remove(oldStr); this.incompleteObjects[newStr] = UnmanagedDataMarker.PyStringObject; return(this.IC__PyString_Resize_NoGrow(newStr, newSize)); }
PyString_InternInPlace(IntPtr strPtrPtr) { IntPtr intStrPtr = IntPtr.Zero; IntPtr strPtr = CPyMarshal.ReadPtr(strPtrPtr); string str = (string)this.Retrieve(strPtr); if (this.internedStrings.ContainsKey(str)) { intStrPtr = this.internedStrings[str]; } else { intStrPtr = strPtr; this.internedStrings[str] = intStrPtr; this.IncRef(intStrPtr); } this.IncRef(intStrPtr); this.DecRef(strPtr); CPyMarshal.WritePtr(strPtrPtr, intStrPtr); }
ActualiseList(IntPtr ptr) { if (this.listsBeingActualised.ContainsKey(ptr)) { throw new Exception("Fatal error: PythonMapper.listsBeingActualised is somehow corrupt"); } List newList = new List(); this.listsBeingActualised[ptr] = newList; int length = CPyMarshal.ReadIntField(ptr, typeof(PyListObject), "ob_size"); if (length != 0) { IntPtr itemPtrPtr = CPyMarshal.ReadPtrField(ptr, typeof(PyListObject), "ob_item"); for (int i = 0; i < length; i++) { IntPtr itemPtr = CPyMarshal.ReadPtr(itemPtrPtr); if (itemPtr == IntPtr.Zero) { // We have *no* idea what to do here. throw new ArgumentException("Attempted to Retrieve uninitialised PyListObject -- expect strange bugs"); } if (this.listsBeingActualised.ContainsKey(itemPtr)) { newList.append(this.listsBeingActualised[itemPtr]); } else { newList.append(this.Retrieve(itemPtr)); } itemPtrPtr = CPyMarshal.Offset(itemPtrPtr, CPyMarshal.PtrSize); } } this.listsBeingActualised.Remove(ptr); this.incompleteObjects.Remove(ptr); this.map.Associate(ptr, newList); }
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); }
public object get_string(object instance, int offset) { this.mapper.EnsureGIL(); try { IntPtr instancePtr = this.mapper.Store(instance); IntPtr address = CPyMarshal.Offset(instancePtr, offset); IntPtr value = CPyMarshal.ReadPtr(address); object ret = null; if (value != IntPtr.Zero) { ret = Marshal.PtrToStringAnsi(value); } this.mapper.DecRef(instancePtr); return(ret); } finally { this.mapper.ReleaseGIL(); } }
public void set_object(object instance, int offset, object value) { this.mapper.EnsureGIL(); try { IntPtr instancePtr = this.mapper.Store(instance); IntPtr valuePtr = this.mapper.Store(value); IntPtr address = CPyMarshal.Offset(instancePtr, offset); IntPtr oldValuePtr = CPyMarshal.ReadPtr(address); CPyMarshal.WritePtr(address, valuePtr); if (oldValuePtr != IntPtr.Zero) { this.mapper.DecRef(oldValuePtr); } this.mapper.DecRef(instancePtr); } finally { this.mapper.ReleaseGIL(); } }
public object get_object(object instance, int offset) { this.mapper.EnsureGIL(); try { IntPtr instancePtr = this.mapper.Store(instance); IntPtr address = CPyMarshal.Offset(instancePtr, offset); IntPtr retptr = CPyMarshal.ReadPtr(address); object ret = null; if (retptr != IntPtr.Zero) { ret = this.mapper.Retrieve(retptr); } this.mapper.DecRef(instancePtr); return(ret); } finally { this.mapper.ReleaseGIL(); } }
IC_PyTuple_Dealloc(IntPtr tuplePtr) { int length = CPyMarshal.ReadIntField(tuplePtr, typeof(PyTupleObject), "ob_size"); IntPtr itemsPtr = CPyMarshal.Offset( tuplePtr, Marshal.OffsetOf(typeof(PyTupleObject), "ob_item")); for (int i = 0; i < length; i++) { IntPtr itemPtr = CPyMarshal.ReadPtr( CPyMarshal.Offset( itemsPtr, i * CPyMarshal.PtrSize)); if (itemPtr != IntPtr.Zero) { this.DecRef(itemPtr); } } dgt_void_ptr freeDgt = (dgt_void_ptr) CPyMarshal.ReadFunctionPtrField( this.PyTuple_Type, typeof(PyTypeObject), "tp_free", typeof(dgt_void_ptr)); freeDgt(tuplePtr); }
_PyTuple_Resize(IntPtr tuplePtrPtr, int length) { // note: make sure you don't actualise this try { IntPtr tuplePtr = CPyMarshal.ReadPtr(tuplePtrPtr); this.incompleteObjects.Remove(tuplePtr); uint newSize = (uint)Marshal.SizeOf(typeof(PyTupleObject)) + (uint)(CPyMarshal.PtrSize * (length - 1)); tuplePtr = this.allocator.Realloc(tuplePtr, newSize); CPyMarshal.WriteIntField(tuplePtr, typeof(PyTupleObject), "ob_size", length); this.incompleteObjects[tuplePtr] = UnmanagedDataMarker.PyTupleObject; CPyMarshal.WritePtr(tuplePtrPtr, tuplePtr); return(0); } catch (Exception e) { this.LastException = e; CPyMarshal.WritePtr(tuplePtrPtr, IntPtr.Zero); return(-1); } }
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); }
ReadPtrField(IntPtr addr, Type type, string field) { IntPtr readAddr = CPyMarshal.GetField(addr, type, field); return(CPyMarshal.ReadPtr(readAddr)); }