IronLanguages / ironpython2

Implementation of the Python programming language for .NET Framework; built on top of the Dynamic Language Runtime (DLR).
http://ironpython.net
Apache License 2.0
1.07k stars 229 forks source link

InvalidCastException when calling FirstOrDefault on a PythonList in .NET 6 #812

Open slozier opened 2 years ago

slozier commented 2 years ago

To reproduce:

import clr
import System
clr.AddReference("System.Linq")
clr.ImportExtensions(System.Linq)
[0].FirstOrDefault(lambda x: x == 2)

Fails with:

Error in IEnumeratorOfTWrapper.Current. Could not cast: from System.Int32 to IronPython.Runtime.PythonFunction

Works fine on .NET 5.0, fails on .NET 6.0. It looks like the T of IEnumeratorOfTWrapper<T> is PythonFunction instead of System.Object that we get on .NET 5.0.

Reported by @Simon900225 on gitter.

Simon900225 commented 1 year ago

Hi. I'd like to get this issue resolved in some way. Is there a way for my company to sponsor this? Or could I get some input on how I can troubleshoot, find and fix this issue myself?

slozier commented 1 year ago

.NET 6 added an overload FirstOrDefault<TSource>(IEnumerable<TSource>, TSource)-0)) so, other than being explicit about your type, I'm not sure there's a whole lot you can do for this one:

import clr
import System
clr.AddReference("System.Linq")
clr.ImportExtensions(System.Linq)
[0].FirstOrDefault[System.Int32](lambda x: x == 2)
Simon900225 commented 11 months ago

I'm back in the project where I had this issue after a years break. Being explicit works, but we have a lot of code in different places so I'd have to create a regex that adds [System.Object] after every FirstOrDefault (and similar calls) to be sure that it works for every customer. The thing is I don't fully understand why the new overload should mess this up? If I run the new overload range(1,10).FirstOrDefault(lambda x: x == 20, 1) there is no issue.

I have now downloaded the source of IronPython and managed to compile it so now I'm able to test a bit more. The thing is that I haven't manged to find where the FuncCallInstruction is generated where the return type is set to PythonFunction.

The difference of the FuncCallInstruction between .NET 5 and .NET 8 looks like this {Call(PythonFunction FirstOrDefault[PythonFunction](System.Collections.Generic.IEnumerable1[IronPython.Runtime.PythonFunction], IronPython.Runtime.PythonFunction))} {Call(System.Object FirstOrDefault[Object](System.Collections.Generic.IEnumerable1[System.Object], System.Func2[System.Object,System.Boolean]))}

Simon900225 commented 11 months ago

Another error found both when using the implicit and the explicitly typed call List[Object]().FirstOrDefault[System.Object](lambda x: x == 10) List[Object]().FirstOrDefault(lambda x: x == 10) returns <function <lambda$29> at 0x0000000000000033> when it should return None

This also stopped working from .NET 6 and forward. I think this is caused by the same error. It seems like it returns the lambda function and not the result of the lambda function. If you add the default value parameter as None it works but it should understand that implicitly.