Ejemplo n.º 1
0
        public override bool Remove(ref DictionaryStorage storage, object key)
        {
            string strKey = key as string;

            if (strKey != null)
            {
                return(_data.Remove(SymbolTable.StringToId(strKey)));
            }
            else
            {
                return(_data.RemoveObjectKey(key));
            }
        }
Ejemplo n.º 2
0
        public override bool TryGetValue(object key, out object value)
        {
            string strKey = key as string;

            if (strKey != null)
            {
                return(_data.TryGetValue(SymbolTable.StringToId(strKey), out value));
            }
            else
            {
                return(_data.TryGetObjectValue(key, out value));
            }
        }
Ejemplo n.º 3
0
        public override void Add(ref DictionaryStorage storage, object key, object value)
        {
            string strKey = key as string;

            if (strKey != null)
            {
                _data[SymbolTable.StringToId(strKey)] = value;
            }
            else
            {
                _data.AddObjectKey(key, value);
            }
        }
Ejemplo n.º 4
0
        public override bool Contains(object key)
        {
            string strKey = key as string;

            if (strKey != null)
            {
                return(_data.ContainsKey(SymbolTable.StringToId(strKey)));
            }
            else
            {
                return(_data.ContainsObjectKey(key));
            }
        }
Ejemplo n.º 5
0
        public override Dict GetAttrDict(ICallerContext context, object self)
        {
            List attrs = GetAttrNames(context, self);

            Dict res = new Dict();

            foreach (string o in attrs)
            {
                res[o] = GetAttr(context, self, SymbolTable.StringToId(o));
            }

            return(res);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        public override void AddNoLock(object key, object value)
        {
            _hidden.Remove(key);

            string strKey = key as string;

            if (strKey != null)
            {
                _data[SymbolTable.StringToId(strKey)] = value;
            }
            else
            {
                _data.AddObjectKey(key, value);
            }
        }
Ejemplo n.º 8
0
        public override bool Remove(object key)
        {
            if (_hidden.Contains(key))
            {
                return(false);
            }
            string strKey = key as string;

            if (strKey != null)
            {
                return(_data.Remove(SymbolTable.StringToId(strKey)));
            }
            else
            {
                return(_data.RemoveObjectKey(key));
            }
        }
Ejemplo n.º 9
0
        public override bool TryGetValue(object key, out object value)
        {
            if (_hidden.Contains(key))
            {
                value = null;
                return(false);
            }

            string strKey = key as string;

            if (strKey != null)
            {
                return(_data.TryGetValue(SymbolTable.StringToId(strKey), out value));
            }

            return(_data.TryGetObjectValue(key, out value));
        }
Ejemplo n.º 10
0
        public static object DelAttrMethod(object self, object name)
        {
            string strName = name as string;

            if (strName == null)
            {
                throw Ops.TypeError("attribute name must be a string");
            }

            ISuperDynamicObject s = self as ISuperDynamicObject;

            if (s == null)
            {
                throw new NotImplementedException();
            }
            ((UserType)s.GetDynamicType()).DelAttr(DefaultContext.Default, s, SymbolTable.StringToId(strName));
            return(null);
        }
Ejemplo n.º 11
0
        public object GetGlobal(string name)
        {
            object ret;

            if (f_globals.TryGetValue(name, out ret))
            {
                return(ret);
            }

            // In theory, we need to check if "__builtins__" has been set by the user
            // to some custom module. However, we do not do that for perf reasons.
            if (__builtin__.TryGetAttr(this, SymbolTable.StringToId(name), out ret))
            {
                return(ret);
            }

            throw Ops.NameError("name '{0}' not defined", name);
        }
Ejemplo n.º 12
0
        private void PromoteFunctionsToMethods()
        {
            List <KeyValuePair <SymbolId, object> > updates = new List <KeyValuePair <SymbolId, object> >(__dict__.Count);

            foreach (KeyValuePair <object, object> item in __dict__)
            {
                PythonFunction func = item.Value as PythonFunction;
                if (func != null)
                {
                    SymbolId key = SymbolTable.StringToId(item.Key as string);
                    updates.Add(new KeyValuePair <SymbolId, object>(key, new Method(func, null, this)));
                }
            }

            for (int i = 0; i < updates.Count; i++)
            {
                __dict__[updates[i].Key] = updates[i].Value;
            }
        }
Ejemplo n.º 13
0
        private static object ImportTopFrom(PythonModule mod, string name)
        {
            string baseName;
            List   path = ResolveSearchPath(mod, out baseName);

            if (path == null)
            {
                return(null);
            }

            object ret = ImportFromPath(mod.SystemState, name, CreateFullName(baseName, name), path);

            if (ret != null)
            {
                mod.SetAttr(mod, SymbolTable.StringToId(name), ret);
                return(ret);
            }
            return(null);
        }
Ejemplo n.º 14
0
        static IEnumerable <PropertyDescriptor> GetPropertiesImpl(object self)
        {
            List attrNames = Ops.GetAttrNames(DefaultContext.Default, self);

            if (attrNames != null)
            {
                foreach (object o in attrNames)
                {
                    string s = o as string;
                    if (s == null)
                    {
                        continue;
                    }
                    object attrVal  = Ops.GetAttr(DefaultContext.Default, self, SymbolTable.StringToId(s));
                    Type   attrType = (attrVal == null) ? typeof(NoneType) : attrVal.GetType();
                    yield return(new SuperDynamicObjectPropertyDescriptor(
                                     s,
                                     attrType,
                                     self.GetType()));
                }
            }
        }
Ejemplo n.º 15
0
        private void AddReflectedUnboundMethod(MethodInfo mi)
        {
            if (!mi.IsStatic)
            {
                return;
            }

            string   name;
            NameType nt = NameConverter.TryGetName(this, mi, out name);

            if (nt == NameType.None)
            {
                return;
            }

            FunctionType funcType = FunctionType.Method;

            if (mi.DeclaringType == typeof(ArrayOps))
            {
                funcType |= FunctionType.SkipThisCheck;
            }
            if (nt == NameType.PythonMethod)
            {
                funcType |= FunctionType.PythonVisible;
            }

            RemoveNonOps(SymbolTable.StringToId(name));

            // store Python version
            StoreMethod <ReflectedUnboundMethod>(name, mi, funcType | FunctionType.OpsFunction);

            // store CLR version, if different and we don't have a clash (if we do
            // have a clash our version is still available under the python name)
            if (name != mi.Name && !ContainsNonOps(SymbolTable.StringToId(mi.Name)))
            {
                StoreMethod <ReflectedUnboundMethod>(mi.Name, mi, FunctionType.Method | FunctionType.OpsFunction);
            }
        }
Ejemplo n.º 16
0
        internal ReflectedPackage GetOrMakePackage(SystemState state, string fullName, string name)
        {
            object ret;

            if (__dict__.TryGetValue(SymbolTable.StringToId(name), out ret))
            {
                // if it's not a module we'll wipe it out below, eg def System(): pass then
                // import System will result in the namespace being visible.
                PythonModule     pm = ret as PythonModule;
                ReflectedPackage res;
                do
                {
                    res = pm.InnerModule as ReflectedPackage;
                    if (res != null)
                    {
                        return(res);
                    }

                    pm = pm.InnerModule as PythonModule;
                } while (pm != null);
            }

            return(MakePackage(state, fullName, name));
        }
Ejemplo n.º 17
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;
                }
            }
        }
Ejemplo n.º 18
0
        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);
                }
            }
        }
Ejemplo n.º 19
0
 public override object GetValue(object component)
 {
     return(Ops.GetAttr(DefaultContext.Default, component, SymbolTable.StringToId(_name)));
 }
Ejemplo n.º 20
0
        public static object GetAttributeMethod(object self, object name)
        {
            string strName = name as string;

            if (strName == null)
            {
                throw Ops.TypeError("attribute name must be a string");
            }

            ISuperDynamicObject s = self as ISuperDynamicObject;

            if (s != null)
            {
                object ret;
                if (((UserType)s.GetDynamicType()).TryBaseGetAttr(DefaultContext.Default, s, SymbolTable.StringToId(strName), out ret))
                {
                    return(ret);
                }
            }
            throw Ops.AttributeError("no attribute {0}", name); //??? better message
        }
Ejemplo n.º 21
0
 public override void SetValue(object component, object value)
 {
     Ops.SetAttr(DefaultContext.Default, component, SymbolTable.StringToId(_name), value);
 }
Ejemplo n.º 22
0
        public virtual object Call(object[] args, string[] names)
        {
            // we allow kw-arg binding to ctor's of arbitrary CLS types, but
            // NOT Python built-in types.  After the ctor succeeds we'll set the kw args as
            // arbitrary properties on the CLS type.  If this ends up being a built-in type we'll
            // do the check when we're going to set the kw-args.  This accomplishes 2 things:
            //      1. Our error messages match CPython more closely
            //      2. The attribute lookup is done lazily only if kw-args are supplied to a ctor

            KwArgBinder            kwArgBinder     = new KwArgBinder(args, names, targets[0].IsConstructor);
            MethodBinding          bestBinding     = new MethodBinding();
            List <UnboundArgument> bestUnboundArgs = null;

            for (int i = 0; i < targets.Length; i++)
            {
                object[] realArgs = kwArgBinder.DoBind(targets[i], Name);

                if (realArgs != null)
                {
                    MethodBinding mb = new MethodBinding();
                    mb.method = targets[i];

                    if (!(targets[i].IsStatic || targets[i].IsConstructor))
                    {
                        if (!HasInstance)
                        {
                            if (realArgs.Length == 0)
                            {
                                throw Ops.TypeError("bad number of arguments for function {0}", targets[0].Name);
                            }
                            mb.instance  = realArgs[0];
                            mb.arguments = new object[realArgs.Length - 1];
                            Array.Copy(realArgs, mb.arguments, realArgs.Length - 1);
                        }
                        else
                        {
                            mb.instance  = Instance;
                            mb.arguments = realArgs;
                        }
                    }
                    else
                    {
                        mb.arguments = realArgs;
                    }

                    if (!kwArgBinder.AllowUnboundArgs)
                    {
                        // we can have no better bindings!
                        bestBinding = mb;
                        break;
                    }

                    if (bestBinding.method == null ||
                        (kwArgBinder.UnboundArgs == null ||
                         (bestUnboundArgs != null && bestUnboundArgs.Count > kwArgBinder.UnboundArgs.Count)))
                    {
                        bestBinding     = mb;
                        bestUnboundArgs = kwArgBinder.UnboundArgs;
                    }
                }
            }

            if (bestBinding.method != null)
            {
                // we've bound the arguments to a real method,
                // finally we're going to dispatch back to the
                // optimized version of the calls.
                object ret = Call(bestBinding.arguments);

                // any unbound arguments left over we assume the user
                // wants to do a property set with.  We'll go ahead and try
                // that - if they fail we'll throw.
                if (bestUnboundArgs != null)
                {
                    ///!!! if we had a constructor w/ a ref param then we'll try
                    // updating the Tuple here instead of the user's object.

                    if (targets[0].DeclaringType.IsDefined(typeof(PythonTypeAttribute), false))
                    {
                        throw Ops.TypeError("'{0}' is an invalid keyword argument for this function",
                                            bestUnboundArgs[0].Name,
                                            Name);
                    }

                    for (int j = 0; j < bestUnboundArgs.Count; j++)
                    {
                        Ops.SetAttr(DefaultContext.Default, ret,
                                    SymbolTable.StringToId(bestUnboundArgs[j].Name),
                                    bestUnboundArgs[j].Value);
                    }
                }

                return(ret);
            }

            if (kwArgBinder.GetError() != null)
            {
                throw kwArgBinder.GetError();
            }

            throw Ops.TypeError("bad number of arguments for function {0}", FriendlyName);
        }
Ejemplo n.º 23
0
 public override void ResetValue(object component)
 {
     Ops.DelAttr(DefaultContext.Default, component, SymbolTable.StringToId(_name));
 }
Ejemplo n.º 24
0
            public override bool ShouldSerializeValue(object component)
            {
                object o;

                return(Ops.TryGetAttr(component, SymbolTable.StringToId(_name), out o));
            }