Skip to content

Enumerable First/Last using Python list. #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
galpin opened this issue Dec 14, 2015 · 7 comments
Closed

Enumerable First/Last using Python list. #113

galpin opened this issue Dec 14, 2015 · 7 comments

Comments

@galpin
Copy link

galpin commented Dec 14, 2015

The following test currently crashes the runtime (stack trace included below).

class EnumerableTest(unittest.TestCase):
    def testFirstAndLast(self):
        values = [1, 2, 3]
        self.assertTrue(Enumerable.First(values) == 1)
        self.assertTrue(Enumerable.Last(values) == 3)
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at Python.Runtime.MethodBinder.MatchParameters(MethodInfo[] mi, Type[] tp)
   at Python.Runtime.MethodBinder.Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, MethodInfo[] methodinfo)
   at Python.Runtime.MethodBinder.Invoke(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, MethodInfo[] methodinfo)
   at Python.Runtime.MethodObject.Invoke(IntPtr target, IntPtr args, IntPtr kw, MethodBase info)
   at Python.Runtime.MethodBinding.tp_call(IntPtr ob, IntPtr args, IntPtr kw)
   at Python.Runtime.Runtime.Py_Main(Int32 argc, String[] argv)
   at Python.Runtime.PythonConsole.Main(String[] args)

Do you expect this to work correctly?

Thanks,

@den-run-ai
Copy link
Contributor

values = [1, 2, 3] is PyList and hence only implements IEnumerable basic interface (not generic):

https://msdn.microsoft.com/en-us/library/system.collections.ienumerable(v=vs.110).aspx

@tonyroberts
Copy link
Contributor

@denfromufa it shouldn't crash the runtime though, it should raise a Python exception if there's a problem.

@galpin
Copy link
Author

galpin commented Jan 22, 2016

@tonyroberts Quite.

@den-run-ai
Copy link
Contributor

full traceback from debug build (after tp_call):

>   Python.Runtime.dll!Python.Runtime.MethodBinder.MatchParameters(System.Reflection.MethodInfo[] mi, System.Type[] tp) Line 77 C#
    Python.Runtime.dll!Python.Runtime.MethodBinder.Bind(System.IntPtr inst, System.IntPtr args, System.IntPtr kw, System.Reflection.MethodBase info, System.Reflection.MethodInfo[] methodinfo) Line 345    C#
    Python.Runtime.dll!Python.Runtime.MethodBinder.Invoke(System.IntPtr inst, System.IntPtr args, System.IntPtr kw, System.Reflection.MethodBase info, System.Reflection.MethodInfo[] methodinfo) Line 363  C#
    Python.Runtime.dll!Python.Runtime.MethodObject.Invoke(System.IntPtr target, System.IntPtr args, System.IntPtr kw, System.Reflection.MethodBase info) Line 65    C#
    Python.Runtime.dll!Python.Runtime.MethodBinding.tp_call(System.IntPtr ob, System.IntPtr args, System.IntPtr kw) Line 179    C#

autos:

        count   0   int
-       mi  {System.Reflection.MethodInfo[2]}   System.Reflection.MethodInfo[]
-       [0] {TSource First[TSource](System.Collections.Generic.IEnumerable`1[TSource])} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
        Attributes  Public | Static | HideBySig System.Reflection.MethodAttributes
        CallingConvention   Standard    System.Reflection.CallingConventions
        ContainsGenericParameters   true    bool
+       CustomAttributes    Count = 2   System.Collections.Generic.IEnumerable<System.Reflection.CustomAttributeData> {System.Collections.ObjectModel.ReadOnlyCollection<System.Reflection.CustomAttributeData>}
+       DeclaringType   {Name = "Enumerable" FullName = "System.Linq.Enumerable"}   System.Type {System.RuntimeType}
        IsAbstract  false   bool
        IsAssembly  false   bool
        IsConstructor   false   bool
        IsFamily    false   bool
        IsFamilyAndAssembly false   bool
        IsFamilyOrAssembly  false   bool
        IsFinal false   bool
        IsGenericMethod true    bool
        IsGenericMethodDefinition   true    bool
        IsHideBySig true    bool
        IsPrivate   false   bool
        IsPublic    true    bool
        IsSecurityCritical  false   bool
        IsSecuritySafeCritical  false   bool
        IsSecurityTransparent   true    bool
        IsSpecialName   false   bool
        IsStatic    true    bool
        IsVirtual   false   bool
        MemberType  Method  System.Reflection.MemberTypes
        MetadataToken   100665141   int
+       MethodHandle    {System.RuntimeMethodHandle}    System.RuntimeMethodHandle
        MethodImplementationFlags   IL  System.Reflection.MethodImplAttributes
+       Module  {System.Core.dll}   System.Reflection.Module {System.Reflection.RuntimeModule}
        Name    "First" string
+       ReflectedType   {Name = "Enumerable" FullName = "System.Linq.Enumerable"}   System.Type {System.RuntimeType}
+       ReturnParameter {TSource }  System.Reflection.ParameterInfo {System.Reflection.RuntimeParameterInfo}
+       ReturnType  {Name = "TSource" FullName = null}  System.Type {System.RuntimeType}
+       ReturnTypeCustomAttributes  {TSource }  System.Reflection.ICustomAttributeProvider {System.Reflection.RuntimeParameterInfo}
+       Non-Public members      
-       [1] {TSource First[TSource](System.Collections.Generic.IEnumerable`1[TSource], System.Func`2[TSource,System.Boolean])}  System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
        Attributes  Public | Static | HideBySig System.Reflection.MethodAttributes
        CallingConvention   Standard    System.Reflection.CallingConventions
        ContainsGenericParameters   true    bool
+       CustomAttributes    Count = 2   System.Collections.Generic.IEnumerable<System.Reflection.CustomAttributeData> {System.Collections.ObjectModel.ReadOnlyCollection<System.Reflection.CustomAttributeData>}
+       DeclaringType   {Name = "Enumerable" FullName = "System.Linq.Enumerable"}   System.Type {System.RuntimeType}
        IsAbstract  false   bool
        IsAssembly  false   bool
        IsConstructor   false   bool
        IsFamily    false   bool
        IsFamilyAndAssembly false   bool
        IsFamilyOrAssembly  false   bool
        IsFinal false   bool
        IsGenericMethod true    bool
        IsGenericMethodDefinition   true    bool
        IsHideBySig true    bool
        IsPrivate   false   bool
        IsPublic    true    bool
        IsSecurityCritical  false   bool
        IsSecuritySafeCritical  false   bool
        IsSecurityTransparent   true    bool
        IsSpecialName   false   bool
        IsStatic    true    bool
        IsVirtual   false   bool
        MemberType  Method  System.Reflection.MemberTypes
        MetadataToken   100665142   int
+       MethodHandle    {System.RuntimeMethodHandle}    System.RuntimeMethodHandle
        MethodImplementationFlags   IL  System.Reflection.MethodImplAttributes
+       Module  {System.Core.dll}   System.Reflection.Module {System.Reflection.RuntimeModule}
        Name    "First" string
+       ReflectedType   {Name = "Enumerable" FullName = "System.Linq.Enumerable"}   System.Type {System.RuntimeType}
+       ReturnParameter {TSource }  System.Reflection.ParameterInfo {System.Reflection.RuntimeParameterInfo}
+       ReturnType  {Name = "TSource" FullName = null}  System.Type {System.RuntimeType}
+       ReturnTypeCustomAttributes  {TSource }  System.Reflection.ICustomAttributeProvider {System.Reflection.RuntimeParameterInfo}
+       Non-Public members      
        tp  null    System.Type[]

@den-run-ai
Copy link
Contributor

using pull request #126 the following proper exception is raised:

>>> Enumerable.First(values)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: No method matches given arguments

@tonyroberts
Copy link
Contributor

thanks, that PR has been merged.

cheers,
Tony

@den-run-ai
Copy link
Contributor

@tonyroberts @galpin I added more similar checks in #129

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants