-
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 Dictionary in function calls #10868
Conversation
Adds tests for the following conversion scenarios: - Python list => .NET IList (CPython fails) - .NET array => Python list - Python tuple => .NET array - Python tuple => .NET IList (CPython fails) - Python range => .NET array - Python range => .NET IList (CPython fails) - Python dict => .NET IDictionary (CPython fails) - .NET IDictionary => Python dict (CPython unkonwn) Also commented out some tests that fail in both engines but may be considered to fix in CPython: - .NET IList => Python list - Python array => .NET IList - Python dict => DS Dictionary Other types considered but not added as tests: - classes (can't convert them) - dictionary view objects (uncommon) - bytes, bytearray, memoryview (uncommon) - set, frozenset (uncommon) - complex (uncommon)
var decoders = shared.Cast<IPyObjectDecoder>().Concat(new IPyObjectDecoder[] | ||
{ | ||
new DictionaryDecoder() | ||
}).ToArray(); |
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.
@mmisol so it looks like we have both an encoder and a decoder for List but not for Dictionary, correct?
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.
Yes, that's technically correct. In practice, the encoder we have for List only has the purpose of preventing the default encoding Python.NET does for lists. By doing that we can get a consistent behaviour between List and Dictionary conversions.
@@ -6,7 +6,7 @@ | |||
|
|||
namespace DSCPython.Encoders | |||
{ | |||
internal class ListEncoder : IPyObjectEncoder, IPyObjectDecoder | |||
internal class ListEncoderDecoder : IPyObjectEncoder, IPyObjectDecoder |
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 see obsolete messages on IPyObjectEncoder
and Decoder saying they are unstable and can be changed in the next minor release - something to keep note of unless there is an alternative interface already existing that we can use and replace right away.
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.
The custom encoder/decoder API appeared with 2.5, which is the latest minor release of Python.NET. My guess is they might make changes to it in version 3.0 so you bring a good point.
Before this API there was no way to do something like this AFAIK. We chose to use the API where possible, even if it is unstable, because otherwise we would have to modify Python.NET even more. Also, from these encoders/decoders you could target custom types, like DS Dictionary, which is another plus.
|
||
OUT = d | ||
# Python dict => .NET IDictionary<> - Does not work in IronPython | ||
typedDictionary = DummyCollection.AcceptDictionary(d) |
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 understand that passing d
to AcceptDictionary
or AcceptIDictionary
is the decoding part to convert it into the .NET dictionary but what about returning the .net dictionary from AcceptDictionary
? Doesn't that need an encoding from the returned .NET dictionary to the Python dictionary?
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.
Are the returned untypedDictinoary
and typedDictionary
not Python dictionaries?
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 does trigger an encode operation but in this case we are not doing anything special. Python.NET will wrap .NET objects that it doesn't know how to encode with a generic wrapper. I haven't taken much of a look to their implementation, but they even support iterating like this for example https://github.com/DynamoDS/Dynamo/blob/master/test/Libraries/DynamoPythonTests/PythonEvalTestsWithLibraries.cs#L77
Purpose
Allows to decode Python dict to .NET IDictionary by using a Python.NET
decoder. Both generic and non-generic dictionaries are supported.
Declarations
Check these if you believe they are true
*.resx
filesReviewers
@DynamoDS/dynamo