- Notifications
You must be signed in to change notification settings - Fork749
implement list codec#1084
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
implement list codec#1084
Changes fromall commits
Commits
Show all changes
25 commits Select commitHold shift + click to select a range
b500aa1
begin to implement list codec
koubaaf80ae87
add a test
koubaa765b6a2
improve CanDecode
koubaa98ce49c
improve list codec
koubaab827f5a
full set of list codecs
koubaa224df0e
abandon rank and use three codecs
koubaa1600fdc
update
koubaac43446e
respond to PR comments
koubaa32fa32d
make decoders public
koubaadfc814b
Make reset public
koubaaf025cd1
add codec tests
koubaa24d78c3
respond to comments
koubaaebcfa30
fix brace
koubaa5cc575d
use unix line endings to avoid large diff
koubaa17c47a7
fix compiler error
koubaae8cc3d8
don't rethrow exception
koubaa080dbc3
cleanup
koubaa059ab08
fixes
koubaa432c09e
clean up sequence wrapper
koubaa3043201
respond to comments
koubaa57d7779
fix potential double free
koubaa7fb5915
fix exception
koubaa085c665
fix all exceptions
koubaabf2d038
respond to PR feedback
koubaa07ee9fb
use hasattr("__iter__")
koubaaFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
250 changes: 232 additions & 18 deletionssrc/embed_tests/Codecs.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletionssrc/runtime/Codecs/IterableDecoder.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
namespace Python.Runtime.Codecs | ||
{ | ||
public class IterableDecoder : IPyObjectDecoder | ||
{ | ||
internal static bool IsIterable(Type targetType) | ||
{ | ||
//if it is a plain IEnumerable, we can decode it using sequence protocol. | ||
if (targetType == typeof(System.Collections.IEnumerable)) | ||
return true; | ||
if (!targetType.IsGenericType) | ||
return false; | ||
return targetType.GetGenericTypeDefinition() == typeof(IEnumerable<>); | ||
} | ||
internal static bool IsIterable(PyObject objectType) | ||
{ | ||
return objectType.HasAttr("__iter__"); | ||
lostmsu marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
public bool CanDecode(PyObject objectType, Type targetType) | ||
{ | ||
return IsIterable(objectType) && IsIterable(targetType); | ||
} | ||
public bool TryDecode<T>(PyObject pyObj, out T value) | ||
{ | ||
//first see if T is a plan IEnumerable | ||
if (typeof(T) == typeof(System.Collections.IEnumerable)) | ||
{ | ||
object enumerable = new CollectionWrappers.IterableWrapper<object>(pyObj); | ||
value = (T)enumerable; | ||
return true; | ||
} | ||
var elementType = typeof(T).GetGenericArguments()[0]; | ||
var collectionType = typeof(CollectionWrappers.IterableWrapper<>).MakeGenericType(elementType); | ||
var instance = Activator.CreateInstance(collectionType, new[] { pyObj }); | ||
value = (T)instance; | ||
return true; | ||
} | ||
public static IterableDecoder Instance { get; } = new IterableDecoder(); | ||
public static void Register() | ||
{ | ||
PyObjectConversions.RegisterDecoder(Instance); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.