コード例 #1
0
        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);
            }
        }
コード例 #2
0
ファイル: PythonMapper_list.cs プロジェクト: slozier/ironclad
        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);
        }
コード例 #3
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);
            }
        }
コード例 #4
0
        CreatePyStringWithBytes(byte[] bytes)
        {
            IntPtr strPtr = this.AllocPyString(bytes.Length);
            IntPtr bufPtr = CPyMarshal.Offset(
                strPtr, Marshal.OffsetOf(typeof(PyStringObject), "ob_sval"));

            Marshal.Copy(bytes, 0, bufPtr, bytes.Length);
            return(strPtr);
        }
コード例 #5
0
        IC__PyString_Resize_NoGrow(IntPtr strPtr, int newSize)
        {
            CPyMarshal.WriteIntField(strPtr, typeof(PyStringObject), "ob_size", newSize);
            IntPtr bufPtr = CPyMarshal.Offset(
                strPtr, Marshal.OffsetOf(typeof(PyStringObject), "ob_sval"));
            IntPtr terminatorPtr = CPyMarshal.Offset(
                bufPtr, newSize);

            CPyMarshal.WriteByte(terminatorPtr, 0);
            return(0);
        }
コード例 #6
0
ファイル: ClassBuilder.cs プロジェクト: nathanwblair/ironclad
        GenerateMembers()
        {
            IntPtr memberPtr = CPyMarshal.ReadPtrField(this.ptr, typeof(PyTypeObject), "tp_members");

            if (memberPtr == IntPtr.Zero)
            {
                return;
            }

            while (CPyMarshal.ReadInt(memberPtr) != 0)
            {
                this.GenerateMember(memberPtr);
                memberPtr = CPyMarshal.Offset(memberPtr, Marshal.SizeOf(typeof(PyMemberDef)));
            }
        }
コード例 #7
0
ファイル: ClassBuilder.cs プロジェクト: nathanwblair/ironclad
        GenerateProperties()
        {
            IntPtr getsetPtr = CPyMarshal.ReadPtrField(this.ptr, typeof(PyTypeObject), "tp_getset");

            if (getsetPtr == IntPtr.Zero)
            {
                return;
            }

            while (CPyMarshal.ReadInt(getsetPtr) != 0)
            {
                this.GenerateProperty(getsetPtr);
                getsetPtr = CPyMarshal.Offset(getsetPtr, Marshal.SizeOf(typeof(PyGetSetDef)));
            }
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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));
        }
コード例 #10
0
 PyString_AsString(IntPtr strPtr)
 {
     try
     {
         if (CPyMarshal.ReadPtrField(strPtr, typeof(PyObject), "ob_type") != this.PyString_Type)
         {
             throw PythonOps.TypeError("PyString_AsString: not a string");
         }
         return(CPyMarshal.Offset(strPtr, Marshal.OffsetOf(typeof(PyStringObject), "ob_sval")));
     }
     catch (Exception e)
     {
         this.LastException = e;
         return(IntPtr.Zero);
     }
 }
コード例 #11
0
ファイル: PythonMapper_list.cs プロジェクト: slozier/ironclad
        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);
        }
コード例 #12
0
ファイル: CPyMarshal.cs プロジェクト: nathanwblair/ironclad
        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);
            }
        }
コード例 #13
0
        PyString_FromString(IntPtr stringData)
        {
            IntPtr      current   = stringData;
            List <byte> bytesList = new List <byte>();

            while (CPyMarshal.ReadByte(current) != 0)
            {
                bytesList.Add(CPyMarshal.ReadByte(current));
                current = CPyMarshal.Offset(current, 1);
            }
            byte[] bytes = new byte[bytesList.Count];
            bytesList.CopyTo(bytes);
            // note: NOT Associate
            // couldn't figure out to test this directly
            // without this, h5py tests get horribly screwy in PHIL contextmanager
            return(this.Store(this.StringFromBytes(bytes)));
        }
コード例 #14
0
        ReadPyString(IntPtr ptr)
        {
            IntPtr typePtr = CPyMarshal.ReadPtrField(ptr, typeof(PyObject), "ob_type");

            if (PyType_IsSubtype(typePtr, this.PyString_Type) == 0)
            {
                throw new ArgumentTypeException("ReadPyString: Expected a str, or subclass thereof");
            }
            IntPtr buffer = CPyMarshal.Offset(ptr, Marshal.OffsetOf(typeof(PyStringObject), "ob_sval"));
            int    length = CPyMarshal.ReadIntField(ptr, typeof(PyStringObject), "ob_size");

            byte[] bytes = new byte[length];
            Marshal.Copy(buffer, bytes, 0, length);
            char[] chars = Array.ConvertAll <byte, char>(
                bytes, new Converter <byte, char>(CharFromByte));
            return(new string(chars));
        }
コード例 #15
0
ファイル: CPyMarshal.cs プロジェクト: nathanwblair/ironclad
        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();
        }
コード例 #16
0
        CreateTuple(int size)
        {
            PyTupleObject tuple = new PyTupleObject();

            tuple.ob_refcnt = 1;
            tuple.ob_type   = this.PyTuple_Type;
            tuple.ob_size   = size;

            int    baseSize  = Marshal.SizeOf(typeof(PyTupleObject));
            int    extraSize = (CPyMarshal.PtrSize * (size - 1));
            IntPtr tuplePtr  = this.allocator.Alloc((uint)(baseSize + extraSize));

            Marshal.StructureToPtr(tuple, tuplePtr, false);

            IntPtr itemsPtr = CPyMarshal.Offset(
                tuplePtr, Marshal.OffsetOf(typeof(PyTupleObject), "ob_item"));

            CPyMarshal.Zero(itemsPtr, CPyMarshal.PtrSize * size);
            return(tuplePtr);
        }
コード例 #17
0
        AllocPyString(int length)
        {
            uint   size = (uint)(Marshal.SizeOf(typeof(PyStringObject)) + length);
            IntPtr data = this.allocator.Alloc(size);

            PyStringObject s = new PyStringObject();

            s.ob_refcnt = 1;
            s.ob_type   = this.PyString_Type;
            s.ob_size   = length;
            s.ob_shash  = -1;
            s.ob_sstate = 0;
            Marshal.StructureToPtr(s, data, false);

            IntPtr terminator = CPyMarshal.Offset(data, size - 1);

            CPyMarshal.WriteByte(terminator, 0);

            return(data);
        }
コード例 #18
0
ファイル: PythonMapper_list.cs プロジェクト: slozier/ironclad
        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);
        }
コード例 #19
0
ファイル: PythonMapper_list.cs プロジェクト: slozier/ironclad
        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);
        }
コード例 #20
0
 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();
     }
 }
コード例 #21
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();
     }
 }
コード例 #22
0
 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();
     }
 }
コード例 #23
0
        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);
        }
コード例 #24
0
ファイル: PythonMapper_list.cs プロジェクト: slozier/ironclad
        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);
        }
コード例 #25
0
        GenerateCallablesFromMethodDefs(StringBuilder code,
                                        IntPtr methods,
                                        PythonDictionary methodTable,
                                        string tablePrefix,
                                        string oldargsTemplate,
                                        string noargsTemplate,
                                        string objargTemplate,
                                        string varargsTemplate,
                                        string varargsKwargsTemplate)
        {
            IntPtr methodPtr = methods;

            if (methodPtr == IntPtr.Zero)
            {
                return;
            }

            while (CPyMarshal.ReadInt(methodPtr) != 0)
            {
                PyMethodDef thisMethod = (PyMethodDef)Marshal.PtrToStructure(
                    methodPtr, typeof(PyMethodDef));
                string   name     = thisMethod.ml_name;
                string   template = null;
                Delegate dgt      = null;

                // COEXIST flag ignored; which method is chosen depends on order of calls in ClassBuilder.
                bool unsupportedFlags = false;
                METH flags            = (METH)thisMethod.ml_flags & ~METH.COEXIST;
                switch (flags)
                {
                case METH.OLDARGS:
                    template = oldargsTemplate;
                    dgt      = Marshal.GetDelegateForFunctionPointer(
                        thisMethod.ml_meth,
                        typeof(dgt_ptr_ptrptr));
                    break;

                case METH.NOARGS:
                    template = noargsTemplate;
                    dgt      = Marshal.GetDelegateForFunctionPointer(
                        thisMethod.ml_meth,
                        typeof(dgt_ptr_ptrptr));
                    break;

                case METH.O:
                    template = objargTemplate;
                    dgt      = Marshal.GetDelegateForFunctionPointer(
                        thisMethod.ml_meth,
                        typeof(dgt_ptr_ptrptr));
                    break;

                case METH.VARARGS:
                    template = varargsTemplate;
                    dgt      = Marshal.GetDelegateForFunctionPointer(
                        thisMethod.ml_meth,
                        typeof(dgt_ptr_ptrptr));
                    break;

                case METH.KEYWORDS:
                case METH.VARARGS | METH.KEYWORDS:
                    template = varargsKwargsTemplate;
                    dgt      = Marshal.GetDelegateForFunctionPointer(
                        thisMethod.ml_meth,
                        typeof(dgt_ptr_ptrptrptr));
                    break;

                default:
                    unsupportedFlags = true;
                    break;
                }

                if (!unsupportedFlags)
                {
                    code.Append(String.Format(template,
                                              name, thisMethod.ml_doc, tablePrefix));
                    methodTable[tablePrefix + name] = dgt;
                }
                else
                {
                    Console.WriteLine("Detected unsupported method flags for {0}{1} ({2}); ignoring.",
                                      tablePrefix, name, thisMethod.ml_flags);
                }

                methodPtr = CPyMarshal.Offset(methodPtr, Marshal.SizeOf(typeof(PyMethodDef)));
            }
        }
コード例 #26
0
ファイル: CPyMarshal.cs プロジェクト: nathanwblair/ironclad
 GetField(IntPtr addr, Type type, string field)
 {
     return(CPyMarshal.Offset(addr, Marshal.OffsetOf(type, field)));
 }