Inheritance: ICustomTypeDescriptor, ICodeFormattable, IMembersList, IDynamicMetaObjectProvider, IPythonMembersList, IWeakReferenceable
示例#1
0
        internal bool IsSubclassOf(object other)
        {
            if (this == other)
            {
                return(true);
            }

            OldClass dt = other as OldClass;

            if (dt == null)
            {
                return(false);
            }

            List <OldClass> bases = _bases;

            foreach (OldClass bc in bases)
            {
                if (bc.IsSubclassOf(other))
                {
                    return(true);
                }
            }
            return(false);
        }
示例#2
0
        private List <OldClass> ValidateBases(object value)
        {
            PythonTuple t = value as PythonTuple;

            if (t == null)
            {
                throw PythonOps.TypeError("__bases__ must be a tuple object");
            }

            List <OldClass> res = new List <OldClass>(t.__len__());

            foreach (object o in t)
            {
                OldClass oc = o as OldClass;
                if (oc == null)
                {
                    throw PythonOps.TypeError("__bases__ items must be classes (got {0})", PythonTypeOps.GetName(o));
                }

                if (oc.IsSubclassOf(this))
                {
                    throw PythonOps.TypeError("a __bases__ item causes an inheritance cycle");
                }

                res.Add(oc);
            }
            return(res);
        }
示例#3
0
        IList <object> IPythonMembersList.GetMemberNames(CodeContext /*!*/ context)
        {
            PythonDictionary attrs = new PythonDictionary(_dict);

            OldClass.RecurseAttrHierarchy(this._class, attrs);
            return(PythonOps.MakeListFromSequence(attrs));
        }
示例#4
0
 private void SetClass(object value)
 {
     if (!(value is OldClass oc))
     {
         throw PythonOps.TypeError("__class__ must be set to class");
     }
     _class = oc;
 }
示例#5
0
 public OldInstance(CodeContext/*!*/ context, OldClass @class) {
     _class = @class;
     _dict = MakeDictionary(@class);
     if (_class.HasFinalizer) {
         // class defines finalizer, we get it automatically.
         AddFinalizer(context);
     }
 }
示例#6
0
 public OldInstance(CodeContext/*!*/ context, OldClass @class, PythonDictionary dict) {
     _class = @class;
     _dict = dict ?? PythonDictionary.MakeSymbolDictionary();
     if (_class.HasFinalizer) {
         // class defines finalizer, we get it automatically.
         AddFinalizer(context);
     }
 }
示例#7
0
        private OldInstance(SerializationInfo info, StreamingContext context) {
            _class = (OldClass)info.GetValue("__class__", typeof(OldClass));
            _dict = MakeDictionary(_class);

            List<object> keys = (List<object>)info.GetValue("keys", typeof(List<object>));
            List<object> values = (List<object>)info.GetValue("values", typeof(List<object>));
            for (int i = 0; i < keys.Count; i++) {
                _dict[keys[i]] = values[i];
            }
        }
示例#8
0
 public OldInstance(CodeContext /*!*/ context, OldClass @class)
 {
     _class = @class;
     _dict  = MakeDictionary(@class);
     if (_class.HasFinalizer)
     {
         // class defines finalizer, we get it automatically.
         AddFinalizer(context);
     }
 }
示例#9
0
        private void SetClass(object value)
        {
            OldClass oc = value as OldClass;

            if (oc == null)
            {
                throw PythonOps.TypeError("__class__ must be set to class");
            }
            _class = oc;
        }
示例#10
0
 public OldInstance(CodeContext /*!*/ context, OldClass @class, PythonDictionary dict)
 {
     _class = @class;
     _dict  = dict ?? PythonDictionary.MakeSymbolDictionary();
     if (_class.HasFinalizer)
     {
         // class defines finalizer, we get it automatically.
         AddFinalizer(context);
     }
 }
示例#11
0
        private OldInstance(SerializationInfo info, StreamingContext context)
        {
            _class = (OldClass)info.GetValue("__class__", typeof(OldClass));
            _dict  = MakeDictionary(_class);

            List <object> keys   = (List <object>)info.GetValue("keys", typeof(List <object>));
            List <object> values = (List <object>)info.GetValue("values", typeof(List <object>));

            for (int i = 0; i < keys.Count; i++)
            {
                _dict[keys[i]] = values[i];
            }
        }
示例#12
0
文件: OldClass.cs 项目: gavz/IronKit
        internal static void RecurseAttrHierarchy(OldClass oc, IDictionary <object, object> attrs)
        {
            foreach (KeyValuePair <object, object> kvp in oc._dict._storage.GetItems())
            {
                if (!attrs.ContainsKey(kvp.Key))
                {
                    attrs.Add(kvp.Key, kvp.Key);
                }
            }

            //  recursively get attrs in parent hierarchy
            if (oc._bases.Count != 0)
            {
                foreach (OldClass parent in oc._bases)
                {
                    RecurseAttrHierarchy(parent, attrs);
                }
            }
        }
        private PythonFixture BuildFixture(OldClass pythonClass)
        {
            PythonFixture fixture = new PythonFixture((string) pythonClass.__name__);

            OldInstance instance = _createInstance(pythonClass);

            // assing the test methods

            foreach (string methodName in FindTestMethods(pythonClass, TestMethodPrefix))
            {
                fixture.Add(new PythonTestCase(methodName, MakeProc(instance, methodName)));
            }

            // assign the setup and tear down methods

            fixture.SetFixtureSetup(MakeProc(instance, "setUpFixture"));
            fixture.SetFixtureTeardown(MakeProc(instance, "tearDownFixture"));
            fixture.SetSetup(MakeProc(instance, "setUp"));
            fixture.SetTeardown(MakeProc(instance, "tearDown"));

            // all done

            return fixture;
        }
示例#14
0
 public OldClassDebugView(OldClass klass) {
     _class = klass;
 }
示例#15
0
        private WeakRefTracker _weakRef;       // initialized if user defines finalizer on class or instance

        private static PythonDictionary MakeDictionary(OldClass oldClass) {
            return new PythonDictionary(new CustomInstanceDictionaryStorage(oldClass.OptimizedInstanceNames, oldClass.OptimizedInstanceNamesVersion));
        }
示例#16
0
 private void SetClass(object value) {
     OldClass oc = value as OldClass;
     if (oc == null) {
         throw PythonOps.TypeError("__class__ must be set to class");
     }
     _class = oc;
 }
示例#17
0
 StoreTyped(OldClass cls)
 {
     uint size = (uint)Marshal.SizeOf(typeof(PyClassObject));
     IntPtr ptr = this.allocator.Alloc(size);
     CPyMarshal.Zero(ptr, size);
     
     CPyMarshal.WriteIntField(ptr, typeof(PyObject), "ob_refcnt", 2); // leak classes deliberately
     CPyMarshal.WritePtrField(ptr, typeof(PyObject), "ob_type", this.PyClass_Type);
     
     CPyMarshal.WritePtrField(ptr, typeof(PyClassObject), "cl_bases", 
         this.Store(Builtin.getattr(this.scratchContext, cls, "__bases__")));
     CPyMarshal.WritePtrField(ptr, typeof(PyClassObject), "cl_dict", 
         this.Store(Builtin.getattr(this.scratchContext, cls, "__dict__")));
     CPyMarshal.WritePtrField(ptr, typeof(PyClassObject), "cl_name", 
         this.Store(Builtin.getattr(this.scratchContext, cls, "__name__")));
     
     this.map.Associate(ptr, cls);
     return ptr;
 }
示例#18
0
        internal static void RecurseAttrHierarchy(OldClass oc, IDictionary<object, object> attrs) {
            foreach (KeyValuePair<object, object> kvp in oc._dict._storage.GetItems()) {
                if (!attrs.ContainsKey(kvp.Key)) {
                    attrs.Add(kvp.Key, kvp.Key);
                }
            }

            //  recursively get attrs in parent hierarchy
            if (oc._bases.Count != 0) {
                foreach (OldClass parent in oc._bases) {
                    RecurseAttrHierarchy(parent, attrs);
                }
            }
        }
示例#19
0
        public void SetAttr(ICallerContext context, SymbolId name, object value)
        {
            object setFunc;

            if (name.Id == SymbolTable.ClassId) {
                OldClass oc = value as OldClass;
                if (oc == null) {
                    throw Ops.TypeError("__class__ must be set to class");
                }
                __class__ = oc;
            } else if (name.Id == SymbolTable.DictId) {
                IAttributesDictionary dict = value as IAttributesDictionary;
                if (dict == null) {
                    throw Ops.TypeError("__dict__ must be set to a dictionary");
                }
                if (HasFinalizer() && !__class__.HasFinalizer) {
                    if (!dict.ContainsKey(SymbolTable.Unassign)) {
                        ClearFinalizer();
                    }
                } else if (dict.ContainsKey(SymbolTable.Unassign)) {
                    AddFinalizer();
                }

                __dict__ = dict;
            } else if (__class__.HasSetAttr && __class__.TryLookupSlot(SymbolTable.SetAttr, out setFunc)) {
                Ops.Call(Ops.GetDescriptor(setFunc, this, __class__), name.ToString(), value);
            } else if (name.Id == SymbolTable.UnassignId) {
                if (!HasFinalizer()) {
                    // user is defining __del__ late bound for the 1st time
                    AddFinalizer();
                }

                __dict__[name] = value;
            } else {
                __dict__[name] = value;
            }
        }
示例#20
0
        private WeakRefTracker _weakRef;       // initialized if user defines finalizer on class or instance

        private static PythonDictionary MakeDictionary(OldClass oldClass)
        {
            return(new PythonDictionary(new CustomInstanceDictionaryStorage(oldClass.OptimizedInstanceNames, oldClass.OptimizedInstanceNamesVersion)));
        }
示例#21
0
 public OldInstance(OldClass _class)
 {
     __class__ = _class;
     __dict__ = new CustomOldClassDict();
     if (__class__.HasFinalizer) {
         // class defines finalizer, we get it automatically.
         AddFinalizer();
     }
 }
示例#22
0
 public virtual bool __subclasscheck__(OldClass sub) {
     return IsSubclassOf(sub.TypeObject);
 }
示例#23
0
文件: OldClass.cs 项目: gavz/IronKit
 public OldClassDebugView(OldClass klass)
 {
     _class = klass;
 }
        internal static OldClass DefaultExceptionCreator(string name, string module, object baseType)
        {
            Tuple bases = ObjectToTuple(baseType);

            FieldIdDict dict = new FieldIdDict();
            dict[SymbolTable.Module] = module;
            OldClass oc = new OldClass(name, bases, dict);
            oc.SetAttr(DefaultContext.Default, SymbolTable.Init, exceptionInitMethod);
            oc.SetAttr(DefaultContext.Default, SymbolTable.GetItem, exceptionGetItemMethod);
            oc.SetAttr(DefaultContext.Default, SymbolTable.String, exceptionStrMethod);
            oc.SetAttr(DefaultContext.Default, SymbolTable.GetState, exceptionGetStateMethod);

            return oc;
        }
 private List<string> FindTestMethods(OldClass obj, string prefix)
 {
     return _findTestMethods(obj, prefix).ToList<string>();
 }
        private List<string> FindTestMethods(OldClass obj, string prefix)
        {
            List list = _findTestMethods(obj, prefix);

            return new List<string>(new StrongEnumerator<string>(list));
        }
示例#27
0
        internal static System.Exception CreateThrowableForRaise(CodeContext/*!*/ context, OldClass type, object value) {
            object pyEx;

            if (PythonOps.IsInstance(context, value, type)) {
                pyEx = value;
            } else if (value is PythonTuple) {
                pyEx = PythonOps.CallWithArgsTuple(type, ArrayUtils.EmptyObjects, value);
            } else {
                pyEx = PythonCalls.Call(context, type, value);
            }

            return new OldInstanceException((OldInstance)pyEx);
        }
示例#28
0
        private WeakRefTracker _weakRef;       // initialized if user defines finalizer on class or instance

        private PythonDictionary MakeDictionary(OldClass oldClass) {
            //if (oldClass.OptimizedInstanceNames.Length == 0) {
            //    return new CustomOldClassDictionar();
            //}
            return new PythonDictionary(new CustomOldClassDictionaryStorage(oldClass.OptimizedInstanceNames, oldClass.OptimizedInstanceNamesVersion));
        }
示例#29
0
 private void RecurseAttrHierarchyInt(OldClass oc, IDictionary<SymbolId, object> attrs)
 {
     foreach (SymbolId key in oc.__dict__.Keys) {
         if (!attrs.ContainsKey(key)) {
             attrs.Add(key, key);
         }
     }
     //  recursively get attrs in parent hierarchy
     if (oc.__bases__.Count != 0) {
         foreach (OldClass parent in oc.__bases__) {
             RecurseAttrHierarchyInt(parent, attrs);
         }
     }
 }
示例#30
0
        /// <summary>
        /// Creates a new PythonType object which represents an Old-style class.
        /// </summary>
        internal PythonType(OldClass oc) {
            EnsureDict();

            _underlyingSystemType = typeof(OldInstance);
            Name = oc.__name__;
            OldClass = oc;

            List<PythonType> ocs = new List<PythonType>(oc.BaseClasses.Count);
            foreach (OldClass klass in oc.BaseClasses) {
                ocs.Add(klass.TypeObject);
            }

            List<PythonType> mro = new List<PythonType>();
            mro.Add(this);

            _bases = ocs.ToArray(); 
            _resolutionOrder = mro;
            AddSlot(Symbols.Class, new PythonTypeValueSlot(this));
        }
示例#31
0
 public MetaOldClass(Expression/*!*/ expression, BindingRestrictions/*!*/ restrictions, OldClass/*!*/ value)
     : base(expression, BindingRestrictions.Empty, value) {
     Assert.NotNull(value);
 }
        private void AddOldClassMembers(List<MemberDoc> res, OldClass oc) {
            foreach (var member in oc._dict) {
                AddMember(res, member, true);
            }

            foreach (var baseCls in oc.BaseClasses) {
                AddOldClassMembers(res, baseCls);
            }
        }
 private bool IsNUnitFixture(OldClass obj)
 {
     return _isNUnitFixture(obj);
 }
示例#34
0
        internal static void RecurseAttrHierarchy(OldClass oc, IDictionary<object, object> attrs)
        {
            foreach (object key in oc.__dict__.Keys) {
                if (!attrs.ContainsKey(key)) {
                    attrs.Add(key, key);
                }
            }

            //  recursively get attrs in parent hierarchy
            if (oc.__bases__.Count != 0) {
                foreach (OldClass parent in oc.__bases__) {
                    RecurseAttrHierarchy(parent, attrs);
                }
            }
        }