IronPython 抛出以下异常:无法将 Func<PythonFunction,Object> 类型的对象转换为 Func<CodeContext, CodeContext>
IronPython is throwing the following Exception: Unable to cast object of type Func<PythonFunction,Object> to Func<CodeContext, CodeContext>
我们在我们的软件中嵌入了 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 源代码,当它试图将 funcCode.Target
转换为 IronPython.Runtime.Operations.PythonOps 中的 Func<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 期望它是 Func<CodeContext, CodeContext>
类型,但由于某种原因它是 Func<PythonFunction, Object>
类型。但是我不知道该委托如何或为什么会被设置为不同类型的 Func。
客户的代码看起来很无害。
我无法在我的开发环境中重新创建异常,但此客户经常发生这种情况。
有什么我可以尝试的吗?或者这是我应该向 IronPython 人员提交的错误。如果是这样,我该如何向 IronPython 人员提交错误?
我还应该提到这是 IronPython2,而不是 3。
这是 IronPython 中的一个错误。
能够首先重现我们软件中的错误,然后 simple console app. I reported the bug 给 IronPython 团队。
我们在我们的软件中嵌入了 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 源代码,当它试图将 funcCode.Target
转换为 IronPython.Runtime.Operations.PythonOps 中的 Func<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 期望它是 Func<CodeContext, CodeContext>
类型,但由于某种原因它是 Func<PythonFunction, Object>
类型。但是我不知道该委托如何或为什么会被设置为不同类型的 Func。
客户的代码看起来很无害。 我无法在我的开发环境中重新创建异常,但此客户经常发生这种情况。
有什么我可以尝试的吗?或者这是我应该向 IronPython 人员提交的错误。如果是这样,我该如何向 IronPython 人员提交错误?
我还应该提到这是 IronPython2,而不是 3。
这是 IronPython 中的一个错误。
能够首先重现我们软件中的错误,然后 simple console app. I reported the bug 给 IronPython 团队。