,c#,ironpython"/>

首页 > 解决方案 > IronPython 抛出以下异常:无法转换 Func 类型的对象功能

问题描述

我们将 IronPython 嵌入到我们的软件中,并允许用户编写和运行他们自己的自定义 Python 脚本,我们在 IronPython 引擎中运行这些脚本。我们的一位用户遇到了以下异常:

Unable to cast object of type 'System.Func`2[IronPython.Runtime.PythonFunction,System.Object]' to type 'System.Func`2[IronPython.Runtime.CodeContext,IronPython.Runtime.CodeContext]'.

此异常在以下代码的第一行引发(显然有更多代码,但这是失败的行):

class User:
    pass

堆栈跟踪如下:

at IronPython.Runtime.Operations.PythonOps.GetClassCode(CodeContext context, FunctionCode funcCode, Func`2 body)
at IronPython.Runtime.Operations.PythonOps.MakeClass(FunctionCode funcCode, Func`2 body, CodeContext parentContext, String name, Object[] bases, String selfNames)
at Microsoft.Scripting.Interpreter.FuncCallInstruction`7.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
at IronPython.Compiler.PythonCallTargets.OriginalCallTarget1(PythonFunction function, Object arg0)
at IronPython.Runtime.FunctionCaller`1.Call1(CallSite site, CodeContext context, Object func, T0 arg0)
at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
at Microsoft.Scripting.Interpreter.DynamicInstruction`4.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
at IronPython.Compiler.PythonScriptCode.Run(Scope scope)
at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope)
at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink)
at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)
at Microsoft.Scripting.Hosting.ScriptEngine.Execute(String expression, ScriptScope scope)...

查看 IronPython 源代码,当它尝试在 IronPython.Runtime.Operations.PythonOps 中转换时显然失败funcCode.TargetFunc<CodeContext, CodeContext>

public static object MakeClass(FunctionCode funcCode, Func<CodeContext, CodeContext> body, CodeContext/*!*/ parentContext, string name, object[] bases, string selfNames) {
    Func<CodeContext, CodeContext> func = GetClassCode(parentContext, funcCode, body);

    return MakeClass(parentContext, name, bases, selfNames, func(parentContext).Dict);
}

private static Func<CodeContext, CodeContext> GetClassCode(CodeContext/*!*/ context, FunctionCode funcCode, Func<CodeContext, CodeContext> body) {
    if (body == null) {
        if (funcCode.Target == null) {
            funcCode.UpdateDelegate(context.LanguageContext, true);
        }
        return (Func<CodeContext, CodeContext>)funcCode.Target;
    } else {
        if (funcCode.Target == null) {
            funcCode.SetTarget(body);
            funcCode._normalDelegate = body;
        }
        return body;
    }
}

对我来说,这似乎是 IronPython 中的一个错误。但我不得不承认,就真正导致 IronPython 在那里发声的原因而言,我有点过头了。funcCode.Target是一个委托,IronPython 期望它是 typeFunc<CodeContext, CodeContext>但由于某种原因它是 type Func<PythonFunction, Object>。但是我不知道该 Delegate 如何或为什么会被设置为不同类型的 Func。

客户的代码似乎很无害。我无法在我的开发环境中重新创建异常,但是这个客户经常发生这种情况。

有什么我可以尝试的吗?或者这是我应该向 IronPython 人员提交的错误。如果是这样,我如何向 IronPython 人员提交错误?

我还应该提到这是 IronPython2,而不是 3。

标签: c#ironpython

解决方案


这是 IronPython 中的一个错误。

能够首先在我们的软件中重现该错误,然后在一个简单的控制台应用程序中重现。我向 IronPython 团队报告了这个错误。


推荐阅读