-
Notifications
You must be signed in to change notification settings - Fork 635
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
Decode Python list into IList in function calls #10865
Changes from 8 commits
6423e4e
69be4b6
f447f2d
d46aaf4
8f1e71c
cced161
28d77b8
7c88e8b
81c724f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using Dynamo.Utilities; | ||
using Python.Runtime; | ||
|
||
namespace DSCPython.Encoders | ||
{ | ||
internal class ListEncoder : IPyObjectEncoder, IPyObjectDecoder | ||
{ | ||
private static readonly Type[] decodableTypes = new Type[] | ||
{ | ||
typeof(IList), typeof(ArrayList), | ||
typeof(IList<>), typeof(List<>) | ||
}; | ||
|
||
public bool CanDecode(PyObject objectType, Type targetType) | ||
{ | ||
if (targetType.IsGenericType) | ||
{ | ||
targetType = targetType.GetGenericTypeDefinition(); | ||
} | ||
return decodableTypes.IndexOf(targetType) >= 0; | ||
} | ||
|
||
public bool CanEncode(Type type) | ||
{ | ||
return typeof(IList).IsAssignableFrom(type); | ||
} | ||
|
||
public bool TryDecode<T>(PyObject pyObj, out T value) | ||
{ | ||
if (!PySequence.IsSequenceType(pyObj)) | ||
{ | ||
value = default; | ||
return false; | ||
} | ||
|
||
using (var pyList = PyList.AsList(pyObj)) | ||
{ | ||
if (typeof(T).IsGenericType) | ||
{ | ||
value = pyList.ToList<T>(); | ||
} | ||
else | ||
{ | ||
value = (T)pyList.ToList(); | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
public PyObject TryEncode(object value) | ||
{ | ||
// This is a no-op to prevent Python.NET from encoding generic lists | ||
// https://github.com/pythonnet/pythonnet/pull/963#issuecomment-642938541 | ||
return PyObject.FromManagedObject(value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this break examples where I want to call a python function with a list of ints?
where data is a .net There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question. Let me try it and come back with the answer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure, nothing wrong with more tests? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay, so l2 is a .net list because Flatten is a zt function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it's |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused by this using statement, does
AsList
return a .net list or a Python List?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a Python list