ironpython2 icon indicating copy to clipboard operation
ironpython2 copied to clipboard

InvalidCastException when calling FirstOrDefault on a PythonList in .NET 6

Open slozier opened this issue 2 years ago • 4 comments

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.

slozier avatar Mar 10 '22 17:03 slozier

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?

Simon900225 avatar Dec 13 '22 15:12 Simon900225

.NET 6 added an overload FirstOrDefault<TSource>(IEnumerable<TSource>, TSource) 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)

slozier avatar Dec 14 '22 02:12 slozier

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 avatar Dec 08 '23 08:12 Simon900225

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.

Simon900225 avatar Dec 13 '23 15:12 Simon900225