Inheritance: ICustomTypeDescriptor, ICodeFormattable, IMembersList, IDynamicMetaObjectProvider, IPythonMembersList, IWeakReferenceable
Esempio n. 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);
        }
Esempio n. 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);
        }
Esempio n. 3
0
        IList <object> IPythonMembersList.GetMemberNames(CodeContext /*!*/ context)
        {
            PythonDictionary attrs = new PythonDictionary(_dict);

            OldClass.RecurseAttrHierarchy(this._class, attrs);
            return(PythonOps.MakeListFromSequence(attrs));
        }
Esempio n. 4
0
 private void SetClass(object value)
 {
     if (!(value is OldClass oc))
     {
         throw PythonOps.TypeError("__class__ must be set to class");
     }
     _class = oc;
 }
Esempio n. 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);
     }
 }
Esempio n. 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);
     }
 }
Esempio n. 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];
            }
        }
Esempio n. 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);
     }
 }
Esempio n. 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;
        }
Esempio n. 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);
     }
 }
Esempio n. 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];
            }
        }
Esempio n. 12
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);
                }
            }
        }
        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;
        }
Esempio n. 14
0
 public OldClassDebugView(OldClass klass) {
     _class = klass;
 }
Esempio n. 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));
        }
Esempio n. 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;
 }
Esempio n. 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;
 }
Esempio n. 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);
                }
            }
        }
Esempio n. 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;
            }
        }
Esempio n. 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)));
        }
Esempio n. 21
0
 public OldInstance(OldClass _class)
 {
     __class__ = _class;
     __dict__ = new CustomOldClassDict();
     if (__class__.HasFinalizer) {
         // class defines finalizer, we get it automatically.
         AddFinalizer();
     }
 }
Esempio n. 22
0
 public virtual bool __subclasscheck__(OldClass sub) {
     return IsSubclassOf(sub.TypeObject);
 }
Esempio n. 23
0
 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));
        }
Esempio n. 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);
        }
Esempio n. 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));
        }
Esempio n. 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);
         }
     }
 }
Esempio n. 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));
        }
Esempio n. 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);
 }
Esempio n. 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);
                }
            }
        }