Ejemplo n.º 1
0
 public override bool TryGetAttr(ICallerContext context, object self, SymbolId name, out object ret)
 {
     if (__getattribute__F.IsObjectMethod())
     {
         ISuperDynamicObject sdo = self as ISuperDynamicObject;
         if (sdo != null)
         {
             return(TryBaseGetAttr(context, sdo, name, out ret));
         }
         else
         {
             ret = null;
             return(false);
         }
     }
     else
     {
         try {
             ret = __getattribute__F.Invoke(self, SymbolTable.IdToString(name));
             return(true);
         } catch (MissingMemberException) {
             ret = null;
             return(false);
         }
     }
 }
Ejemplo n.º 2
0
        public override Dict GetAttrDict(ICallerContext context, object self)
        {
            // Get the entries from the type
            Dict res = new Dict(GetAttrDict(context));

            // Add the entries from the instance
            ISuperDynamicObject sdo = self as ISuperDynamicObject;

            if (sdo != null)
            {
                IAttributesDictionary dict = sdo.GetDict();
                if (dict != null)
                {
                    foreach (KeyValuePair <object, object> val in dict)
                    {
                        object fieldName = val.Key;
                        if (!res.ContainsKey(fieldName))
                        {
                            res.Add(new KeyValuePair <object, object>(fieldName, val.Value));
                        }
                    }
                }
            }
            return(res);
        }
Ejemplo n.º 3
0
        public static void SetWeakRefHelper(object self, WeakRefTracker value)
        {
            ISuperDynamicObject sdo = self as ISuperDynamicObject;

            System.Diagnostics.Debug.Assert(sdo != null);

            IAttributesDictionary d = GetInitializedDict(sdo);

            d[SymbolTable.WeakRef] = value;
        }
Ejemplo n.º 4
0
        private static IAttributesDictionary GetInitializedDict(ISuperDynamicObject self)
        {
            IAttributesDictionary d = self.GetDict();

            if (d == null)
            {
                d = new FieldIdDict();
                self.SetDict(d);
            }
            return(d);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Object.ToString() displays the CLI type name.  But we want to display the class name (e.g.
        /// '<foo object at 0x000000000000002C>' unless we've overridden __repr__ but not __str__ in
        /// which case we'll display the result of __repr__.
        /// </summary>
        public static string ToStringHelper(ISuperDynamicObject o)
        {
            object   ret;
            UserType ut = o.GetDynamicType() as UserType;

            if (ut.TryLookupSlot(DefaultContext.Default, SymbolTable.Repr, out ret))
            {
                return(Converter.ConvertToString(Ops.Call(Ops.GetDescriptor(ret, o, ut))));
            }

            return(PythonType.ReprMethod(o).ToString());
        }
Ejemplo n.º 6
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.º 7
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);
        }
Ejemplo n.º 8
0
        public void BaseDelAttr(ICallerContext context, ISuperDynamicObject self, SymbolId name)
        {
            object slot;

            if (!TryLookupSlot(context, name, out slot))
            {
                IAttributesDictionary d = GetInitializedDict((ISuperDynamicObject)self); //!!! forces dict init
                if (d.ContainsKey(name))
                {
                    d.Remove(name);
                    return;
                }
                else
                {
                    throw Ops.AttributeError("no slot named {0} on {1}", SymbolTable.IdToString(name), this.__name__);
                }
            }
            Ops.DelDescriptor(slot, self);
        }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
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.º 11
0
        //!!! private would be better, only called from PythonType
        internal void BaseSetAttr(ICallerContext context, ISuperDynamicObject self, SymbolId name, object value)
        {
            object slot;

            if (TryLookupSlot(context, name, out slot))
            {
                if (Ops.SetDescriptor(slot, self, value))
                {
                    return;
                }
            }

            if (name == SymbolTable.WeakRef)
            {
                throw Ops.TypeError("attribute '__weakref__' of '{0}' objects is not writable", __name__);
            }

            IAttributesDictionary d = GetInitializedDict(self);

            d[name] = value;
        }
Ejemplo n.º 12
0
        public override List GetAttrNames(ICallerContext context, object self)
        {
            List baseNames = base.GetAttrNames(context, self);

            ISuperDynamicObject sdo = self as ISuperDynamicObject;

            if (sdo != null)
            {
                IAttributesDictionary dict = sdo.GetDict();
                if (dict != null)
                {
                    foreach (object o in dict.Keys)
                    {
                        if (!baseNames.Contains(o))
                        {
                            baseNames.Add(o);
                        }
                    }
                }
            }

            return(baseNames);
        }
Ejemplo n.º 13
0
        public override List GetAttrNames(ICallerContext context, object self)
        {
            // Get the entries from the type
            List ret = GetAttrNames(context);

            // Add the entries from the instance
            ISuperDynamicObject sdo = self as ISuperDynamicObject;

            if (sdo != null)
            {
                if (sdo.GetDict() != null)
                {
                    ICollection <object> keys = sdo.GetDict().Keys;
                    foreach (object key in keys)
                    {
                        if (!ret.Contains(key))
                        {
                            ret.AddNoLock(key);
                        }
                    }
                }
            }
            return(ret);
        }
Ejemplo n.º 14
0
 private static IAttributesDictionary GetInitializedDict(ISuperDynamicObject self)
 {
     IAttributesDictionary d = self.GetDict();
     if (d == null) {
         d = new FieldIdDict();
         if (!self.SetDict(d)) return null;
     }
     return d;
 }
Ejemplo n.º 15
0
        /// <summary>
        /// Object.ToString() displays the CLI type name.  But we want to display the class name (e.g.
        /// '<foo object at 0x000000000000002C>' unless we've overridden __repr__ but not __str__ in 
        /// which case we'll display the result of __repr__.
        /// </summary>
        public static string ToStringHelper(ISuperDynamicObject o)
        {
            object ret;
            UserType ut = o.GetDynamicType() as UserType;
            if (ut.TryLookupBoundSlot(DefaultContext.Default, o, SymbolTable.Repr, out ret)) {
                string strRet;
                if (ret != null && Converter.TryConvertToString(Ops.Call(Ops.GetDescriptor(ret, o, ut)), out strRet)) return strRet;
                throw Ops.TypeError("__repr__ returned non-string type ({0})", Ops.GetDynamicType(ret).__name__);
            }

            return DynamicType.ReprMethod(o).ToString();
        }