Beispiel #1
0
        public bool TryGetAttr(ICallerContext context, SymbolId name, out object value)
        {
            if (name == SymbolTable.Dict)
            {
                value = EnsureDict();
                return(true);
            }

            if (dict != null)
            {
                if (dict.TryGetValue(name, out value))
                {
                    return(true);
                }
            }

            // We check for SymbolTable.Module as the user code can modify it
            if (name == SymbolTable.Module)
            {
                value = Module.ModuleName;
                return(true);
            }

            return(GetDynamicType().TryGetAttr(context, this, name, out value));
        }
        public bool TryGetAttr(ICallerContext context, SymbolId name, out object value)
        {
            if (name == SymbolTable.Dict)
            {
                value = __dict__;
                return(true);
            }

            if (TypeCache.SystemState.TryGetAttr(context, this, name, out value))
            {
                return(true);
            }
            return(__dict__.TryGetValue(name, out value));
        }
Beispiel #3
0
        internal bool TryGetSlot(ICallerContext context, SymbolId name, out object ret)
        {
            Initialize();

            switch (name.Id)
            {
            case SymbolTable.DictId: ret = dict; return(true);

            case SymbolTable.GetAttributeId:  ret = __getattribute__F;  return(true);

            case SymbolTable.GetAttrId: ret = __getattr__F; return(true);

            case SymbolTable.SetAttrId: ret = __setattr__F; return(true);

            case SymbolTable.DelAttrId: ret = __delattr__F; return(true);

            case SymbolTable.ReprId: ret = __repr__F; return(true);

            case SymbolTable.StringId: ret = __str__F; return(true);

            case SymbolTable.CmpId:
                if (!__cmp__F.IsSuperTypeMethod())
                {
                    ret = __cmp__F;
                    return(true);
                }
                break;

            case SymbolTable.MethodResolutionOrderId:
                if (methodResolutionOrder == null)
                {
                    methodResolutionOrder = CalculateMro(BaseClasses);
                }
                ret = methodResolutionOrder;
                return(true);
            }

            if (dict.TryGetValue(name, out ret))
            {
                IContextAwareMember icam = ret as IContextAwareMember;
                if (icam != null && !icam.IsVisible(context))
                {
                    ret = null;
                    return(false);
                }
                return(true);
            }
            return(false);
        }
Beispiel #4
0
        private bool TryRawGetAttr(SymbolId name, out object ret)
        {
            if (__dict__.TryGetValue(name, out ret))
            {
                return(true);
            }

            if (__class__.TryLookupSlot(name, out ret))
            {
                ret = Ops.GetDescriptor(ret, this, __class__);
                return(true);
            }

            return(false);
        }
Beispiel #5
0
        public static WeakRefTracker GetWeakRefHelper(object self)
        {
            ISuperDynamicObject sdo = self as ISuperDynamicObject;

            if (sdo != null)
            {
                IAttributesDictionary dict = sdo.GetDict();
                if (dict != null)
                {
                    object res;
                    if (dict.TryGetValue(SymbolTable.WeakRef, out res))
                    {
                        return(res as WeakRefTracker);
                    }
                }
            }
            return(null);
        }
Beispiel #6
0
        public bool TryLookupSlot(SymbolId name, out object ret)
        {
            if (__dict__.TryGetValue(name, out ret))
            {
                return(true);
            }

            // bases only ever contains OldClasses (tuples are immutable, and when assigned
            // to we verify the types in the Tuple)
            foreach (OldClass c in __bases__)
            {
                if (c.TryLookupSlot(name, out ret))
                {
                    return(true);
                }
            }
            ret = null;
            return(false);
        }
Beispiel #7
0
        //!!! private would be better, only called from PythonType
        internal bool TryBaseGetAttr(ICallerContext context, ISuperDynamicObject self, SymbolId name, out object ret)
        {
            if (name == SymbolTable.Dict)
            {
                ret = GetInitializedDict(self);
                return(true);
            }

            IAttributesDictionary d = self.GetDict();

            if (d != null)
            {
                if (d.TryGetValue(name, out ret))
                {
                    return(true);
                }
            }

            if (TryLookupSlot(context, name, out ret))
            {
                ret = Ops.GetDescriptor(ret, self, this);
                return(true);
            }

            if (name == SymbolTable.Class)
            {
                ret = this; return(true);
            }
            if (name == SymbolTable.WeakRef)
            {
                ret = null; return(true);
            }

            if (!__getattr__F.IsObjectMethod())
            {
                ret = __getattr__F.Invoke(self, SymbolTable.IdToString(name));
                return(true);
            }

            ret = null;
            return(false);
        }
 public bool TryGetAttr(ICallerContext context, SymbolId name, out object value)
 {
     if (__dict__.TryGetValue(name, out value))
     {
         IContextAwareMember icaa = value as IContextAwareMember;
         if (icaa == null || icaa.IsVisible(context))
         {
             return(true);
         }
         value = null;
     }
     if (name == SymbolTable.Dict)
     {
         value = __dict__; return(true);
     }
     if (packageImported)
     {
         return(innerMod.TryGetAttr(context, name, out value));
     }
     return(false);
 }
        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);
                }
            }
        }
        internal bool DeleteAttrWithCustomDict(ICallerContext context, ICustomAttributes self, IAttributesDictionary selfDict, SymbolId name)
        {
            Debug.Assert(IsInstanceOfType(self));

            if (name == SymbolTable.Dict)
                throw Ops.AttributeErrorForReadonlyAttribute(__name__.ToString(), name);

            object value;
            if (selfDict.TryGetValue(name, out value)) {
                if (value == Uninitialized.instance) return false;

                selfDict.Remove(name);
                return true;
            }

            object dummy;
            if (TryLookupSlot(context, name, out dummy)) {
                selfDict[name] = Uninitialized.instance;
                return true;
            } else {
                return selfDict.Remove(name);
            }
        }
Beispiel #11
0
 /// <summary>
 /// called from generated code for overridden methods
 /// </summary>
 public bool TryGetNonInheritedMethodHelper(object instance, IAttributesDictionary instanceDict, SymbolId name, int key, out object value)
 {
     if (instanceDict != null) {
         if (instanceDict.TryGetValue(name, out value)) return true;
     }
     if (((NamespaceDictionary)dict).TryGetNonInheritedValue(key, out value)) {
         value = Ops.GetDescriptor(value, instance, this);
         return true;
     }
     return false;
 }