示例#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));
                    }
                }
            }
        }
示例#2
0
        PyType_GenericNew(IntPtr typePtr, IntPtr args, IntPtr kwargs)
        {
            dgt_ptr_ptrint dgt = (dgt_ptr_ptrint)CPyMarshal.ReadFunctionPtrField(
                typePtr, typeof(PyTypeObject), "tp_alloc", typeof(dgt_ptr_ptrint));

            return(dgt(typePtr, 0));
        }
示例#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);
     }
 }
示例#4
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;
     }
 }
示例#5
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);
        }
        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);
        }
示例#7
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);
        }
示例#8
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);
        }
示例#9
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);
        }
示例#10
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);
                }
            }
        }
示例#11
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);
                }
            }
        }
示例#12
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);
        }
示例#13
0
        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);
        }