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); } } }
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); }
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; }
private static IAttributesDictionary GetInitializedDict(ISuperDynamicObject self) { IAttributesDictionary d = self.GetDict(); if (d == null) { d = new FieldIdDict(); self.SetDict(d); } return(d); }
/// <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()); }
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); }
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); }
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); }
//!!! 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 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 }
//!!! 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; }
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); }
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); }
private static IAttributesDictionary GetInitializedDict(ISuperDynamicObject self) { IAttributesDictionary d = self.GetDict(); if (d == null) { d = new FieldIdDict(); if (!self.SetDict(d)) return null; } return d; }
/// <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(); }