- Notifications
You must be signed in to change notification settings - Fork768
Improve method binding#974
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
Closed
Uh oh!
There was an error while loading.Please reload this page.
Closed
Changes from1 commit
Commits
Show all changes
11 commits Select commitHold shift + click to select a range
21528ee Add test for ienumerable method
koubaab4d60c6 Add tests for collection & task/action from function
koubaa3f02abf Add more tests
koubaa9c3e1c5 Implement list converter
koubaab9db0f6 Implement action converter
koubaa4b0a1ad add TODO
koubaa8bed1bb Update changelog
koubaaa4599d9 share common code between ToArray and ToList
koubaa96fd896 improve ref counting
koubaac6fd768 revert whitespace
koubaae63d11e list wrapper
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
share common code between ToArray and ToList
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commita4599d922bf0742f35e76f720ba0fefcdadf86af
There are no files selected for viewing
88 changes: 34 additions & 54 deletionssrc/runtime/converter.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 |
|---|---|---|
| @@ -864,44 +864,28 @@ private static void SetConversionError(IntPtr value, Type target) | ||
| Exceptions.SetError(Exceptions.TypeError, $"Cannot convert {src} to {target}"); | ||
| } | ||
| private static bool ToListOfT(IntPtr value, Type elementType, out object result) | ||
| { | ||
| result = null; | ||
| bool IsSeqObj = Runtime.PySequence_Check(value); | ||
| var len = IsSeqObj ? Runtime.PySequence_Size(value) : -1; | ||
| IntPtr IterObject = Runtime.PyObject_GetIter(value); | ||
| if (IterObject == IntPtr.Zero) | ||
| return false; | ||
| var listType = typeof(List<>); | ||
| var constructedListType = listType.MakeGenericType(elementType); | ||
| IList list = IsSeqObj ? (IList)Activator.CreateInstance(constructedListType, new Object[] {(int)len}) : | ||
| (IList)Activator.CreateInstance(constructedListType); | ||
| IntPtr item; | ||
| while ((item = Runtime.PyIter_Next(IterObject)) != IntPtr.Zero) { | ||
| object obj = null; | ||
| if (!Converter.ToManaged(item, elementType, out obj, true)) { | ||
| Runtime.XDecref(item); | ||
koubaa marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| return false; | ||
| } | ||
| @@ -910,12 +894,34 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s | ||
| Runtime.XDecref(item); | ||
| } | ||
| Runtime.XDecref(IterObject); | ||
| result = list; | ||
| return true; | ||
| } | ||
| /// <summary> | ||
| /// Convert a Python value to a correctly typed managed array instance. | ||
| /// The Python value must support the Python iterator protocol or and the | ||
| /// items in the sequence must be convertible to the target array type. | ||
| /// </summary> | ||
| private static bool ToArray(IntPtr value, Type obType, out object result, bool setError) | ||
| { | ||
| Type elementType = obType.GetElementType(); | ||
| result = null; | ||
| bool success = ToListOfT(value, elementType, out result); | ||
| if (!success) | ||
| { | ||
| if (setError) | ||
| SetConversionError(value, obType); | ||
| return false; | ||
| } | ||
| IList list = (IList)result; | ||
| Array items = Array.CreateInstance(elementType, list.Count); | ||
| list.CopyTo(items, 0); | ||
| result = items; | ||
| return true; | ||
| } | ||
| /// <summary> | ||
| @@ -963,39 +969,13 @@ private static bool ToAction(IntPtr value, Type obType, out object result, bool | ||
| /// </summary> | ||
| private static bool ToList(IntPtr value, Type obType, out object result, bool setError) { | ||
| Type elementType = obType.GetGenericArguments()[0]; | ||
| var success = ToListOfT(value, elementType, out result); | ||
| if (!success) | ||
| { | ||
| if (setError) | ||
| SetConversionError(value, obType); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
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.