示例#1
0
 public Frame(PythonModule mod)
 {
     __module__  = mod;
     f_globals   = ((IDictionary <object, object>)mod.__dict__);
     f_locals    = mod.__dict__;
     __builtin__ = TypeCache.Builtin;
 }
示例#2
0
 public Frame(PythonModule mod, IDictionary <object, object> globals, object locals)
 {
     __module__  = mod;
     f_globals   = globals;
     f_locals    = locals;
     __builtin__ = TypeCache.Builtin;
 }
示例#3
0
        public static ReflectedType MakeDynamicType(ReflectedType integerType)
        {
            OpsReflectedType ret = new OpsReflectedType("int64", typeof(long), typeof(Int64Ops), null);

            ret.effectivePythonType = integerType;
            return(ret);
        }
        protected override void OptimizeMethod()
        {
            BuiltinFunction optimized = ReflectOptimizer.MakeFunction(this);

            if (optimized != null)
            {
                ReflectedType declType = (ReflectedType)DeclaringType;
                if (declType.dict == null)
                {
                    declType.dict = new Dict();
                }

                object    newmeth;
                NewMethod newMethAsMeth;
                if (declType.dict.TryGetValue(SymbolTable.NewInst, out newmeth) && (newMethAsMeth = newmeth as NewMethod) != null)
                {
                    newMethAsMeth.Optimize(optimized);
                }
                else
                {
                    declType.dict[SymbolTable.NewInst] = optimized;
                    declType.ctor = optimized;
                }

                optimizedTarget = optimized;
            }
        }
示例#5
0
        internal static PythonModule ReloadBuiltin(PythonModule module)
        {
            Type ty;

            if (module.SystemState.TopPackage.Builtins.TryGetValue(module.ModuleName, out ty))
            {
                if (typeof(CustomFieldIdDict).IsAssignableFrom(ty))
                {
                    CustomFieldIdDict dict = (CustomFieldIdDict)ty.GetConstructor(Type.EmptyTypes).Invoke(new object[0]);
                    //@todo share logic to copy old values in when not already there from reload
                    module.__dict__ = dict;
                    module.Initialize();
                    return(module);
                }
                else
                {
                    ReflectedType type = (ReflectedType)Ops.GetDynamicTypeFromType(ty);
                    type.Initialize();
                    foreach (string attrName in type.GetAttrNames(module))
                    {
                        SymbolId id = SymbolTable.StringToId(attrName);
                        module.__dict__[id] = type.GetAttr(module, null, id);
                    }
                    module.Initialize();
                    return(module);
                }
            }
            throw new NotImplementedException();
        }
示例#6
0
        public static ReflectedType MakeDynamicType(ReflectedType type)
        {
            if (BoolType == null)
            {
                OpsReflectedType ret = new OpsReflectedType("bool", typeof(bool), typeof(BoolOps), null, new CallTarget1(FastNew));
                if (Interlocked.CompareExchange <ReflectedType>(ref BoolType, ret, null) == null)
                {
                    return(ret);
                }
            }

            return(BoolType);
        }
示例#7
0
 public override bool IsSubclassOf(object other)
 {
     //!!! This is an ugly special case to handle the fact the Python bool extends Python int
     if (type == typeof(bool))
     {
         ReflectedType rt = other as ReflectedType;
         if (rt == null)
         {
             return(false);
         }
         return(rt.type == typeof(bool) || rt.type == typeof(int) || rt.type == typeof(object));
     }
     return(base.IsSubclassOf(other));
 }
示例#8
0
        public override bool IsSubclassOf(object other)
        {
            ReflectedType rt = other as ReflectedType;

            if (rt != null)
            {
                if (type == rt.type || type.IsSubclassOf(rt.type))
                {
                    return(true);
                }

                Type otherTypeToExtend = rt.GetTypeToExtend();
                if (otherTypeToExtend != null)
                {
                    if (type == otherTypeToExtend || type.IsSubclassOf(otherTypeToExtend))  //!!! inefficient in most cases
                    {
                        return(true);
                    }

                    foreach (Type interfaceType in type.GetInterfaces())
                    {
                        if (interfaceType == otherTypeToExtend)
                        {
                            return(true);
                        }
                    }
                }

                return(false);
            }

            if (this.Equals(other))
            {
                return(true);
            }

            foreach (DynamicType baseType in BaseClasses)
            {
                if (baseType.IsSubclassOf(other))
                {
                    return(true);
                }
            }
            return(false);
        }
示例#9
0
        /// <summary>
        /// If we have only interfaces, we'll need to insert object's base
        /// </summary>
        private static Tuple EnsureBaseType(Tuple bases)
        {
            foreach (object baseClass in bases)
            {
                if (baseClass is OldClass)
                {
                    continue;
                }

                ReflectedType reflectedBaseType = baseClass as ReflectedType;
                if (reflectedBaseType == null || !reflectedBaseType.GetTypeToExtend().IsInterface)
                {
                    // Found a concrete (non-interface) type. We are done.
                    return(bases);
                }
            }

            // We found only interfaces. We need do add System.Object to the bases
            return(new Tuple(bases, TypeCache.Object));
        }
示例#10
0
        protected virtual void OptimizeMethod()
        {
            BuiltinFunction optimized = ReflectOptimizer.MakeFunction(this);

            if (optimized != null)
            {
                Debug.Assert(optimized.Targets.Length == Targets.Length);

                ReflectedType declType = (ReflectedType)DeclaringType;
                declType.Initialize();

                object   prevVal;
                SymbolId myId = SymbolTable.StringToId(Name);
                if (declType.dict.TryGetValue(myId, out prevVal))
                {
                    ClassMethod cm;
                    if ((cm = prevVal as ClassMethod) != null)
                    {
                        cm.func = optimized;
                    }
                    else
                    {
                        if (myId == SymbolTable.NewInst)
                        {
                            declType.dict[myId] = declType.ctor = optimized;
                        }
                        else
                        {
                            declType.dict[myId] = optimized.GetDescriptor();
                        }
                    }

                    optimizedTarget = optimized;
                }
            }
        }
示例#11
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__));
                    }
                }
            }
        }
        internal void SaveType(Type type)
        {
            string name = GetCoreTypeName(type);
            object existingType;

            // if there's no collisions we just save the type
            if (!__dict__.TryGetValue(SymbolTable.StringToId(name), out existingType))
            {
                __dict__[SymbolTable.StringToId(name)] = Ops.GetDynamicTypeFromType(type);
                return;
            }

            // two types w/ the same name.  Good examples are:
            //      System.Nullable and System.Nullable<T>
            //      System.IComparable vs System.IComparable<T>
            //    In this case we need to allow the user to disambiguate the two.
            //
            // Or we could have a recompile & reload cycle (or a really bad
            // collision).  In those cases the new type wins.

            TypeCollision tc = existingType as TypeCollision;

            if (tc != null)
            {
                // we've collided before...
                if (!type.ContainsGenericParameters)
                {
                    // we're replacing the existing non generic type
                    // or moving some random generic type from the "base"
                    // reflected type into the list of generics.
                    __dict__[SymbolTable.StringToId(name)] = tc.CloneWithNewBase(type);
                }
                else
                {
                    // we're a generic type.  we just need to add
                    // ourselves to the list or replace an existing type
                    // of the same arity.
                    tc.UpdateType(type);
                }
            }
            else
            {
                // first time collision on this name, provide
                // the type collision to disambiguate.  The non-generic
                // is exposed by default, and the generic gets added
                // to the list to disambiguate.
                ReflectedType rt = existingType as ReflectedType;
                Debug.Assert(rt != null);

                if (rt.type.ContainsGenericParameters)
                {
                    // existing type has generics, append it.
                    tc = new TypeCollision(type);
                    __dict__[SymbolTable.StringToId(name)] = tc;
                    tc.UpdateType(rt.type);
                }
                else if (type.ContainsGenericParameters)
                {
                    // new type has generics, append it.
                    tc = new TypeCollision(rt.type);
                    __dict__[SymbolTable.StringToId(name)] = tc;
                    tc.UpdateType(type);
                }
                else
                {
                    // neither type has generics, replace the old
                    // non-generic type w/ the new non-generic type.
                    __dict__[SymbolTable.StringToId(name)] = Ops.GetDynamicTypeFromType(type);
                }
            }
        }
示例#13
0
        private static PythonModule MakePythonModule(PythonModule importer, string name, ReflectedType type)
        {
            type.Initialize();
            FieldIdDict dict = new FieldIdDict();

            //!!! need a GetAttrIds
            foreach (string attrName in type.GetAttrNames(DefaultContext.Default))
            {
                dict[SymbolTable.StringToId(attrName)] = type.GetAttr(DefaultContext.Default, null, SymbolTable.StringToId(attrName));
            }
            PythonModule ret = new PythonModule(name, dict, importer.SystemState);

            importer.SystemState.modules[name] = ret;
            return(ret);
        }