示例#1
0
        public static object GetAttribute(CodeContext /*!*/ context, object self, string name, PythonTypeSlot getAttributeSlot, PythonTypeSlot getAttrSlot, SiteLocalStorage <CallSite <Func <CallSite, CodeContext, object, string, object> > > /*!*/ callSite)
        {
            object value;

            if (callSite.Data == null)
            {
                callSite.Data = MakeGetAttrSite(context);
            }

            try {
                if (getAttributeSlot.TryGetValue(context, self, ((IPythonObject)self).PythonType, out value))
                {
                    return(callSite.Data.Target(callSite.Data, context, value, name));
                }
            } catch (MissingMemberException) {
                if (getAttrSlot != null && getAttrSlot.TryGetValue(context, self, ((IPythonObject)self).PythonType, out value))
                {
                    return(callSite.Data.Target(callSite.Data, context, value, name));
                }

                throw;
            }

            if (getAttrSlot != null && getAttrSlot.TryGetValue(context, self, ((IPythonObject)self).PythonType, out value))
            {
                return(callSite.Data.Target(callSite.Data, context, value, name));
            }

            throw PythonOps.AttributeError(name);
        }
示例#2
0
            public object GetBindSlot(CallSite site, TSelfType target, CodeContext context)
            {
                if (target != null && target.GetType() == _type)
                {
                    object value;
                    _slot.TryGetValue(context, target, _owner, out value);
                    return(value);
                }

                return(((CallSite <Func <CallSite, TSelfType, CodeContext, object> >)site).Update(site, target, context));
            }
示例#3
0
        public static object GetAttributeNoThrow(CodeContext /*!*/ context, object self, string name, PythonTypeSlot getAttributeSlot, PythonTypeSlot getAttrSlot, SiteLocalStorage <CallSite <Func <CallSite, CodeContext, object, string, object> > > /*!*/ callSite)
        {
            object value;

            if (callSite.Data == null)
            {
                callSite.Data = MakeGetAttrSite(context);
            }

            try {
                if (getAttributeSlot.TryGetValue(context, self, ((IPythonObject)self).PythonType, out value))
                {
                    return(callSite.Data.Target(callSite.Data, context, value, name));
                }
            } catch (MissingMemberException) {
                try {
                    if (getAttrSlot != null && getAttrSlot.TryGetValue(context, self, ((IPythonObject)self).PythonType, out value))
                    {
                        ExceptionHelpers.DynamicStackFrames = null;
                        return(callSite.Data.Target(callSite.Data, context, value, name));
                    }

                    return(OperationFailed.Value);
                } catch (MissingMemberException) {
                    ExceptionHelpers.DynamicStackFrames = null;
                    return(OperationFailed.Value);
                }
            }

            try {
                if (getAttrSlot != null && getAttrSlot.TryGetValue(context, self, ((IPythonObject)self).PythonType, out value))
                {
                    return(callSite.Data.Target(callSite.Data, context, value, name));
                }
            } catch (MissingMemberException) {
                ExceptionHelpers.DynamicStackFrames = null;
            }

            return(OperationFailed.Value);
        }
示例#4
0
        public static object GetPropertyHelper(object prop, object instance, string name)
        {
            PythonTypeSlot desc = prop as PythonTypeSlot;

            if (desc == null)
            {
                throw PythonOps.TypeError("Expected property for {0}, but found {1}",
                                          name.ToString(), DynamicHelpers.GetPythonType(prop).Name);
            }
            object value;

            desc.TryGetValue(DefaultContext.Default, instance, DynamicHelpers.GetPythonType(instance), out value);
            return(value);
        }
示例#5
0
        public static object GetCustomMember(CodeContext /*!*/ context, NamespaceTracker /*!*/ self, string name)
        {
            if (self.TryGetValue(name, out MemberTracker mt))
            {
                if (mt.MemberType == TrackerTypes.Namespace || mt.MemberType == TrackerTypes.TypeGroup)
                {
                    return(mt);
                }

                PythonTypeSlot pts = PythonTypeOps.GetSlot(new MemberGroup(mt), name, context.LanguageContext.Binder.PrivateBinding);
                if (pts != null && pts.TryGetValue(context, null, TypeCache.PythonType, out object value))
                {
                    return(value);
                }
            }

            return(OperationFailed.Value);
        }
示例#6
0
        public static void AddRemoveEventHelper(object method, IPythonObject instance, object eventValue, string name)
        {
            object callable = method;

            // TODO: dt gives us a PythonContext which we should use
            PythonType     dt  = instance.PythonType;
            PythonTypeSlot dts = method as PythonTypeSlot;

            if (dts != null)
            {
                if (!dts.TryGetValue(DefaultContext.Default, instance, dt, out callable))
                {
                    throw PythonOps.AttributeErrorForMissingAttribute(dt.Name, name);
                }
            }

            if (!PythonOps.IsCallable(DefaultContext.Default, callable))
            {
                throw PythonOps.TypeError("Expected callable value for {0}, but found {1}", name.ToString(),
                                          PythonTypeOps.GetName(method));
            }

            PythonCalls.Call(callable, eventValue);
        }