예제 #1
0
        internal static object MemberTrackerToPython(CodeContext /*!*/ context, object ret)
        {
            if (ret is MemberTracker res)
            {
                context.ShowCls = true;
                object realRes = res;

                switch (res.MemberType)
                {
                case TrackerTypes.Type: realRes = DynamicHelpers.GetPythonTypeFromType(((TypeTracker)res).Type); break;

                case TrackerTypes.Field: realRes = PythonTypeOps.GetReflectedField(((FieldTracker)res).Field); break;

                case TrackerTypes.Event: realRes = PythonTypeOps.GetReflectedEvent((EventTracker)res); break;

                case TrackerTypes.Method:
                    MethodTracker mt = res as MethodTracker;
                    realRes = PythonTypeOps.GetBuiltinFunction(mt.DeclaringType, mt.Name, new MemberInfo[] { mt.Method });
                    break;
                }

                ret = realRes;
            }
            return(ret);
        }
예제 #2
0
        public override PythonTypeSlot/*!*/ GetSlot() {
            List<MethodBase> meths = new List<MethodBase>();
            foreach (MethodTracker mt in _trackers) {
                meths.Add(mt.Method);
            }

            MethodBase[] methods = meths.ToArray();
            FunctionType ft = (PythonTypeOps.GetMethodFunctionType(DeclaringType, methods) | FunctionType.Method) & (~FunctionType.Function);
            if (_reversed) {
                ft |= FunctionType.ReversedOperator;
            } else {
                ft &= ~FunctionType.ReversedOperator;
            }

            // check if this operator is only availble after importing CLR (e.g. __getitem__ on functions)
            foreach (MethodInfo mi in methods) {
                if (!mi.IsDefined(typeof(PythonHiddenAttribute), false)) {
                    ft |= FunctionType.AlwaysVisible;
                    break;
                }
            }

            return PythonTypeOps.GetFinalSlotForFunction(PythonTypeOps.GetBuiltinFunction(DeclaringType, 
                        Name,
                        ft,
                        meths.ToArray()
                    ));
        }
        private object CallGetter(CodeContext context, PythonType owner, SiteLocalStorage <CallSite <Func <CallSite, CodeContext, object, object> > > storage, object instance)
        {
            if (NeedToReturnProperty(instance, Getter))
            {
                return(this);
            }

            if (Getter.Length == 0)
            {
                throw new MissingMemberException("unreadable property");
            }

            if (owner == null)
            {
                owner = DynamicHelpers.GetPythonType(instance);
            }

            // this matches the logic in the default binder when it does a property get.  We
            // need to duplicate it here to be consistent for all gets.
            MethodInfo[] members = Getter;

            Type type = owner.UnderlyingSystemType;

            if (Getter.Length > 1)
            {
                // if we were given multiple members pick the member closest to the type...
                Type       bestMemberDeclaringType = Getter[0].DeclaringType;
                MethodInfo bestMember = Getter[0];

                for (int i = 1; i < Getter.Length; i++)
                {
                    MethodInfo mt = Getter[i];
                    if (!IsApplicableForType(type, mt))
                    {
                        continue;
                    }

                    if (Getter[i].DeclaringType.IsSubclassOf(bestMemberDeclaringType) ||
                        !IsApplicableForType(type, bestMember))
                    {
                        bestMember = Getter[i];
                        bestMemberDeclaringType = Getter[i].DeclaringType;
                    }
                }
                members = new MethodInfo[] { bestMember };
            }

            BuiltinFunction target = PythonTypeOps.GetBuiltinFunction(type, __name__, members);

            // Workaround for https://github.com/IronLanguages/ironpython3/issues/1326
            Debug.Assert(members.Length == 1);
            if (members[0].IsStatic)
            {
                instance = null;
            }

            return(target.Call0(context, storage, instance));
        }
예제 #4
0
        public override PythonTypeSlot GetSlot() {
            List<MethodBase> meths = new List<MethodBase>();
            foreach (MethodTracker mt in _trackers) {
                meths.Add(mt.Method);
            }

            return PythonTypeOps.GetFinalSlotForFunction(
                PythonTypeOps.GetBuiltinFunction(DeclaringType,
                        Name,
                        meths.ToArray()
                )
            );
        }
예제 #5
0
 private void MakeSetFunc()
 {
     _setfunc = PythonTypeOps.GetBuiltinFunction(DeclaringType, __name__, _setter);
 }
예제 #6
0
        internal object CallTarget(CodeContext context, SiteLocalStorage <CallSite <Func <CallSite, CodeContext, object, object[], object> > > storage, MethodInfo[] targets, object instance, params object[] args)
        {
            BuiltinFunction target = PythonTypeOps.GetBuiltinFunction(DeclaringType, __name__, targets);

            return(target.Call(context, storage, instance, args));
        }