pythonnet: List conversion fails on new version of pythonnet compared to 3.0.0.post1

Environment

  • Pythonnet version: Works with pythonnet==3.0.0.post1 Failes with pythonnet>=3.0.0.post1

  • Python version: Python 3.8.0

  • Operating System: Windows

  • .NET Runtime: netstandard2.0;net471

Details

I followed the notes to write a codec to convert a C# List<strings> to a Python list, here it is:

  /// <summary>
   /// This class will convert a C# list of strings to a Python list [] of strings.
   /// </summary>
   public class ListStringConversions : IPyObjectEncoder
   {
       bool IPyObjectEncoder.CanEncode(Type type)
       {
           return type == typeof(IList<string>);
       }

       PyObject IPyObjectEncoder.TryEncode(object value)
       {
           using (var scope = Py.CreateScope())
           {
               scope.Exec($"a = []");
               var input = (List<string>)value;
               foreach (var item in input)
                   scope.Exec($"a.append('{item}')");  // {item}
               return scope.Get("a");
           }
       }
   }

I have a simple test which calls in to a C# assembly and returns a list. With pythonnet version 3.0.0.post1, the C# list is successfully converted to a python list.

In my requirements file, I had this pythonnet>=3.0.0.post1

Now on the CICD process it has updated the pythonnet package and the same test fails:

>       assert isinstance(codes, list)
E       assert False
E        +  where False = isinstance(<System.Collections.Generic.IList[String] object at 0x0000023C1D9D5180>, list)

The code no longer is returning a python list. I switched the requirements file to pythonnet==3.0.0.post1

and the test passes. As such I think there is a breaking change in the latest release that prevents the conversions been executed.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 19 (15 by maintainers)

Most upvoted comments

I made you a sample project

https://github.com/screig/pythonnet_test

It runs the tests under Github actions, there are two jobs. Which are identical apart from, one does this

   pip install pythonnet==3.0.0.post1

and the other, does this:

   pip install pythonnet==3.0.1

https://github.com/screig/pythonnet_test/actions/runs/3418546969

As you can see 3.0.1 fails