예제 #1
0
        /// <summary>
        /// Looks up __init__ avoiding calls to __getattribute__ and handling both
        /// new-style and old-style classes in the MRO.
        /// </summary>
        private static object GetInitMethod(CodeContext /*!*/ context, PythonType dt, object newObject)
        {
            // __init__ is never searched for w/ __getattribute__
            for (int i = 0; i < dt.ResolutionOrder.Count; i++)
            {
                PythonType     cdt = dt.ResolutionOrder[i];
                PythonTypeSlot dts;
                object         value;
                if (cdt.IsOldClass)
                {
                    OldClass oc = PythonOps.ToPythonType(cdt) as OldClass;

                    if (oc != null && oc.TryGetBoundCustomMember(context, "__init__", out value))
                    {
                        return(oc.GetOldStyleDescriptor(context, value, newObject, oc));
                    }
                    // fall through to new-style only case.  We might accidently
                    // detect old-style if the user imports a IronPython.NewTypes
                    // type.
                }

                if (cdt.TryLookupSlot(context, "__init__", out dts) &&
                    dts.TryGetValue(context, newObject, dt, out value))
                {
                    return(value);
                }
            }
            return(null);
        }
예제 #2
0
        public static bool TryGetMixedNewStyleOldStyleSlot(CodeContext context, object instance, string name, out object value)
        {
            IPythonObject sdo = instance as IPythonObject;

            if (sdo != null)
            {
                PythonDictionary dict = sdo.Dict;
                if (dict != null && dict.TryGetValue(name, out value))
                {
                    return(true);
                }
            }

            PythonType dt = DynamicHelpers.GetPythonType(instance);

            foreach (PythonType type in dt.ResolutionOrder)
            {
                PythonTypeSlot dts;
                if (type != TypeCache.Object && type.OldClass != null)
                {
                    // we're an old class, check the old-class way
                    OldClass oc = type.OldClass;

                    if (oc.TryGetBoundCustomMember(context, name, out value))
                    {
                        value = oc.GetOldStyleDescriptor(context, value, instance, oc);
                        return(true);
                    }
                }
                else if (type.TryLookupSlot(context, name, out dts))
                {
                    // we're a dynamic type, check the dynamic type way
                    return(dts.TryGetValue(context, instance, dt, out value));
                }
            }

            value = null;
            return(false);
        }