PyObject_IsInstance() private méthode

private PyObject_IsInstance ( IntPtr ob, IntPtr type ) : int
ob IntPtr
type IntPtr
Résultat int
        //====================================================================
        // Descriptor __get__ implementation. A getattr on an event returns
        // a "bound" event that keeps a reference to the object instance.
        //====================================================================

        public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
        {
            EventObject  self = GetManagedObject(ds) as EventObject;
            EventBinding binding;

            if (self == null)
            {
                return(Exceptions.RaiseTypeError("invalid argument"));
            }

            // If the event is accessed through its type (rather than via
            // an instance) we return an 'unbound' EventBinding that will
            // be cached for future accesses through the type.

            if (ob == IntPtr.Zero)
            {
                if (self.unbound == null)
                {
                    self.unbound = new EventBinding(self, IntPtr.Zero);
                }
                binding = self.unbound;
                Runtime.Incref(binding.pyHandle);
                return(binding.pyHandle);
            }

            if (Runtime.PyObject_IsInstance(ob, tp) < 1)
            {
                return(Exceptions.RaiseTypeError("invalid argument"));
            }

            binding = new EventBinding(self, ob);
            return(binding.pyHandle);
        }
Exemple #2
0
        /// <summary>
        /// IsInstance Method
        /// </summary>
        /// <remarks>
        /// Return true if the object is an instance of the given Python type
        /// or class. This method always succeeds.
        /// </remarks>
        public bool IsInstance(PyObject typeOrClass)
        {
            int r = Runtime.PyObject_IsInstance(obj, typeOrClass.obj);

            if (r < 0)
            {
                Runtime.PyErr_Clear();
                return(false);
            }
            return(r != 0);
        }
Exemple #3
0
        /// <summary>
        /// IsInstance Method
        /// </summary>
        /// <remarks>
        /// Return true if the object is an instance of the given Python type
        /// or class. This method always succeeds.
        /// </remarks>
        public bool IsInstance(PyObject typeOrClass)
        {
            if (typeOrClass == null)
            {
                throw new ArgumentNullException(nameof(typeOrClass));
            }

            int r = Runtime.PyObject_IsInstance(obj, typeOrClass.obj);

            if (r < 0)
            {
                Runtime.PyErr_Clear();
                return(false);
            }
            return(r != 0);
        }
Exemple #4
0
        /// <summary>
        /// Descriptor __get__ implementation. Accessing a CLR method returns
        /// a "bound" method similar to a Python bound method.
        /// </summary>
        public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
        {
            var           self = (MethodObject)GetManagedObject(ds);
            MethodBinding binding;

            // If the method is accessed through its type (rather than via
            // an instance) we return an 'unbound' MethodBinding that will
            // cached for future accesses through the type.

            if (ob == IntPtr.Zero)
            {
                if (self.unbound == null)
                {
                    self.unbound = new MethodBinding(self, IntPtr.Zero, tp);
                }
                binding = self.unbound;
                Runtime.XIncref(binding.pyHandle);
                ;
                return(binding.pyHandle);
            }

            if (Runtime.PyObject_IsInstance(ob, tp) < 1)
            {
                return(Exceptions.RaiseTypeError("invalid argument"));
            }

            // If the object this descriptor is being called with is a subclass of the type
            // this descriptor was defined on then it will be because the base class method
            // is being called via super(Derived, self).method(...).
            // In which case create a MethodBinding bound to the base class.
            var obj = GetManagedObject(ob) as CLRObject;

            if (obj != null &&
                obj.inst.GetType() != self.type &&
                obj.inst is IPythonDerivedType &&
                self.type.IsInstanceOfType(obj.inst))
            {
                ClassBase basecls = ClassManager.GetClass(self.type);
                binding = new MethodBinding(self, ob, basecls.pyHandle);
                return(binding.pyHandle);
            }

            binding = new MethodBinding(self, ob, tp);
            return(binding.pyHandle);
        }
Exemple #5
0
        /// <summary>
        /// Descriptor __get__ implementation. A getattr on an event returns
        /// a "bound" event that keeps a reference to the object instance.
        /// </summary>
        public static NewReference tp_descr_get(BorrowedReference ds, BorrowedReference ob, BorrowedReference tp)
        {
            var self = GetManagedObject(ds) as EventObject;

            if (self == null)
            {
                return(Exceptions.RaiseTypeError("invalid argument"));
            }

            if (ob == null)
            {
                return(new NewReference(ds));
            }

            if (Runtime.PyObject_IsInstance(ob, tp) < 1)
            {
                return(Exceptions.RaiseTypeError("invalid argument"));
            }

            return(new EventBinding(self.name, self.reg, new PyObject(ob)).Alloc());
        }
Exemple #6
0
        internal static bool ToManagedValue(IntPtr value, Type obType,
                                            out Object result, bool setError)
        {
            // Common case: if the Python value is a wrapped managed object
            // instance, just return the wrapped object.
            ManagedType mt = ManagedType.GetManagedObject(value);

            result = null;

            // XXX - hack to support objects wrapped in old-style classes
            // (such as exception objects).
            if (Runtime.wrap_exceptions)
            {
                if (mt == null)
                {
                    if (Runtime.PyObject_IsInstance(
                            value, Exceptions.Exception
                            ) > 0)
                    {
                        IntPtr p = Runtime.PyObject_GetAttrString(value, "_inner");
                        if (p != IntPtr.Zero)
                        {
                            // This is safe because we know that the __dict__ of
                            // value holds a reference to _inner.
                            value = p;
                            Runtime.Decref(p);
                            mt = ManagedType.GetManagedObject(value);
                        }
                    }
                    IntPtr c = Exceptions.UnwrapExceptionClass(value);
                    if ((c != IntPtr.Zero) && (c != value))
                    {
                        value = c;
                        Runtime.Decref(c);
                        mt = ManagedType.GetManagedObject(value);
                    }
                }
            }

            if (mt != null)
            {
                if (mt is CLRObject)
                {
                    object tmp = ((CLRObject)mt).inst;
                    if (obType.IsInstanceOfType(tmp))
                    {
                        result = tmp;
                        return(true);
                    }
                    string err = "value cannot be converted to {0}";
                    err = String.Format(err, obType);
                    Exceptions.SetError(Exceptions.TypeError, err);
                    return(false);
                }
                if (mt is ClassBase)
                {
                    result = ((ClassBase)mt).type;
                    return(true);
                }
                // shouldnt happen
                return(false);
            }

            if (value == Runtime.PyNone && !obType.IsValueType)
            {
                result = null;
                return(true);
            }

            if (obType.IsArray)
            {
                return(ToArray(value, obType, out result, setError));
            }

            if (obType.IsEnum)
            {
                return(ToEnum(value, obType, out result, setError));
            }

            // Conversion to 'Object' is done based on some reasonable
            // default conversions (Python string -> managed string,
            // Python int -> Int32 etc.).

            if (obType == objectType)
            {
                if (Runtime.IsStringType(value))
                {
                    return(ToPrimitive(value, stringType, out result,
                                       setError));
                }

                else if (Runtime.PyBool_Check(value))
                {
                    return(ToPrimitive(value, boolType, out result, setError));
                }

                else if (Runtime.PyInt_Check(value))
                {
                    return(ToPrimitive(value, int32Type, out result, setError));
                }

                else if (Runtime.PyLong_Check(value))
                {
                    return(ToPrimitive(value, int64Type, out result, setError));
                }

                else if (Runtime.PyFloat_Check(value))
                {
                    return(ToPrimitive(value, doubleType, out result, setError));
                }

                else if (Runtime.PySequence_Check(value))
                {
                    return(ToArray(value, typeof(object[]), out result,
                                   setError));
                }

                if (setError)
                {
                    Exceptions.SetError(Exceptions.TypeError,
                                        "value cannot be converted to Object"
                                        );
                }

                return(false);
            }

            // Conversion to 'Type' is done using the same mappings as above
            // for objects.

            if (obType == typeType)
            {
                if (value == Runtime.PyStringType)
                {
                    result = stringType;
                    return(true);
                }

                else if (value == Runtime.PyBoolType)
                {
                    result = boolType;
                    return(true);
                }

                else if (value == Runtime.PyIntType)
                {
                    result = int32Type;
                    return(true);
                }

                else if (value == Runtime.PyLongType)
                {
                    result = int64Type;
                    return(true);
                }

                else if (value == Runtime.PyFloatType)
                {
                    result = doubleType;
                    return(true);
                }

                else if (value == Runtime.PyListType || value == Runtime.PyTupleType)
                {
                    result = typeof(object[]);
                    return(true);
                }

                if (setError)
                {
                    Exceptions.SetError(Exceptions.TypeError,
                                        "value cannot be converted to Type"
                                        );
                }

                return(false);
            }

            return(ToPrimitive(value, obType, out result, setError));
        }