Ejemplo n.º 1
0
        GenerateProtocolMagicMethods(IntPtr protocolPtr, Type protocol, string[] fields)
        {
            if (protocolPtr == IntPtr.Zero)
            {
                return;
            }

            foreach (string field in fields)
            {
                if (CPyMarshal.ReadPtrField(protocolPtr, protocol, field) != IntPtr.Zero)
                {
                    string name;
                    string template;
                    Type   dgtType;
                    bool   needGetSwappedInfo;
                    MagicMethods.GetInfo(field, out name, out template, out dgtType, out needGetSwappedInfo);
                    this.methodTable[this.tablePrefix + name] = CPyMarshal.ReadFunctionPtrField(protocolPtr, protocol, field, dgtType);
                    this.code.Append(String.Format(template, name, "", this.tablePrefix));

                    if (needGetSwappedInfo)
                    {
                        MagicMethods.GetSwappedInfo(field, out name, out template, out dgtType);
                        this.methodTable[this.tablePrefix + name] = CPyMarshal.ReadFunctionPtrField(protocolPtr, protocol, field, dgtType);;
                        this.code.Append(String.Format(template, name, "", this.tablePrefix));
                    }
                }
            }
        }
Ejemplo n.º 2
0
        AttemptToMap(IntPtr ptr)
        {
            if (this.HasPtr(ptr))
            {
                return;
            }

            if (ptr == IntPtr.Zero)
            {
                throw new CannotInterpretException("cannot map IntPtr.Zero");
            }

            IntPtr typePtr = CPyMarshal.ReadPtrField(ptr, typeof(PyTypeObject), "ob_type");

            this.AttemptToMap(typePtr);

            if (!this.actualisableTypes.ContainsKey(typePtr))
            {
                string msg   = "cannot map object at {0} with type at {1} ({2})";
                string type_ = CPyMarshal.ReadCStringField(typePtr, typeof(PyTypeObject), "tp_name");
                throw new CannotInterpretException(String.Format(msg, ptr.ToString("x"), typePtr.ToString("x"), type_));
            }

            this.actualisableTypes[typePtr](ptr);
        }
Ejemplo n.º 3
0
 PySequence_Repeat(IntPtr objPtr, int count)
 {
     try
     {
         IntPtr typePtr = CPyMarshal.ReadPtrField(objPtr, typeof(PyObject), "ob_type");
         IntPtr seqPtr  = CPyMarshal.ReadPtrField(typePtr, typeof(PyTypeObject), "tp_as_sequence");
         if (seqPtr != IntPtr.Zero)
         {
             IntPtr sq_repeat = CPyMarshal.ReadPtrField(seqPtr, typeof(PySequenceMethods), "sq_repeat");
             if (sq_repeat != IntPtr.Zero)
             {
                 dgt_ptr_ptrint dgt = (dgt_ptr_ptrint)CPyMarshal.ReadFunctionPtrField(
                     seqPtr, typeof(PySequenceMethods), "sq_repeat", typeof(dgt_ptr_ptrint));
                 return(dgt(objPtr, count));
             }
         }
         object obj = this.Retrieve(objPtr);
         if ((!Builtin.isinstance(obj, TypeCache.PythonType)) &&
             Builtin.hasattr(this.scratchContext, obj, "__len__") &&
             Builtin.hasattr(this.scratchContext, obj, "__getitem__"))
         {
             return(this.Store(PythonOperator.mul(this.scratchContext, obj, (int)count)));
         }
         throw PythonOps.TypeError("PySequence_Repeat: failed to convert {0} to sequence", obj);
     }
     catch (Exception e)
     {
         this.LastException = e;
         return(IntPtr.Zero);
     }
 }
Ejemplo n.º 4
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.º 5
0
        ActualiseArbitraryObject(IntPtr ptr)
        {
            IntPtr     typePtr = CPyMarshal.ReadPtrField(ptr, typeof(PyObject), "ob_type");
            PythonType type_   = (PythonType)this.Retrieve(typePtr);

            object[] args = new object[] {};
            if (Builtin.issubclass(this.scratchContext, type_, TypeCache.Int32))
            {
                args = new object[] { CPyMarshal.ReadIntField(ptr, typeof(PyIntObject), "ob_ival") };
            }
            if (Builtin.issubclass(this.scratchContext, type_, TypeCache.Double))
            {
                args = new object[] { CPyMarshal.ReadDoubleField(ptr, typeof(PyFloatObject), "ob_fval") };
            }
            if (Builtin.issubclass(this.scratchContext, type_, TypeCache.String))
            {
                args = new object[] { this.ReadPyString(ptr) };
            }
            if (Builtin.issubclass(this.scratchContext, type_, TypeCache.PythonType))
            {
                string      name     = CPyMarshal.ReadCStringField(ptr, typeof(PyTypeObject), "tp_name");
                PythonTuple tp_bases = this.ExtractBases(typePtr);
                args = new object[] { name, tp_bases, new PythonDictionary() };
            }

            object obj = PythonCalls.Call(this.classStubs[typePtr], args);

            Builtin.setattr(this.scratchContext, obj, "__class__", type_);
            this.StoreBridge(ptr, obj);
            this.IncRef(ptr);
            GC.KeepAlive(obj); // TODO: please test me, if you can work out how to
        }
Ejemplo n.º 6
0
        GenerateClass(IntPtr typePtr)
        {
            ClassBuilder cb       = new ClassBuilder(typePtr);
            PythonTuple  tp_bases = this.ExtractBases(typePtr);

            foreach (object _base in tp_bases)
            {
                this.UpdateMethodTableObj(cb.methodTable, _base);
            }

            IntPtr ob_typePtr = CPyMarshal.ReadPtrField(typePtr, typeof(PyObject), "ob_type");

            this.IncRef(ob_typePtr);
            object ob_type = this.Retrieve(ob_typePtr);

            this.scratchModule.Get__dict__()["_ironclad_metaclass"] = ob_type;
            this.scratchModule.Get__dict__()["_ironclad_bases"]     = tp_bases;
            this.ExecInModule(cb.code.ToString(), this.scratchModule);
            object klass      = this.scratchModule.Get__dict__()["_ironclad_class"];
            object klass_stub = this.scratchModule.Get__dict__()["_ironclad_class_stub"];

            this.classStubs[typePtr] = klass_stub;
            Builtin.setattr(this.scratchContext, klass, "_dispatcher", new Dispatcher(this, cb.methodTable));
            object typeDict = Builtin.getattr(this.scratchContext, klass, "__dict__");

            CPyMarshal.WritePtrField(typePtr, typeof(PyTypeObject), "tp_dict", this.Store(typeDict));
            return(klass);
        }
Ejemplo n.º 7
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);
            }
        }
Ejemplo n.º 8
0
 GenerateRichcmpMethods()
 {
     if (CPyMarshal.ReadPtrField(this.ptr, typeof(PyTypeObject), "tp_richcompare") != IntPtr.Zero)
     {
         this.code.Append(String.Format(CodeSnippets.RICHCMP_METHOD_TEMPLATE, tablePrefix));
         this.ConnectTypeField("tp_richcompare", typeof(dgt_ptr_ptrptrint));
     }
 }
Ejemplo n.º 9
0
 ConnectTypeField(string fieldName, Type dgtType)
 {
     if (CPyMarshal.ReadPtrField(this.ptr, typeof(PyTypeObject), fieldName) != IntPtr.Zero)
     {
         Delegate dgt = CPyMarshal.ReadFunctionPtrField(this.ptr, typeof(PyTypeObject), fieldName, dgtType);
         this.methodTable[this.tablePrefix + fieldName] = dgt;
     }
 }
Ejemplo n.º 10
0
        IC_PyBaseObject_Dealloc(IntPtr objPtr)
        {
            IntPtr       objType = CPyMarshal.ReadPtrField(objPtr, typeof(PyObject), "ob_type");
            dgt_void_ptr freeDgt = (dgt_void_ptr)
                                   CPyMarshal.ReadFunctionPtrField(
                objType, typeof(PyTypeObject), "tp_free", typeof(dgt_void_ptr));

            freeDgt(objPtr);
        }
Ejemplo n.º 11
0
        AddNumberMethodsWithIndex(IntPtr typePtr)
        {
            this.AddNumberMethodsWithoutIndex(typePtr);
            IntPtr nmPtr = CPyMarshal.ReadPtrField(typePtr, typeof(PyTypeObject), "tp_as_number");

            CPyMarshal.WritePtrField(nmPtr, typeof(PyNumberMethods), "nb_index", this.GetFuncPtr("PyNumber_Index"));

            Py_TPFLAGS flags = (Py_TPFLAGS)CPyMarshal.ReadIntField(typePtr, typeof(PyTypeObject), "tp_flags");

            flags |= Py_TPFLAGS.HAVE_INDEX;
            CPyMarshal.WriteIntField(typePtr, typeof(PyTypeObject), "tp_flags", (Int32)flags);
        }
Ejemplo n.º 12
0
        IC_PySlice_Dealloc(IntPtr slicePtr)
        {
            this.DecRef(CPyMarshal.ReadPtrField(slicePtr, typeof(PySliceObject), "start"));
            this.DecRef(CPyMarshal.ReadPtrField(slicePtr, typeof(PySliceObject), "stop"));
            this.DecRef(CPyMarshal.ReadPtrField(slicePtr, typeof(PySliceObject), "step"));

            dgt_void_ptr freeDgt = (dgt_void_ptr)
                                   CPyMarshal.ReadFunctionPtrField(
                this.PySlice_Type, typeof(PyTypeObject), "tp_free", typeof(dgt_void_ptr));

            freeDgt(slicePtr);
        }
Ejemplo n.º 13
0
        IC_PyMethod_Dealloc(IntPtr objPtr)
        {
            this.DecRef(CPyMarshal.ReadPtrField(objPtr, typeof(PyMethodObject), "im_func"));
            this.DecRef(CPyMarshal.ReadPtrField(objPtr, typeof(PyMethodObject), "im_self"));
            this.DecRef(CPyMarshal.ReadPtrField(objPtr, typeof(PyMethodObject), "im_class"));

            IntPtr       objType = CPyMarshal.ReadPtrField(objPtr, typeof(PyObject), "ob_type");
            dgt_void_ptr freeDgt = (dgt_void_ptr)
                                   CPyMarshal.ReadFunctionPtrField(
                objType, typeof(PyTypeObject), "tp_free", typeof(dgt_void_ptr));

            freeDgt(objPtr);
        }
Ejemplo n.º 14
0
        IC_file_dealloc(IntPtr ptr)
        {
            if (this.FILEs.ContainsKey(ptr))
            {
                Unmanaged.fclose(this.FILEs[ptr]);
                this.FILEs.Remove(ptr);
            }
            IntPtr       _type   = CPyMarshal.ReadPtrField(ptr, typeof(PyObject), "ob_type");
            dgt_void_ptr freeDgt = (dgt_void_ptr)
                                   CPyMarshal.ReadFunctionPtrField(
                _type, typeof(PyTypeObject), "tp_free", typeof(dgt_void_ptr));

            freeDgt(ptr);
        }
Ejemplo n.º 15
0
        InheritIntField(IntPtr typePtr, string name)
        {
            int fieldVal = CPyMarshal.ReadIntField(typePtr, typeof(PyTypeObject), name);

            if (fieldVal == 0)
            {
                IntPtr basePtr = CPyMarshal.ReadPtrField(typePtr, typeof(PyTypeObject), "tp_base");
                if (basePtr != IntPtr.Zero)
                {
                    CPyMarshal.WriteIntField(typePtr, typeof(PyTypeObject), name,
                                             CPyMarshal.ReadIntField(basePtr, typeof(PyTypeObject), name));
                }
            }
        }
Ejemplo n.º 16
0
        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)));
            }
        }
Ejemplo n.º 17
0
        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)));
            }
        }
Ejemplo n.º 18
0
        IC_PyInstance_Dealloc(IntPtr objPtr)
        {
            IntPtr dictPtr = CPyMarshal.ReadPtrField(objPtr, typeof(PyInstanceObject), "in_dict");

            if (dictPtr != IntPtr.Zero)
            {
                this.DecRef(dictPtr);
            }

            IntPtr       objType = CPyMarshal.ReadPtrField(objPtr, typeof(PyObject), "ob_type");
            dgt_void_ptr freeDgt = (dgt_void_ptr)
                                   CPyMarshal.ReadFunctionPtrField(
                objType, typeof(PyTypeObject), "tp_free", typeof(dgt_void_ptr));

            freeDgt(objPtr);
        }
Ejemplo n.º 19
0
        ExtractBases(IntPtr typePtr)
        {
            PythonTuple tp_bases    = null;
            IntPtr      tp_basesPtr = CPyMarshal.ReadPtrField(typePtr, typeof(PyTypeObject), "tp_bases");

            if (tp_basesPtr != IntPtr.Zero)
            {
                tp_bases = (PythonTuple)this.Retrieve(tp_basesPtr);
            }
            if (tp_bases == null)
            {
                IntPtr tp_basePtr = CPyMarshal.ReadPtrField(typePtr, typeof(PyTypeObject), "tp_base");
                tp_bases = new PythonTuple(new object[] { this.Retrieve(tp_basePtr) });
            }
            return(tp_bases);
        }
Ejemplo n.º 20
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);
     }
 }
Ejemplo n.º 21
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));
        }
Ejemplo n.º 22
0
 PySequence_Tuple(IntPtr seqPtr)
 {
     try
     {
         if (CPyMarshal.ReadPtrField(seqPtr, typeof(PyObject), "ob_type") == this.PyTuple_Type)
         {
             this.IncRef(seqPtr);
             return(seqPtr);
         }
         object seq = this.Retrieve(seqPtr);
         return(this.Store(PythonCalls.Call(TypeCache.PythonTuple, new object[] { seq })));
     }
     catch (Exception e)
     {
         this.LastException = e;
         return(IntPtr.Zero);
     }
 }
Ejemplo n.º 23
0
        DecRef(IntPtr ptr)
        {
            this.AttemptToMap(ptr);

            if (!this.HasPtr(ptr))
            {
                throw new KeyNotFoundException(String.Format(
                                                   "DecRef: missing key in pointer map: {0}", ptr.ToString("x")));
            }

            int count = CPyMarshal.ReadIntField(ptr, typeof(PyObject), "ob_refcnt");

            if (count == 0)
            {
                throw new BadRefCountException("Trying to DecRef an object with ref count 0");
            }
            else if (count == 1)
            {
                IntPtr typePtr = CPyMarshal.ReadPtrField(ptr, typeof(PyObject), "ob_type");
                if (typePtr == IntPtr.Zero)
                {
                    throw new CannotInterpretException(String.Format(
                                                           "Cannot destroy object at {0}: null type", ptr.ToString("x")));
                }

                if (CPyMarshal.ReadPtrField(typePtr, typeof(PyTypeObject), "tp_dealloc") == IntPtr.Zero)
                {
                    throw new CannotInterpretException(String.Format(
                                                           "Cannot destroy object at {0} with type at {1}: no dealloc function", ptr.ToString("x"), typePtr.ToString("x")));
                }

                dgt_void_ptr deallocDgt = (dgt_void_ptr)CPyMarshal.ReadFunctionPtrField(
                    typePtr, typeof(PyTypeObject), "tp_dealloc", typeof(dgt_void_ptr));
                deallocDgt(ptr);
            }
            else
            {
                CPyMarshal.WriteIntField(ptr, typeof(PyObject), "ob_refcnt", count - 1);
                if (this.map.HasPtr(ptr))
                {
                    this.map.UpdateStrength(ptr);
                }
            }
        }
Ejemplo n.º 24
0
        DumpPtr(IntPtr ptr)
        {
            if (!this.allocator.Contains(ptr))
            {
                // we don't own this memory; not our problem.
                return;
            }

            try
            {
                IntPtr typePtr = CPyMarshal.ReadPtrField(ptr, typeof(PyObject), "ob_type");
                if (typePtr == IntPtr.Zero)
                {
                    // no type
                    return;
                }

                if (CPyMarshal.ReadPtrField(typePtr, typeof(PyTypeObject), "tp_dealloc") == IntPtr.Zero)
                {
                    // no dealloc function
                    return;
                }

                dgt_void_ptr dealloc = (dgt_void_ptr)
                                       CPyMarshal.ReadFunctionPtrField(
                    typePtr, typeof(PyTypeObject), "tp_dealloc", typeof(dgt_void_ptr));
                dealloc(ptr);
            }

            // not really surprised to see these errors...
            catch (BadMappingException) {}
            catch (AccessViolationException) {}
            catch (NullReferenceException) {}

            // may be worth mentioning other errors though
            catch (Exception e)
            {
                if (this.logErrors)
                {
                    Console.WriteLine("unexpected error during DumpPtr:\n{0}", e);
                }
            }
        }
Ejemplo n.º 25
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);
        }
Ejemplo n.º 26
0
        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);
        }
Ejemplo n.º 27
0
        PySequence_SetItem(IntPtr objPtr, int idx, IntPtr valuePtr)
        {
            try
            {
                IntPtr typePtr = CPyMarshal.ReadPtrField(objPtr, typeof(PyObject), "ob_type");
                if (typePtr == this.PyList_Type)
                {
                    int newIdx = idx;
                    int length = CPyMarshal.ReadIntField(objPtr, typeof(PyListObject), "ob_size");
                    if (newIdx < 0)
                    {
                        newIdx += length;
                    }
                    if (newIdx >= 0 && newIdx < length)
                    {
                        this.IncRef(valuePtr);
                        return(this.PyList_SetItem(objPtr, newIdx, valuePtr));
                    }
                    // otherwise, fall through and allow normal exception to occur
                }

                object sequence = this.Retrieve(objPtr);
                object setitem;
                if (PythonOps.TryGetBoundAttr(sequence, "__setitem__", out setitem))
                {
                    PythonCalls.Call(setitem, idx, this.Retrieve(valuePtr));
                    return(0);
                }
                throw PythonOps.TypeError("PySequence_SetItem: failed to convert {0} to sequence", sequence);
            }
            catch (Exception e)
            {
                this.LastException = e;
                return(-1);
            }
        }
Ejemplo n.º 28
0
        PyType_Ready(IntPtr typePtr)
        {
            if (typePtr == IntPtr.Zero)
            {
                return(-1);
            }
            Py_TPFLAGS flags = (Py_TPFLAGS)CPyMarshal.ReadIntField(typePtr, typeof(PyTypeObject), "tp_flags");

            if ((Int32)(flags & (Py_TPFLAGS.READY | Py_TPFLAGS.READYING)) != 0)
            {
                return(0);
            }
            flags |= Py_TPFLAGS.READYING;
            CPyMarshal.WriteIntField(typePtr, typeof(PyTypeObject), "tp_flags", (Int32)flags);

            IntPtr typeTypePtr = CPyMarshal.ReadPtrField(typePtr, typeof(PyTypeObject), "ob_type");

            if ((typeTypePtr == IntPtr.Zero) && (typePtr != this.PyType_Type))
            {
                CPyMarshal.WritePtrField(typePtr, typeof(PyTypeObject), "ob_type", this.PyType_Type);
            }

            IntPtr typeBasePtr = CPyMarshal.ReadPtrField(typePtr, typeof(PyTypeObject), "tp_base");

            if ((typeBasePtr == IntPtr.Zero) && (typePtr != this.PyBaseObject_Type))
            {
                typeBasePtr = this.PyBaseObject_Type;
                CPyMarshal.WritePtrField(typePtr, typeof(PyTypeObject), "tp_base", typeBasePtr);
            }

            PyType_Ready(typeBasePtr);
            this.InheritSubclassFlags(typePtr);
            this.InheritPtrField(typePtr, "tp_alloc");
            this.InheritPtrField(typePtr, "tp_new");
            this.InheritPtrField(typePtr, "tp_dealloc");
            this.InheritPtrField(typePtr, "tp_free");
            this.InheritPtrField(typePtr, "tp_doc");
            this.InheritPtrField(typePtr, "tp_call");
            this.InheritPtrField(typePtr, "tp_as_number");
            this.InheritPtrField(typePtr, "tp_as_sequence");
            this.InheritPtrField(typePtr, "tp_as_mapping");
            this.InheritPtrField(typePtr, "tp_as_buffer");
            this.InheritIntField(typePtr, "tp_basicsize");
            this.InheritIntField(typePtr, "tp_itemsize");

            if (!this.HasPtr(typePtr))
            {
                this.Retrieve(typePtr);
            }
            else
            {
                object klass = this.Retrieve(typePtr);
                if (Builtin.hasattr(this.scratchContext, klass, "__dict__"))
                {
                    object typeDict = Builtin.getattr(this.scratchContext, klass, "__dict__");
                    CPyMarshal.WritePtrField(typePtr, typeof(PyTypeObject), "tp_dict", this.Store(typeDict));
                }
            }

            flags  = (Py_TPFLAGS)CPyMarshal.ReadIntField(typePtr, typeof(PyTypeObject), "tp_flags");
            flags |= Py_TPFLAGS.READY | Py_TPFLAGS.HAVE_CLASS;
            flags &= ~Py_TPFLAGS.READYING;
            CPyMarshal.WriteIntField(typePtr, typeof(PyTypeObject), "tp_flags", (Int32)flags);
            return(0);
        }
Ejemplo n.º 29
0
        GenerateMethods()
        {
            IntPtr methodsPtr = CPyMarshal.ReadPtrField(this.ptr, typeof(PyTypeObject), "tp_methods");

            CallableBuilder.GenerateMethods(this.code, methodsPtr, this.methodTable, this.tablePrefix);
        }
Ejemplo n.º 30
0
        GenerateNamedProtocolMagicMethods(string protocolName, Type protocolType, string[] fields)
        {
            IntPtr pPtr = CPyMarshal.ReadPtrField(this.ptr, typeof(PyTypeObject), protocolName);

            this.GenerateProtocolMagicMethods(pPtr, protocolType, fields);
        }