Inheritance: ExtensionType
Esempio n. 1
0
        public static IntPtr tp_repr(IntPtr ob)
        {
            PropertyObject self = (PropertyObject)GetManagedObject(ob);
            string         s    = String.Format("<property '{0}'>", self.info.Name);

            return(Runtime.PyString_FromStringAndSize(s, s.Length));
        }
Esempio n. 2
0
        //====================================================================
        // Descriptor __get__ implementation. This method returns the
        // value of the property on the given object. The returned value
        // is converted to an appropriately typed Python object.
        //====================================================================

        public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
        {
            PropertyObject self   = (PropertyObject)GetManagedObject(ds);
            MethodInfo     getter = self.getter;
            Object         result;


            if (getter == null)
            {
                return(Exceptions.RaiseTypeError("property cannot be read"));
            }

            if ((ob == IntPtr.Zero) || (ob == Runtime.PyNone))
            {
                if (!(getter.IsStatic))
                {
                    Exceptions.SetError(Exceptions.TypeError,
                                        "instance property must be accessed through " +
                                        "a class instance"
                                        );
                    return(IntPtr.Zero);
                }

                try
                {
                    result = self.info.GetValue(null, null);
                    return(Converter.ToPython(result, self.info.PropertyType));
                }
                catch (Exception e)
                {
                    return(Exceptions.RaiseTypeError(e.Message));
                }
            }

            CLRObject co = GetManagedObject(ob) as CLRObject;

            if (co == null)
            {
                return(Exceptions.RaiseTypeError("invalid target"));
            }

            try
            {
                result = self.info.GetValue(co.inst, null);
                return(Converter.ToPython(result, self.info.PropertyType));
            }
            catch (Exception e)
            {
                if (e.InnerException != null)
                {
                    e = e.InnerException;
                }
                Exceptions.SetError(e);
                return(IntPtr.Zero);
            }
        }
Esempio n. 3
0
        private static ClassInfo GetClassInfo(Type type)
        {
            var         ci      = new ClassInfo();
            var         methods = new Hashtable();
            ArrayList   list;
            MethodInfo  meth;
            ManagedType ob;
            string      name;
            object      item;
            Type        tp;
            int         i, n;

            MemberInfo[] info  = type.GetMembers(BindingFlags);
            var          local = new Hashtable();
            var          items = new ArrayList();
            MemberInfo   m;

            // Loop through once to find out which names are declared
            for (i = 0; i < info.Length; i++)
            {
                m = info[i];
                if (m.DeclaringType == type)
                {
                    local[m.Name] = 1;
                }
            }

            // Now again to filter w/o losing overloaded member info
            for (i = 0; i < info.Length; i++)
            {
                m = info[i];
                if (local[m.Name] != null)
                {
                    items.Add(m);
                }
            }

            if (type.IsInterface)
            {
                // Interface inheritance seems to be a different animal:
                // more contractual, less structural.  Thus, a Type that
                // represents an interface that inherits from another
                // interface does not return the inherited interface's
                // methods in GetMembers. For example ICollection inherits
                // from IEnumerable, but ICollection's GetMemebers does not
                // return GetEnumerator.
                //
                // Not sure if this is the correct way to fix this, but it
                // seems to work. Thanks to Bruce Dodson for the fix.

                Type[] inheritedInterfaces = type.GetInterfaces();

                for (i = 0; i < inheritedInterfaces.Length; ++i)
                {
                    Type         inheritedType = inheritedInterfaces[i];
                    MemberInfo[] imembers      = inheritedType.GetMembers(BindingFlags);
                    for (n = 0; n < imembers.Length; n++)
                    {
                        m = imembers[n];
                        if (local[m.Name] == null)
                        {
                            items.Add(m);
                        }
                    }
                }

                // All interface implementations inherit from Object,
                // but GetMembers don't return them either.
                var objFlags = BindingFlags.Public | BindingFlags.Instance;
                foreach (var mi in typeof(object).GetMembers(objFlags))
                {
                    if (local[mi.Name] == null)
                    {
                        items.Add(mi);
                    }
                }
            }

            for (i = 0; i < items.Count; i++)
            {
                var mi = (MemberInfo)items[i];

                switch (mi.MemberType)
                {
                case MemberTypes.Method:
                    meth = (MethodInfo)mi;
                    if (!ShouldBindMethod(meth))
                    {
                        continue;
                    }
                    name = meth.Name;
                    item = methods[name];
                    if (item == null)
                    {
                        item = methods[name] = new ArrayList();
                    }
                    list = (ArrayList)item;
                    list.Add(meth);
                    continue;

                case MemberTypes.Property:
                    var pi = (PropertyInfo)mi;

                    if (!ShouldBindProperty(pi))
                    {
                        continue;
                    }

                    // Check for indexer
                    ParameterInfo[] args = pi.GetIndexParameters();
                    if (args.GetLength(0) > 0)
                    {
                        Indexer idx = ci.indexer;
                        if (idx == null)
                        {
                            ci.indexer = new Indexer();
                            idx        = ci.indexer;
                        }
                        idx.AddProperty(pi);
                        continue;
                    }

                    ob = new PropertyObject(pi);
                    ci.members[pi.Name] = ob;
                    continue;

                case MemberTypes.Field:
                    var fi = (FieldInfo)mi;
                    if (!ShouldBindField(fi))
                    {
                        continue;
                    }
                    ob = new FieldObject(fi);
                    ci.members[mi.Name] = ob;
                    continue;

                case MemberTypes.Event:
                    var ei = (EventInfo)mi;
                    if (!ShouldBindEvent(ei))
                    {
                        continue;
                    }
                    ob = new EventObject(ei);
                    ci.members[ei.Name] = ob;
                    continue;

                case MemberTypes.NestedType:
                    tp = (Type)mi;
                    if (!(tp.IsNestedPublic || tp.IsNestedFamily ||
                          tp.IsNestedFamORAssem))
                    {
                        continue;
                    }
                    // Note the given instance might be uninitialized
                    ob = GetClass(tp);
                    // GetClass returns a Borrowed ref. ci.members owns the reference.
                    ob.IncrRefCount();
                    ci.members[mi.Name] = ob;
                    continue;
                }
            }

            IDictionaryEnumerator iter = methods.GetEnumerator();

            while (iter.MoveNext())
            {
                name = (string)iter.Key;
                list = (ArrayList)iter.Value;

                var mlist = (MethodInfo[])list.ToArray(typeof(MethodInfo));

                ob = new MethodObject(type, name, mlist);
                ci.members[name] = ob;
                if (mlist.Any(OperatorMethod.IsOperatorMethod))
                {
                    string pyName        = OperatorMethod.GetPyMethodName(name);
                    string pyNameReverse = OperatorMethod.ReversePyMethodName(pyName);
                    OperatorMethod.FilterMethods(mlist, out var forwardMethods, out var reverseMethods);
                    // Only methods where the left operand is the declaring type.
                    if (forwardMethods.Length > 0)
                    {
                        ci.members[pyName] = new MethodObject(type, name, forwardMethods);
                    }
                    // Only methods where only the right operand is the declaring type.
                    if (reverseMethods.Length > 0)
                    {
                        ci.members[pyNameReverse] = new MethodObject(type, name, reverseMethods);
                    }
                }
            }

            if (ci.indexer == null && type.IsClass)
            {
                // Indexer may be inherited.
                var parent = type.BaseType;
                while (parent != null && ci.indexer == null)
                {
                    foreach (var prop in parent.GetProperties())
                    {
                        var args = prop.GetIndexParameters();
                        if (args.GetLength(0) > 0)
                        {
                            ci.indexer = new Indexer();
                            ci.indexer.AddProperty(prop);
                            break;
                        }
                    }
                    parent = parent.BaseType;
                }
            }

            return(ci);
        }
Esempio n. 4
0
        private static ClassInfo GetClassInfo(Type type)
        {
            ClassInfo   ci      = new ClassInfo(type);
            Hashtable   methods = new Hashtable();
            ArrayList   list;
            MethodInfo  meth;
            ManagedType ob;
            String      name;
            Object      item;
            Type        tp;
            int         i, n;

            // This is complicated because inheritance in Python is name
            // based. We can't just find DeclaredOnly members, because we
            // could have a base class A that defines two overloads of a
            // method and a class B that defines two more. The name-based
            // descriptor Python will find needs to know about inherited
            // overloads as well as those declared on the sub class.

            BindingFlags flags = BindingFlags.Static |
                                 BindingFlags.Instance |
                                 BindingFlags.Public |
                                 BindingFlags.NonPublic;

            MemberInfo[] info  = type.GetMembers(flags);
            Hashtable    local = new Hashtable();
            ArrayList    items = new ArrayList();
            MemberInfo   m;

            // Loop through once to find out which names are declared
            for (i = 0; i < info.Length; i++)
            {
                m = info[i];
                if (m.DeclaringType == type)
                {
                    local[m.Name] = 1;
                }
            }

            // Now again to filter w/o losing overloaded member info
            for (i = 0; i < info.Length; i++)
            {
                m = info[i];
                if (local[m.Name] != null)
                {
                    items.Add(m);
                }
            }

            if (type.IsInterface)
            {
                // Interface inheritance seems to be a different animal:
                // more contractual, less structural.  Thus, a Type that
                // represents an interface that inherits from another
                // interface does not return the inherited interface's
                // methods in GetMembers. For example ICollection inherits
                // from IEnumerable, but ICollection's GetMemebers does not
                // return GetEnumerator.
                //
                // Not sure if this is the correct way to fix this, but it
                // seems to work. Thanks to Bruce Dodson for the fix.

                Type[] inheritedInterfaces = type.GetInterfaces();

                for (i = 0; i < inheritedInterfaces.Length; ++i)
                {
                    Type         inheritedType = inheritedInterfaces[i];
                    MemberInfo[] imembers      = inheritedType.GetMembers(flags);
                    for (n = 0; n < imembers.Length; n++)
                    {
                        m = imembers[n];
                        if (local[m.Name] == null)
                        {
                            items.Add(m);
                        }
                    }
                }
            }

            for (i = 0; i < items.Count; i++)
            {
                MemberInfo mi = (MemberInfo)items[i];

                switch (mi.MemberType)
                {
                case MemberTypes.Method:
                    meth = (MethodInfo)mi;
                    if (!(meth.IsPublic || meth.IsFamily ||
                          meth.IsFamilyOrAssembly))
                    {
                        continue;
                    }
                    name = meth.Name;
                    item = methods[name];
                    if (item == null)
                    {
                        item = methods[name] = new ArrayList();
                    }
                    list = (ArrayList)item;
                    list.Add(meth);
                    continue;

                case MemberTypes.Property:
                    PropertyInfo pi = (PropertyInfo)mi;

                    MethodInfo mm = null;
                    try {
                        mm = pi.GetGetMethod(true);
                        if (mm == null)
                        {
                            mm = pi.GetSetMethod(true);
                        }
                    }
                    catch (SecurityException) {
                        // GetGetMethod may try to get a method protected by
                        // StrongNameIdentityPermission - effectively private.
                        continue;
                    }

                    if (mm == null)
                    {
                        continue;
                    }

                    if (!(mm.IsPublic || mm.IsFamily || mm.IsFamilyOrAssembly))
                    {
                        continue;
                    }

                    // Check for indexer
                    ParameterInfo[] args = pi.GetIndexParameters();
                    if (args.GetLength(0) > 0)
                    {
                        Indexer idx = ci.indexer;
                        if (idx == null)
                        {
                            ci.indexer = new Indexer();
                            idx        = ci.indexer;
                        }
                        idx.AddProperty(pi);
                        continue;
                    }

                    ob = new PropertyObject(pi);
                    ci.members[pi.Name] = ob;
                    continue;

                case MemberTypes.Field:
                    FieldInfo fi = (FieldInfo)mi;
                    if (!(fi.IsPublic || fi.IsFamily || fi.IsFamilyOrAssembly))
                    {
                        continue;
                    }
                    ob = new FieldObject(fi);
                    ci.members[mi.Name] = ob;
                    continue;

                case MemberTypes.Event:
                    EventInfo  ei = (EventInfo)mi;
                    MethodInfo me = ei.GetAddMethod(true);
                    if (!(me.IsPublic || me.IsFamily || me.IsFamilyOrAssembly))
                    {
                        continue;
                    }
                    ob = new EventObject(ei);
                    ci.members[ei.Name] = ob;
                    continue;

                case MemberTypes.NestedType:
                    tp = (Type)mi;
                    if (!(tp.IsNestedPublic || tp.IsNestedFamily ||
                          tp.IsNestedFamORAssem))
                    {
                        continue;
                    }
                    ob = ClassManager.GetClass(tp);
                    ci.members[mi.Name] = ob;
                    continue;
                }
            }

            IDictionaryEnumerator iter = methods.GetEnumerator();

            while (iter.MoveNext())
            {
                name = (string)iter.Key;
                list = (ArrayList)iter.Value;

                MethodInfo[] mlist = (MethodInfo[])list.ToArray(
                    typeof(MethodInfo)
                    );

                ob = new MethodObject(name, mlist);
                ci.members[name] = ob;
            }

            return(ci);
        }
Esempio n. 5
0
        private static ClassInfo GetClassInfo(Type type)
        {
            ClassInfo ci = new ClassInfo(type);
            Hashtable methods = new Hashtable();
            ArrayList list;
            MethodInfo meth;
            ManagedType ob;
            String name;
            Object item;
            Type tp;
            int i, n;

            // This is complicated because inheritance in Python is name
            // based. We can't just find DeclaredOnly members, because we
            // could have a base class A that defines two overloads of a
            // method and a class B that defines two more. The name-based
            // descriptor Python will find needs to know about inherited
            // overloads as well as those declared on the sub class.

            BindingFlags flags = BindingFlags.Static |
                                 BindingFlags.Instance |
                                 BindingFlags.Public |
                                 BindingFlags.NonPublic;

            MemberInfo[] info = type.GetMembers(flags);
            Hashtable local = new Hashtable();
            ArrayList items = new ArrayList();
            MemberInfo m;

            // Loop through once to find out which names are declared
            for (i = 0; i < info.Length; i++)
            {
                m = info[i];
                if (m.DeclaringType == type)
                {
                    local[m.Name] = 1;
                }
            }

            // Now again to filter w/o losing overloaded member info
            for (i = 0; i < info.Length; i++)
            {
                m = info[i];
                if (local[m.Name] != null)
                {
                    items.Add(m);
                }
            }

            if (type.IsInterface)
            {
                // Interface inheritance seems to be a different animal:
                // more contractual, less structural.  Thus, a Type that
                // represents an interface that inherits from another
                // interface does not return the inherited interface's
                // methods in GetMembers. For example ICollection inherits
                // from IEnumerable, but ICollection's GetMemebers does not
                // return GetEnumerator.
                //
                // Not sure if this is the correct way to fix this, but it
                // seems to work. Thanks to Bruce Dodson for the fix.

                Type[] inheritedInterfaces = type.GetInterfaces();

                for (i = 0; i < inheritedInterfaces.Length; ++i)
                {
                    Type inheritedType = inheritedInterfaces[i];
                    MemberInfo[] imembers = inheritedType.GetMembers(flags);
                    for (n = 0; n < imembers.Length; n++)
                    {
                        m = imembers[n];
                        if (local[m.Name] == null)
                        {
                            items.Add(m);
                        }
                    }
                }
            }

            for (i = 0; i < items.Count; i++)
            {
                MemberInfo mi = (MemberInfo)items[i];

                switch (mi.MemberType)
                {
                    case MemberTypes.Method:
                        meth = (MethodInfo)mi;
                        if (!(meth.IsPublic || meth.IsFamily ||
                              meth.IsFamilyOrAssembly))
                            continue;
                        name = meth.Name;
                        item = methods[name];
                        if (item == null)
                        {
                            item = methods[name] = new ArrayList();
                        }
                        list = (ArrayList)item;
                        list.Add(meth);
                        continue;

                    case MemberTypes.Property:
                        PropertyInfo pi = (PropertyInfo)mi;

                        MethodInfo mm = null;
                        try
                        {
                            mm = pi.GetGetMethod(true);
                            if (mm == null)
                            {
                                mm = pi.GetSetMethod(true);
                            }
                        }
                        catch (SecurityException)
                        {
                            // GetGetMethod may try to get a method protected by
                            // StrongNameIdentityPermission - effectively private.
                            continue;
                        }

                        if (mm == null)
                        {
                            continue;
                        }

                        if (!(mm.IsPublic || mm.IsFamily || mm.IsFamilyOrAssembly))
                            continue;

                        // Check for indexer
                        ParameterInfo[] args = pi.GetIndexParameters();
                        if (args.GetLength(0) > 0)
                        {
                            Indexer idx = ci.indexer;
                            if (idx == null)
                            {
                                ci.indexer = new Indexer();
                                idx = ci.indexer;
                            }
                            idx.AddProperty(pi);
                            continue;
                        }

                        ob = new PropertyObject(pi);
                        ci.members[pi.Name] = ob;
                        continue;

                    case MemberTypes.Field:
                        FieldInfo fi = (FieldInfo)mi;
                        if (!(fi.IsPublic || fi.IsFamily || fi.IsFamilyOrAssembly))
                            continue;
                        ob = new FieldObject(fi);
                        ci.members[mi.Name] = ob;
                        continue;

                    case MemberTypes.Event:
                        EventInfo ei = (EventInfo)mi;
                        MethodInfo me = ei.GetAddMethod(true);
                        if (!(me.IsPublic || me.IsFamily || me.IsFamilyOrAssembly))
                            continue;
                        ob = new EventObject(ei);
                        ci.members[ei.Name] = ob;
                        continue;

                    case MemberTypes.NestedType:
                        tp = (Type)mi;
                        if (!(tp.IsNestedPublic || tp.IsNestedFamily ||
                              tp.IsNestedFamORAssem))
                            continue;
                        // Note the given instance might be uninitialized
                        ob = ClassManager.GetClass(tp);
                        ci.members[mi.Name] = ob;
                        continue;
                }
            }

            IDictionaryEnumerator iter = methods.GetEnumerator();

            while (iter.MoveNext())
            {
                name = (string)iter.Key;
                list = (ArrayList)iter.Value;

                MethodInfo[] mlist = (MethodInfo[])list.ToArray(
                    typeof(MethodInfo)
                    );

                ob = new MethodObject(type, name, mlist);
                ci.members[name] = ob;
            }

            return ci;
        }
Esempio n. 6
0
        public static new int tp_descr_set(IntPtr ds, IntPtr ob, IntPtr val)
        {
            PropertyObject self   = (PropertyObject)GetManagedObject(ds);
            MethodInfo     setter = self.setter;
            IntPtr         ts     = IntPtr.Zero;
            Object         newval;

            if (val == IntPtr.Zero)
            {
                Exceptions.RaiseTypeError("cannot delete property");
                return(-1);
            }

            if (setter == null)
            {
                Exceptions.RaiseTypeError("property is read-only");
                return(-1);
            }


            if (!Converter.ToManaged(val, self.info.PropertyType, out newval,
                                     true))
            {
                return(-1);
            }

            bool is_static = setter.IsStatic;

            if ((ob == IntPtr.Zero) || (ob == Runtime.PyNone))
            {
                if (!(is_static))
                {
                    Exceptions.RaiseTypeError(
                        "instance property must be set on an instance"
                        );
                    return(-1);
                }
            }

            try {
                if (!is_static)
                {
                    CLRObject co = GetManagedObject(ob) as CLRObject;
                    if (co == null)
                    {
                        Exceptions.RaiseTypeError("invalid target");
                        return(-1);
                    }
                    self.info.SetValue(co.inst, newval, null);
                }
                else
                {
                    self.info.SetValue(null, newval, null);
                }
                return(0);
            }
            catch (Exception e) {
                if (e.InnerException != null)
                {
                    e = e.InnerException;
                }
                Exceptions.SetError(e);
                return(-1);
            }
        }