Esempio n. 1
0
        private PythonType CheckGetArgs(CodeContext context, object instance, PythonType owner)
        {
            if (owner == null)
            {
                if (instance == null)
                {
                    throw PythonOps.TypeError("__get__(None, None) is invalid");
                }
                owner = DynamicHelpers.GetPythonType(instance);
            }
            else
            {
                if (!owner.IsSubclassOf(DynamicHelpers.GetPythonTypeFromType(_func.DeclaringType)))
                {
                    throw PythonOps.TypeError("descriptor {0} for type {1} doesn't apply to type {2}",
                                              PythonOps.Repr(context, _func.Name),
                                              PythonOps.Repr(context, DynamicHelpers.GetPythonTypeFromType(_func.DeclaringType).Name),
                                              PythonOps.Repr(context, owner.Name));
                }
            }
            if (instance != null)
            {
                BuiltinMethodDescriptor.CheckSelfWorker(context, instance, _func);
            }

            return(owner);
        }
Esempio n. 2
0
 public void __init__(PythonType type, object obj)
 {
     if (obj != null)
     {
         PythonType dt = obj as PythonType;
         if (PythonOps.IsInstance(obj, type))
         {
             this._thisClass = type;
             this._self      = obj;
             this._selfClass = DynamicHelpers.GetPythonType(obj);
         }
         else if (dt != null && dt.IsSubclassOf(type))
         {
             this._thisClass = type;
             this._selfClass = obj;
             this._self      = obj;
         }
         else
         {
             throw PythonOps.TypeError("super(type, obj): obj must be an instance or subtype of type {1}, not {0}", PythonTypeOps.GetName(obj), type.Name);
         }
     }
     else
     {
         this._thisClass = type;
         this._self      = null;
         this._selfClass = null;
     }
 }
Esempio n. 3
0
            private void ValidateArgs(object[] args)
            {
                int start = 0;

                if (_inst != null)
                {
                    start = 1;
                }


                // no need to validate self... the method should handle it.
                for (int i = start; i < args.Length + start; i++)
                {
                    PythonType dt = DynamicHelpers.GetPythonType(args[i - start]);

                    PythonType expct = _expected[i] as PythonType;
                    if (expct == null)
                    {
                        expct = ((OldClass)_expected[i]).TypeObject;
                    }
                    if (dt != _expected[i] && !dt.IsSubclassOf(expct))
                    {
                        throw PythonOps.AssertionError("argument {0} has bad value (got {1}, expected {2})", i, dt, _expected[i]);
                    }
                }
            }
Esempio n. 4
0
        public static PythonModule /*!*/ __new__(CodeContext /*!*/ context, PythonType /*!*/ cls, params object[] /*!*/ args\u00F8)
        {
            PythonModule res;

            if (cls == TypeCache.Module)
            {
                res = new PythonModule();
            }
            else if (cls.IsSubclassOf(TypeCache.Module))
            {
                res = (PythonModule)cls.CreateInstance(context);
            }
            else
            {
                throw PythonOps.TypeError("{0} is not a subtype of module", cls.Name);
            }

            return(res);
        }
Esempio n. 5
0
            private void ValidateReturn(object ret)
            {
                // we return void...
                if (ret == null && _retType == null)
                {
                    return;
                }

                PythonType dt = DynamicHelpers.GetPythonType(ret);

                if (dt != _retType)
                {
                    PythonType expct = _retType as PythonType;
                    if (expct == null)
                    {
                        expct = ((OldClass)_retType).TypeObject;
                    }

                    if (!dt.IsSubclassOf(expct))
                    {
                        throw PythonOps.AssertionError("bad return value returned (expected {0}, got {1})", _retType, dt);
                    }
                }
            }
Esempio n. 6
0
        private void CheckSelf(object self)
        {
            // if the type has been re-optimized (we also have base type info in here)
            // then we can't do the type checks right now :(.
            if ((template.FunctionType & FunctionType.SkipThisCheck) != 0)
            {
                return;
            }

            if ((template.FunctionType & FunctionType.FunctionMethodMask) == FunctionType.Method)
            {
                // to a fast check on the CLR types, if they match we can avoid the slower
                // check that involves looking up dynamic types.
                if (self.GetType() == template.ClrDeclaringType)
                {
                    return;
                }

                PythonType selfType = Ops.GetDynamicTypeFromType(self.GetType()) as PythonType;
                Debug.Assert(selfType != null);

                ReflectedType declType = DeclaringType;
                if (!selfType.IsSubclassOf(declType))
                {
                    // if a conversion exists to the type allow the call.
                    Conversion conv;
                    if (Converter.TryConvert(self, declType.type, out conv) == null)
                    {
                        throw Ops.TypeError("descriptor {0} requires a {1} object but received a {2}",
                                            Ops.Repr(Name),
                                            Ops.Repr(DeclaringType.__name__),
                                            Ops.Repr(Ops.GetDynamicType(self).__name__));
                    }
                }
            }
        }