Inheritance: IronPython.Runtime.Types.PythonTypeSlot, IWeakReferenceable, IPythonMembersList, IDynamicMetaObjectProvider, ICodeFormattable, Binding.IFastInvokable
示例#1
0
        private bool IsValidCall(string[] argNames, object[] defaultVals)
        {
            for (int i = 0; i < argNames.Length; i++)
            {
                if (!haveArg[i])
                {
                    if (defaultVals != null && i < defaultVals.Length && defaultVals[i] != DBNull.Value)
                    {
                        realArgs[i] = defaultVals[i];
                        haveArg[i]  = true;
                    }
                    else
                    {
                        int realDefaultsCount = 0;
                        for (int d = 0; d < GetNormalArgumentsCount(); d++)
                        {
                            if (defaultVals[d] != DBNull.Value)
                            {
                                realDefaultsCount++;
                            }
                        }

                        error = PythonFunction.TypeErrorForIncorrectArgumentCount(methodName, GetNormalArgumentsCount(), realDefaultsCount, arguments.Length - kwNames.Length, paramArrayIndex != -1, true);
                        return(false);
                    }
                }
            }
            return(true);
        }
        public object Call(object arg0, object arg1, object arg2, object arg3, object arg4)
        {
            PythonFunction f = func as PythonFunction;

            if (inst != null)
            {
                if (f != null)
                {
                    return(f.Call(inst, arg0, arg1, arg2, arg3, arg4));
                }
                return(Ops.Call(func, inst, arg0, arg1, arg2, arg3, arg4));
            }
            else
            {
                if (!Modules.Builtin.IsInstance(arg0, DeclaringClass))
                {
                    throw BadSelf(arg0);
                }
                if (f != null)
                {
                    return(f.Call(arg0, arg1, arg2, arg3, arg4));
                }
                return(Ops.Call(func, arg0, arg1, arg2, arg3, arg4));
            }
        }
示例#3
0
        private NamespaceDictionary CreateNamespaceDictionary(IDictionary <object, object> dict)
        {
            string[]   names    = (string[])this.type.GetField(NewTypeMaker.VtableNamesField).GetValue(null);
            SymbolId[] symNames = new SymbolId[names.Length];
            for (int i = 0; i < symNames.Length; i++)
            {
                symNames[i] = SymbolTable.StringToId(names[i]);
            }
            NamespaceDictionary ret = NamespaceDictionary.Make(symNames, bases);

            foreach (KeyValuePair <object, object> kv in dict)
            {
                PythonFunction func = kv.Value as PythonFunction;
                if (func != null && func.Name != "__new__")
                {
                    ret.AsObjectKeyedDictionary()[kv.Key] = new Method(func, null, this);
                }
                else
                {
                    ret.AsObjectKeyedDictionary()[kv.Key] = kv.Value;
                }
            }
            //!!! need invalidation, inheritance, all that good stuff
            return(ret);
        }
示例#4
0
 private IntPtr StoreTyped(PythonFunction func)
 {
     uint size = (uint)Marshal.SizeOf(typeof(PyFunctionObject));
     IntPtr ptr = this.allocator.Alloc(size);
     CPyMarshal.Zero(ptr, size);
     CPyMarshal.WriteIntField(ptr, typeof(PyIntObject), "ob_refcnt", 1);
     CPyMarshal.WritePtrField(ptr, typeof(PyIntObject), "ob_type", this.PyFunction_Type);
     this.map.Associate(ptr, func);
     return ptr;
 }
示例#5
0
文件: mp.cs 项目: amnek0/mpv.net
 public static void unregister_event(PyRT.PythonFunction pyFunc)
 {
     foreach (var eventObjects in PythonEventObjects)
     {
         if (eventObjects.PythonFunction == pyFunc)
         {
             eventObjects.EventInfo.RemoveEventHandler(eventObjects, eventObjects.Delegate);
         }
     }
 }
示例#6
0
 public static void Schedule(PythonFunction func, double Time)
 {
     Timer timer = new Timer();
     timer.Interval = Time * 1000;
     timer.Elapsed += delegate(object sender, ElapsedEventArgs args){
         timer.Stop();
         ResourceManager.Engine.Operations.Invoke(func);
     };
     timer.Start();
 }
示例#7
0
        protected Exception BadArgumentError(int count)
        {
            int min = Int32.MaxValue;
            int max = Int32.MinValue;

            for (int i = 0; i < targets.Length; i++)
            {
                ParameterInfo[] pis = targets[i].GetParameters();

                if (min > pis.Length)
                {
                    min = pis.Length;
                }
                if (max < pis.Length)
                {
                    max = pis.Length;
                }

                for (int j = 0; j < pis.Length; j++)
                {
                    if (ReflectionUtil.IsParamArray(pis[j]))
                    {
                        max = Int32.MaxValue;
                        if (min == pis.Length)
                        {
                            min--;
                        }
                    }
                    else if (ReflectionUtil.IsParamDict(pis[j]))
                    {
                        max = Int32.MaxValue;
                        if (min == pis.Length)
                        {
                            min--;
                        }
                    }
                }
            }

            if (min == max)
            {
                throw PythonFunction.TypeErrorForIncorrectArgumentCount(FriendlyName, min, 0, count);
            }
            else if (max == Int32.MaxValue)
            {
                throw PythonFunction.TypeErrorForIncorrectArgumentCount(FriendlyName, min, 0, count, true, false);
            }
            else
            {
                throw PythonFunction.TypeErrorForIncorrectArgumentCount(FriendlyName, max, max - min, count);
            }
        }
示例#8
0
 public virtual object GetAttribute(object instance, object owner)
 {
     if (instance != null)
     {
         return(new Method(this, instance, owner));
     }
     else
     {
         PythonFunction res = Clone() as PythonFunction;
         res.doc = FunctionDoc;
         return(res);
     }
 }
示例#9
0
 internal FunctionCode(PythonFunction f, FunctionInfo funcInfo) {
     _func = f;
     _filename = funcInfo.Path;
     object fn;
     if (_filename == null && f.Context.GlobalScope.Dict.TryGetValue(Symbols.File, out fn) && fn is string) {
         _filename = (string)fn;
     }
     _lineNo = funcInfo.LineNumber;
     _flags = funcInfo.Flags;
     _lambda = funcInfo.Code;
     _shouldInterpret = funcInfo.ShouldInterpret;
     _emitDebugSymbols = funcInfo.EmitDebugSymbols;
 }
示例#10
0
        private Exception BadArgumentError(int realArgCount)
        {
            int            argCount = 1;
            int            defaults = 0;
            PythonFunction funcfunc = func as PythonFunction;

            if (funcfunc != null)
            {
                argCount = funcfunc.ArgCount;
            }

            throw PythonFunction.TypeErrorForIncorrectArgumentCount(this.Name, argCount, defaults, realArgCount);
        }
        static ExceptionConverter()
        {
            exceptionInitMethod    = new FunctionX(null, "__init__", new CallTargetN(ExceptionConverter.ExceptionInit), new string[] { "args" }, new object[0], IronPython.Compiler.FuncDefType.ArgList);
            exceptionGetItemMethod = new FunctionX(null, "__getitem__", new CallTargetN(ExceptionConverter.ExceptionGetItem), new string[] { "args" }, new object[0], IronPython.Compiler.FuncDefType.ArgList);
            exceptionStrMethod     = new FunctionX(null, "__str__", new CallTargetN(ExceptionConverter.ExceptionToString), new string[] { "args" }, new object[0], IronPython.Compiler.FuncDefType.ArgList);
            syntaxErrorStrMethod   = new FunctionX(null, "__str__",
                                                   new CallTargetN(ExceptionConverter.SyntaxErrorToString), new string[] { "args" }, new object[0], IronPython.Compiler.FuncDefType.ArgList);

            for (int i = 0; i < exceptionMappings.Length; i++)
            {
                CreateExceptionMapping(null, exceptionMappings[i]);
            }

            // we also have a couple of explicit bonus mappings.
            clrToPython[typeof(InvalidCastException)]  = GetPythonExceptionByName("TypeError");
            clrToPython[typeof(ArgumentNullException)] = GetPythonExceptionByName("TypeError");
        }
示例#12
0
        internal PythonGenerator(PythonFunction function, Func <MutableTuple, object> /*!*/ next, MutableTuple data)
        {
            _function  = function;
            _next      = next;
            _data      = data;
            _dataTuple = GetDataTuple();
            State      = GeneratorRewriter.NotStarted;

            if (_LastFinalizer == null || (_finalizer = Interlocked.Exchange(ref _LastFinalizer, null)) == null)
            {
                _finalizer = new GeneratorFinalizer(this);
            }
            else
            {
                _finalizer.Generator = this;
            }
        }
示例#13
0
        public override ICollection <OverloadDoc> GetOverloads(object value)
        {
            BuiltinFunction bf = value as BuiltinFunction;

            if (bf != null)
            {
                return(GetBuiltinFunctionOverloads(bf));
            }

            BuiltinMethodDescriptor bmd = value as BuiltinMethodDescriptor;

            if (bmd != null)
            {
                return(GetBuiltinFunctionOverloads(bmd.Template));
            }

            PythonFunction pf = value as PythonFunction;

            if (pf != null)
            {
                return(new[] {
                    new OverloadDoc(
                        pf.__name__,
                        pf.__doc__ as string,
                        GetParameterDocs(pf)
                        )
                });
            }

            Method method = value as Method;

            if (method != null)
            {
                return(GetOverloads(method.__func__));
            }

            Delegate dlg = value as Delegate;

            if (dlg != null)
            {
                return(new[] { DocBuilder.GetOverloadDoc(dlg.GetType().GetMethod("Invoke"), dlg.GetType().Name, 0, false) });
            }

            return(new OverloadDoc[0]);
        }
示例#14
0
        private void PromoteFunctionsToMethods()
        {
            List <KeyValuePair <SymbolId, object> > updates = new List <KeyValuePair <SymbolId, object> >(__dict__.Count);

            foreach (KeyValuePair <object, object> item in __dict__)
            {
                PythonFunction func = item.Value as PythonFunction;
                if (func != null)
                {
                    SymbolId key = SymbolTable.StringToId(item.Key as string);
                    updates.Add(new KeyValuePair <SymbolId, object>(key, new Method(func, null, this)));
                }
            }

            for (int i = 0; i < updates.Count; i++)
            {
                __dict__[updates[i].Key] = updates[i].Value;
            }
        }
示例#15
0
        IList <object> IPythonMembersList.GetMemberNames(CodeContext /*!*/ context)
        {
            List ret = TypeCache.Method.GetMemberNames(context);

            ret.AddNoLockNoDups("__module__");

            PythonFunction pf = _func as PythonFunction;

            if (pf != null)
            {
                PythonDictionary dict = pf.func_dict;

                // Check the func
                foreach (KeyValuePair <object, object> kvp in dict)
                {
                    ret.AddNoLockNoDups(kvp.Key);
                }
            }

            return(ret);
        }
示例#16
0
        private static ICollection <ParameterDoc> GetParameterDocs(PythonFunction pf)
        {
            ParameterDoc[] res = new ParameterDoc[pf.ArgNames.Length];

            for (int i = 0; i < res.Length; i++)
            {
                ParameterFlags flags = ParameterFlags.None;
                if (i == pf.ExpandDictPosition)
                {
                    flags |= ParameterFlags.ParamsDict;
                }
                else if (i == pf.ExpandListPosition)
                {
                    flags |= ParameterFlags.ParamsArray;
                }

                res[i] = new ParameterDoc(
                    pf.ArgNames[i],
                    flags
                    );
            }
            return(res);
        }
示例#17
0
文件: mp.cs 项目: amnek0/mpv.net
        public static void register_event(string name, PyRT.PythonFunction pyFunc)
        {
            foreach (var eventInfo in typeof(mp).GetEvents())
            {
                if (eventInfo.Name.ToLower() == name.Replace("-", ""))
                {
                    PythonEventObject eventObject = new PythonEventObject();
                    PythonEventObjects.Add(eventObject);
                    eventObject.PythonFunction = pyFunc;
                    MethodInfo mi;

                    if (eventInfo.EventHandlerType == typeof(Action))
                    {
                        mi = eventObject.GetType().GetMethod(nameof(PythonEventObject.Invoke));
                    }
                    else if (eventInfo.EventHandlerType == typeof(Action <EndFileEventMode>))
                    {
                        mi = eventObject.GetType().GetMethod(nameof(PythonEventObject.InvokeEndFileEventMode));
                    }
                    else if (eventInfo.EventHandlerType == typeof(Action <string[]>))
                    {
                        mi = eventObject.GetType().GetMethod(nameof(PythonEventObject.InvokeStrings));
                    }
                    else
                    {
                        throw new Exception();
                    }

                    eventObject.EventInfo = eventInfo;
                    Delegate handler = Delegate.CreateDelegate(eventInfo.EventHandlerType, eventObject, mi);
                    eventObject.Delegate = handler;
                    eventInfo.AddEventHandler(eventObject, handler);
                    break;
                }
            }
        }
示例#18
0
 public static object OriginalCallTargetN(PythonFunction function, params object[] args) {
     function.Target = function.func_code.Code.Compile();
     return ((CallTargetN)function.Target)(function, args);
 }
示例#19
0
        // *** BEGIN GENERATED CODE ***
        // generated by function: gen_lazy_call_targets from: generate_calls.py

        public static object OriginalCallTarget0(PythonFunction function) {
            function.Target = function.func_code.GetCompiledCode();
            return ((CallTarget0)function.Target)(function);
        }
示例#20
0
 public object CallTarget(PythonFunction/*!*/ function, object arg0, object arg1, object arg2, object arg3) {
     PythonOps.FunctionPushFrame((PythonContext)function.Context.LanguageContext);
     try {
         return _target(function, arg0, arg1, arg2, arg3);
     } finally {
         PythonOps.FunctionPopFrame();
     }
 }
示例#21
0
 public static object OriginalCallTarget16(PythonFunction function, object arg0, object arg1, object arg2, object arg3, object arg4, object arg5, object arg6, object arg7, object arg8, object arg9, object arg10, object arg11, object arg12, object arg13, object arg14, object arg15) {
     function.Target = function.func_code.GetCompiledCode();
     return ((CallTarget16)function.Target)(function, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15);
 }
示例#22
0
 public FunctionCode(PythonFunction f)
 {
     this.func = f;
 }
        private static ICollection<ParameterDoc> GetParameterDocs(PythonFunction pf) {
            ParameterDoc[] res = new ParameterDoc[pf.ArgNames.Length];

            for (int i = 0; i < res.Length; i++) {
                ParameterFlags flags = ParameterFlags.None;
                if (i == pf.ExpandDictPosition) {
                    flags |= ParameterFlags.ParamsDict;
                } else if (i == pf.ExpandListPosition) {
                    flags |= ParameterFlags.ParamsArray;
                }

                res[i] = new ParameterDoc(
                    pf.ArgNames[i],
                    flags
                );
            }
            return res;
        }
示例#24
0
 public static object OriginalCallTarget6(PythonFunction function, object arg0, object arg1, object arg2, object arg3, object arg4, object arg5) {
     function.func_code.LazyCompileFirstTarget(function);
     return ((Func<PythonFunction, object, object, object, object, object, object, object>)function.func_code.Target)(function, arg0, arg1, arg2, arg3, arg4, arg5);
 }
示例#25
0
        internal object Call(CodeContext/*!*/ context) {
            if (co_freevars != PythonTuple.EMPTY) {
                throw PythonOps.TypeError("cannot exec code object that contains free variables: {0}", co_freevars.__repr__(context));
            }

            if (Target == null || (Target.GetMethod() != null && Target.GetMethod().DeclaringType == typeof(PythonCallTargets))) {
                UpdateDelegate(context.LanguageContext, true);
            }

            Func<CodeContext, CodeContext> classTarget = Target as Func<CodeContext, CodeContext>;
            if (classTarget != null) {
                return classTarget(context);
            }

            LookupCompilationDelegate moduleCode = Target as LookupCompilationDelegate;
            if (moduleCode != null) {
                return moduleCode(context, this);
            }

            Func<FunctionCode, object> optimizedModuleCode = Target as Func<FunctionCode, object>;
            if (optimizedModuleCode != null) {
                return optimizedModuleCode(this);
            }

            var func = new PythonFunction(context, this, null, ArrayUtils.EmptyObjects, new MutableTuple<object>());
            CallSite<Func<CallSite, CodeContext, PythonFunction, object>> site = PythonContext.GetContext(context).FunctionCallSite;
            return site.Target(site, context, func);
        }
示例#26
0
 public static object OriginalCallTarget12(PythonFunction function, object arg0, object arg1, object arg2, object arg3, object arg4, object arg5, object arg6, object arg7, object arg8, object arg9, object arg10, object arg11) {
     function.func_code.LazyCompileFirstTarget(function);
     return ((Func<PythonFunction, object, object, object, object, object, object, object, object, object, object, object, object, object>)function.func_code.Target)(function, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11);
 }
示例#27
0
 public static object OriginalCallTargetN(PythonFunction function, params object[] args) {
     function.func_code.LazyCompileFirstTarget(function);
     return ((Func<PythonFunction, object[], object>)function.func_code.Target)(function, args);
 }
示例#28
0
        internal object Call(CodeContext/*!*/ context, Scope/*!*/ scope) {
            if (_code != null) {
                return _code.Run(scope);
            }

            if (_closureVars != PythonTuple.EMPTY) {
                throw PythonOps.TypeError("cannot exec code object that contains free variables");
            }

            var func = new PythonFunction(context, this, null, ArrayUtils.EmptyObjects, new MutableTuple<object>());
            CallSite<Func<CallSite, CodeContext, PythonFunction, object>> site = PythonContext.GetContext(context).FunctionCallSite;
            return site.Target(site, context, func);
        }
示例#29
0
 public static object Make(object cls, PythonFunction newFunction, object inst)
 {
     Debug.Assert(!(inst is InstanceArgument));
     return(new Method(newFunction, inst, null));
 }
 /// <summary>
 /// Create new action decorator
 /// </summary>
 /// <param name="pythonType"></param>
 public ActionDecorator(PythonFunction pythonType)
 {
 }
示例#31
0
 public static object OriginalCallTarget3(PythonFunction function, object arg0, object arg1, object arg2) {
     function.Target = function.func_code.GetCompiledCode();
     return ((CallTarget3)function.Target)(function, arg0, arg1, arg2);
 }
示例#32
0
 public object CallTarget(PythonFunction/*!*/ function, object arg0, object arg1, object arg2, object arg3, object arg4, object arg5, object arg6, object arg7, object arg8, object arg9, object arg10, object arg11, object arg12, object arg13, object arg14) {
     PythonOps.FunctionPushFrame((PythonContext)function.Context.LanguageContext);
     try {
         return _target(function, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14);
     } finally {
         PythonOps.FunctionPopFrame();
     }
 }
示例#33
0
 public static object OriginalCallTarget7(PythonFunction function, object arg0, object arg1, object arg2, object arg3, object arg4, object arg5, object arg6) {
     function.Target = function.func_code.GetCompiledCode();
     return ((CallTarget7)function.Target)(function, arg0, arg1, arg2, arg3, arg4, arg5, arg6);
 }
示例#34
0
 public static object OriginalCallTarget1(PythonFunction function, object arg0) {
     function.func_code.LazyCompileFirstTarget(function);
     return ((Func<PythonFunction, object, object>)function.func_code.Target)(function, arg0);
 }
示例#35
0
 /// <summary>
 /// Called the 1st time a function is invoked by our OriginalCallTarget* methods
 /// over in PythonCallTargets.  This computes the real delegate which needs to be
 /// created for the function.  Usually this means starting off interpretering.  It 
 /// also involves adding the wrapper function for recursion enforcement.
 /// 
 /// Because this can race against sys.settrace/setprofile we need to take our 
 /// _ThreadIsEnumeratingAndAccountingLock to ensure no one is actively changing all
 /// of the live functions.
 /// </summary>
 internal void LazyCompileFirstTarget(PythonFunction function) {
     lock (_CodeCreateAndUpdateDelegateLock) {
         UpdateDelegate(PythonContext.GetContext(function.Context), true);
     }
 }
示例#36
0
 protected override Exception BadArgumentError(int count)
 {
     throw PythonFunction.TypeErrorForIncorrectArgumentCount(Name, nparams, defaults.Length, count, argListPos != -1, false);
 }
示例#37
0
 internal FunctionCode(PythonFunction f) {
     _func = f;
 }
示例#38
0
 public static object InvokeFunction(IronPython.Runtime.PythonFunction fun, params object[] args)
 {
     return(ipy.Engine.Operations.Invoke(fun, args));
 }