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)));
            }
        }
        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)));
            }
        }
        UpdateStrength(IntPtr ptr)
        {
            long id = this.ptr2id[ptr];

            if (!this.id2wref.ContainsKey(id))
            {
                return;
            }

            object obj    = this.id2wref[id].Target;
            int    refcnt = CPyMarshal.ReadInt(ptr);

            if (refcnt > 1)
            {
                this.Strengthen(obj);
            }
            else
            {
                this.Weaken(obj);
            }
        }
        LogMappingInfo(object id_)
        {
            long id = (long)id_;

            if (this.id2ptr.ContainsKey(id))
            {
                IntPtr ptr = this.id2ptr[id];
                Console.WriteLine("object for id {0} is stored at {1}; refcount is {2}",
                                  id, ptr.ToString("x"), CPyMarshal.ReadInt(ptr));
                if (this.id2obj.ContainsKey(id))
                {
                    Console.WriteLine("object is simply mapped");
                    Console.WriteLine(PythonCalls.Call(Builtin.str, new object[] { this.id2obj[id] }));
                }
                else if (this.id2wref.ContainsKey(id))
                {
                    this.UpdateStrength(ptr);
                    Console.WriteLine("object is cleverly mapped");
                    if (this.id2sref.ContainsKey(id))
                    {
                        Console.WriteLine("object is being kept alive");
                    }
                    else
                    {
                        Console.WriteLine("object is at GC's mercy");
                    }
                }
                else
                {
                    Console.WriteLine("hm, object has been lost somewhere");
                }
            }
            else
            {
                Console.WriteLine("{0} is not mapped", id);
            }
        }
Exemple #5
0
        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);
            }
        }
        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)));
            }
        }
Exemple #7
0
        ReadIntField(IntPtr addr, Type type, string field)
        {
            IntPtr readAddr = CPyMarshal.GetField(addr, type, field);

            return(CPyMarshal.ReadInt(readAddr));
        }