PyObject_Hash() private method

private PyObject_Hash ( IntPtr op ) : IntPtr
op IntPtr
return IntPtr
Esempio n. 1
0
        /// <summary>
        /// MethodBinding  __hash__ implementation.
        /// </summary>
        public static IntPtr tp_hash(IntPtr ob)
        {
            var  self = (MethodBinding)GetManagedObject(ob);
            long x    = 0;
            long y    = 0;

            if (self.target != IntPtr.Zero)
            {
                x = Runtime.PyObject_Hash(self.target).ToInt64();
                if (x == -1)
                {
                    return(new IntPtr(-1));
                }
            }

            y = Runtime.PyObject_Hash(self.m.pyHandle).ToInt64();
            if (y == -1)
            {
                return(new IntPtr(-1));
            }

            x ^= y;

            if (x == -1)
            {
                x = -1;
            }

            return(new IntPtr(x));
        }
Esempio n. 2
0
        //====================================================================
        // Remove the given Python object event handler.
        //====================================================================

        internal bool RemoveEventHandler(IntPtr target, IntPtr handler)
        {
            Object obj = null;

            if (target != IntPtr.Zero)
            {
                CLRObject co = (CLRObject)ManagedType.GetManagedObject(target);
                obj = co.inst;
            }

            IntPtr hash = Runtime.PyObject_Hash(handler);

            if (Exceptions.ErrorOccurred() || (reg == null))
            {
                Exceptions.SetError(Exceptions.ValueError,
                                    "unknown event handler"
                                    );
                return(false);
            }

            object    key  = (obj != null) ? obj : this.info.ReflectedType;
            ArrayList list = reg[key] as ArrayList;

            if (list == null)
            {
                Exceptions.SetError(Exceptions.ValueError,
                                    "unknown event handler"
                                    );
                return(false);
            }

            object[]   args = { null };
            MethodInfo mi   = this.info.GetRemoveMethod(true);

            for (int i = 0; i < list.Count; i++)
            {
                Handler item = (Handler)list[i];
                if (item.hash != hash)
                {
                    continue;
                }
                args[0] = item.del;
                try
                {
                    mi.Invoke(obj, BindingFlags.Default, null, args, null);
                }
                catch
                {
                    continue;
                }
                list.RemoveAt(i);
                return(true);
            }

            Exceptions.SetError(Exceptions.ValueError,
                                "unknown event handler"
                                );
            return(false);
        }
        //====================================================================
        // Register a new Python object event handler with the event.
        //====================================================================

        internal bool AddEventHandler(IntPtr target, IntPtr handler)
        {
            Object obj = null;

            if (target != IntPtr.Zero)
            {
                CLRObject co = (CLRObject)ManagedType.GetManagedObject(target);
                obj = co.inst;
            }

            // Create a true delegate instance of the appropriate type to
            // wrap the Python handler. Note that wrapper delegate creation
            // always succeeds, though calling the wrapper may fail.

            Type     type = this.info.EventHandlerType;
            Delegate d    = PythonEngine.DelegateManager.GetDelegate(type, handler);

            // Now register the handler in a mapping from instance to pairs
            // of (handler hash, delegate) so we can lookup to remove later.
            // All this is done lazily to avoid overhead until an event is
            // actually subscribed to by a Python event handler.

            if (reg == null)
            {
                reg = new Hashtable();
            }
            object    key  = (obj != null) ? obj : this.info.ReflectedType;
            ArrayList list = reg[key] as ArrayList;

            if (list == null)
            {
                list     = new ArrayList();
                reg[key] = list;
            }
            list.Add(new Handler(Runtime.PyObject_Hash(handler), d));

            // Note that AddEventHandler helper only works for public events,
            // so we have to get the underlying add method explicitly.

            object[]   args = { d };
            MethodInfo mi   = this.info.GetAddMethod(true);

            mi.Invoke(obj, BindingFlags.Default, null, args, null);

            return(true);
        }
Esempio n. 4
0
        /// <summary>
        /// MethodBinding  __hash__ implementation.
        /// </summary>
        public static nint tp_hash(IntPtr ob)
        {
            var  self = (MethodBinding)GetManagedObject(ob);
            nint x    = 0;

            if (self.target != IntPtr.Zero)
            {
                x = Runtime.PyObject_Hash(self.target);
                if (x == -1)
                {
                    return(x);
                }
            }

            nint y = Runtime.PyObject_Hash(self.m.pyHandle);

            if (y == -1)
            {
                return(y);
            }

            return(x ^ y);
        }
Esempio n. 5
0
 /// <summary>
 /// GetHashCode Method
 /// </summary>
 /// <remarks>
 /// Return a hashcode based on the Python object. This returns the
 /// hash as computed by Python, equivalent to the Python expression
 /// "hash(obj)".
 /// </remarks>
 public override int GetHashCode()
 {
     return(Runtime.PyObject_Hash(obj).ToInt32());
 }
Esempio n. 6
0
 /// <summary>
 /// GetHashCode Method
 /// </summary>
 /// <remarks>
 /// Return a hashcode based on the Python object. This returns the
 /// hash as computed by Python, equivalent to the Python expression
 /// "hash(obj)".
 /// </remarks>
 public override int GetHashCode()
 {
     return(((ulong)Runtime.PyObject_Hash(obj)).GetHashCode());
 }