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); }
PyString_AsStringAndSize(IntPtr strPtr, IntPtr dataPtrPtr, IntPtr sizePtr) { try { if (CPyMarshal.ReadPtrField(strPtr, typeof(PyObject), "ob_type") != this.PyString_Type) { throw PythonOps.TypeError("PyString_AsStringAndSize: not a string"); } IntPtr dataPtr = CPyMarshal.GetField(strPtr, typeof(PyStringObject), "ob_sval"); CPyMarshal.WritePtr(dataPtrPtr, dataPtr); int length = CPyMarshal.ReadIntField(strPtr, typeof(PyStringObject), "ob_size"); if (sizePtr == IntPtr.Zero) { for (int i = 0; i < length; ++i) { if (CPyMarshal.ReadByte(CPyMarshal.Offset(dataPtr, i)) == 0) { throw PythonOps.TypeError("PyString_AsStringAndSize: string contains embedded 0s, but sizePtr is null"); } } } else { CPyMarshal.WriteInt(sizePtr, length); } return(0); } catch (Exception e) { this.LastException = e; return(-1); } }
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); }
EnsureGIL() { this.tempObjects.Push(new List <IntPtr>()); if (this.GIL.Acquire() == 1) { CPyMarshal.WritePtr(this._PyThreadState_Current, this.threadState.Ptr); } }
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); }
IC_str_getreadbuffer(IntPtr strPtr, int seg, IntPtr bufPtrPtr) { if (seg != 0) { this.LastException = PythonOps.SystemError("string buffers have only 1 segment"); return(-1); } IntPtr bufPtr = CPyMarshal.GetField(strPtr, typeof(PyStringObject), "ob_sval"); CPyMarshal.WritePtr(bufPtrPtr, bufPtr); return(this.PyString_Size(strPtr)); }
StoreTyped(PythonTuple tuple) { int length = tuple.__len__(); IntPtr tuplePtr = this.CreateTuple(length); IntPtr itemPtr = CPyMarshal.Offset( tuplePtr, Marshal.OffsetOf(typeof(PyTupleObject), "ob_item")); for (int i = 0; i < length; i++) { CPyMarshal.WritePtr(itemPtr, this.Store(tuple[i])); itemPtr = CPyMarshal.Offset(itemPtr, CPyMarshal.PtrSize); } this.map.Associate(tuplePtr, tuple); return(tuplePtr); }
Zero(IntPtr start, int bytes) { int ptrs = bytes / CPyMarshal.PtrSize; bytes = bytes % CPyMarshal.PtrSize; for (int i = 0; i < ptrs; i++) { CPyMarshal.WritePtr(start, IntPtr.Zero); start = CPyMarshal.Offset(start, CPyMarshal.PtrSize); } for (int i = 0; i < bytes; i++) { CPyMarshal.WriteByte(start, 0); start = CPyMarshal.Offset(start, 1); } }
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__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); }
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); }
ReleaseGIL() { if (this.tempObjects.Count > 0) { List <IntPtr> safeTemps = this.tempObjects.Pop(); if (safeTemps != null) { foreach (IntPtr ptr in safeTemps) { this.DecRef(ptr); } } } this.map.CheckBridgePtrs(false); if (this.GIL.CountAcquired == 1) { CPyMarshal.WritePtr(this._PyThreadState_Current, IntPtr.Zero); } this.GIL.Release(); }
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(); } }
_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); } }
PyDict_Next(IntPtr dictPtr, IntPtr posPtr, IntPtr keyPtrPtr, IntPtr valuePtrPtr) { // note: this is not efficient, and assumes constant ordering of results from // KeyCollection.GetEnumerator. Storing an iterator would probably work, // but we can't work out how to not leak it if iteration does not complete. try { IDictionary dict = (IDictionary)this.Retrieve(dictPtr); IEnumerator keys = dict.Keys.GetEnumerator(); int pos = CPyMarshal.ReadInt(posPtr); for (int i = 0; i <= pos; i++) { if (!keys.MoveNext()) { return(0); } } object key = keys.Current; IntPtr keyPtr = this.Store(key); this.DecRefLater(keyPtr); CPyMarshal.WritePtr(keyPtrPtr, keyPtr); IntPtr valuePtr = this.Store(dict[key]); this.DecRefLater(valuePtr); CPyMarshal.WritePtr(valuePtrPtr, valuePtr); CPyMarshal.WriteInt(posPtr, pos + 1); return(1); } catch (Exception e) { this.LastException = e; return(0); } }
Register__PyThreadState_Current(IntPtr address) { CPyMarshal.WritePtr(address, IntPtr.Zero); }
WritePtrField(IntPtr addr, Type type, string field, IntPtr data) { IntPtr writeAddr = CPyMarshal.GetField(addr, type, field); CPyMarshal.WritePtr(writeAddr, data); }
WriteFunctionPtrField(IntPtr addr, Type type, string field, Delegate dgt) { IntPtr writeAddr = CPyMarshal.GetField(addr, type, field); CPyMarshal.WritePtr(writeAddr, Marshal.GetFunctionPointerForDelegate(dgt)); }