Beispiel #1
0
        public override bool IsSubclassOf(object other)
        {
            if (this == other)
            {
                return(true);
            }

            DynamicType dt = other as DynamicType;

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

            Tuple bases = BaseClasses;

            foreach (object b in bases)
            {
                OldClass bc = b as OldClass;
                if (bc != null && bc.IsSubclassOf(other))
                {
                    return(true);
                }
            }
            return(false);
        }
Beispiel #2
0
        public List GetAttrNames(ICallerContext context)
        {
            FieldIdDict attrs = new FieldIdDict(__dict__);

            OldClass.RecurseAttrHierarchy(this.__class__, attrs);
            return(List.Make(attrs));
        }
Beispiel #3
0
        private bool TryLookupInBase(CodeContext context, PythonType pt, string name, object self, out object value)
        {
            PythonTypeSlot dts;

            if (pt.OldClass == null)
            {
                // new-style class, or reflected type, lookup slot
                if (pt.TryLookupSlot(context, name, out dts) &&
                    dts.TryGetValue(context, self, DescriptorContext, out value))
                {
                    return(true);
                }
            }
            else
            {
                // old-style class, lookup attribute
                OldClass dt = pt.OldClass;

                if (PythonOps.TryGetBoundAttr(context, dt, name, out value))
                {
                    value = OldClass.GetOldStyleDescriptor(context, value, self, DescriptorContext);
                    return(true);
                }
            }
            value = null;
            return(false);
        }
        private static OldClass SyntaxErrorExceptionCreator(string name, object baseType)
        {
            OldClass syntaxError = DefaultExceptionCreator(name, baseType);

            syntaxError.SetAttr(DefaultContext.Default, SymbolTable.String, syntaxErrorStrMethod);
            return(syntaxError);
        }
Beispiel #5
0
        private WeakRefTracker weakRef;       // initialized if user defines finalizer on class or instance

        public OldInstance(OldClass _class)
        {
            __class__ = _class;
            __dict__  = new CustomOldClassDict();
            if (__class__.HasFinalizer)
            {
                // class defines finalizer, we get it automatically.
                AddFinalizer();
            }
        }
Beispiel #6
0
        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 static OldClass DefaultExceptionCreator(string name, object baseType)
        {
            Tuple bases = (baseType == null) ? Tuple.MakeTuple() : Tuple.MakeTuple(baseType);

            FieldIdDict dict = new FieldIdDict();

            dict[SymbolTable.Module] = "exceptions";
            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);

            return(oc);
        }
        private static void CreateExceptionMapping(object baseType, ExceptionMapping em)
        {
            OldClass oc = em.Creator(em.PythonException, baseType);

            pythonToClr[oc] = em.CLRException;
            clrToPython[em.CLRException]     = oc;
            nameToPython[em.PythonException] = oc;

            if (em.SubTypes != null)
            {
                for (int i = 0; i < em.SubTypes.Length; i++)
                {
                    CreateExceptionMapping(oc, em.SubTypes[i]);
                }
            }
        }
Beispiel #9
0
        public void SetAttr(ICallerContext context, SymbolId name, object value)
        {
            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 (name.Id == SymbolTable.UnassignId)
            {
                if (!HasFinalizer())
                {
                    // user is defining __del__ late bound for the 1st time
                    AddFinalizer();
                }

                __dict__[name] = value;
            }
            else
            {
                __dict__[name] = value;
            }
        }
Beispiel #10
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);
         }
     }
 }
Beispiel #11
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);
                }
            }
        }
Beispiel #12
0
        private string DeclaringClassAsString()
        {
            if (im_class == null)
            {
                return("?");
            }
            PythonType dt = im_class as PythonType;

            if (dt != null)
            {
                return(dt.Name);
            }
            OldClass oc = im_class as OldClass;

            if (oc != null)
            {
                return(oc.Name);
            }
            return(im_class.ToString());
        }
        // Is this an exception object (as defined by Python)?
        static bool IsExceptionObject(object e)
        {
            if (e == null)
            {
                return(false);
            }
            if (e is Exception)
            {
                return(true);
            }

            // It could be a PythonType created by CreateExceptionMapping
            if (e is OldInstance)
            {
                OldClass oldClass = ((OldInstance)e).__class__;
                return(oldClass.IsSubclassOf(clrToPython[typeof(Exception)]));
            }

            return(false);
        }
Beispiel #14
0
        private Exception BadSelf(object got)
        {
            OldClass dt = im_class as OldClass;

            string firstArg;

            if (got == null)
            {
                firstArg = "nothing";
            }
            else
            {
                firstArg = PythonOps.GetPythonTypeName(got) + " instance";
            }
            PythonType pt = im_class as PythonType;

            return(PythonOps.TypeError("unbound method {0}() must be called with {1} instance as first argument (got {2} instead)",
                                       Name,
                                       (dt != null) ? dt.Name : (pt != null) ? pt.Name : im_class,
                                       firstArg));
        }
Beispiel #15
0
        public override ICollection <MemberDoc> GetMembers(object value)
        {
            List <MemberDoc> res = new List <MemberDoc>();

            PythonModule mod = value as PythonModule;

            if (mod != null)
            {
                foreach (var kvp in mod.__dict__)
                {
                    AddMember(res, kvp, false);
                }
                return(res);
            }

            NamespaceTracker ns = value as NamespaceTracker;

            if (ns != null)
            {
                foreach (var v in ns)
                {
                    AddMember(
                        res,
                        new KeyValuePair <object, object>(
                            v.Key,
                            Importer.MemberTrackerToPython(_context.SharedClsContext, v.Value)
                            ),
                        false
                        );
                }
            }
            else
            {
                OldInstance oi = value as OldInstance;
                if (oi != null)
                {
                    foreach (var member in oi.Dictionary)
                    {
                        AddMember(res, member, false);
                    }

                    AddOldClassMembers(res, oi._class);
                }
                else
                {
                    PythonType pt = value as PythonType;
                    if (pt != null)
                    {
                        foreach (PythonType type in pt.ResolutionOrder)
                        {
                            foreach (var member in type.GetMemberDictionary(_context.SharedContext))
                            {
                                AddMember(res, member, true);
                            }
                        }
                    }
                    else
                    {
                        OldClass oc = value as OldClass;
                        if (oc != null)
                        {
                            AddOldClassMembers(res, oc);
                        }
                        else
                        {
                            pt = DynamicHelpers.GetPythonType(value);
                            foreach (var member in pt.GetMemberDictionary(_context.SharedContext))
                            {
                                AddMember(res, member, true);
                            }
                        }
                    }

                    IPythonObject ipo = value as IPythonObject;
                    if (ipo != null && ipo.Dict != null)
                    {
                        foreach (var member in ipo.Dict)
                        {
                            AddMember(res, member, false);
                        }
                    }
                }
            }

            return(res.ToArray());
        }