forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 31
Implements KeyValuePairEnumerableObject #42
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| package: | ||
| name: pythonnet | ||
| version: "1.0.5.29" | ||
| version: "1.0.5.30" | ||
|
|
||
| build: | ||
| skip: True # [not win] | ||
|
|
||
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
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
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
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
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
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,141 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Reflection; | ||
|
|
||
| namespace Python.Runtime | ||
| { | ||
| /// <summary> | ||
| /// Implements a Python type for managed dictionaries. This type is essentially | ||
| /// the same as a ClassObject, except that it provides sequence semantics | ||
| /// to support natural dictionary usage (__contains__ and __len__) from Python. | ||
| /// </summary> | ||
| internal class DictionaryObject : ClassObject | ||
| { | ||
| private static Dictionary<Tuple<Type, string>, MethodInfo> methodsByType = new Dictionary<Tuple<Type, string>, MethodInfo>(); | ||
| private static Dictionary<string, string> methodMap = new Dictionary<string, string> | ||
| { | ||
| { "mp_length", "Count" }, | ||
| { "sq_contains", "ContainsKey" } | ||
| }; | ||
|
|
||
| public List<string> MappedMethods { get; } = new List<string>(); | ||
|
|
||
| internal DictionaryObject(Type tp) : base(tp) | ||
| { | ||
| if (!tp.IsDictionary()) | ||
| { | ||
| throw new ArgumentException("object is not a dict"); | ||
| } | ||
|
|
||
| foreach (var name in methodMap) | ||
| { | ||
| var key = Tuple.Create(type, name.Value); | ||
| MethodInfo method; | ||
| if (!methodsByType.TryGetValue(key, out method)) | ||
| { | ||
| method = tp.GetMethod(name.Value); | ||
| if (method == null) | ||
| { | ||
| method = tp.GetMethod($"get_{name.Value}"); | ||
| } | ||
| if (method == null) | ||
| { | ||
| continue; | ||
| } | ||
| methodsByType.Add(key, method); | ||
| } | ||
|
|
||
| MappedMethods.Add(name.Key); | ||
| } | ||
| } | ||
|
|
||
| internal override bool CanSubclass() => false; | ||
|
|
||
| /// <summary> | ||
| /// Implements __len__ for dictionary types. | ||
| /// </summary> | ||
| public static int mp_length(IntPtr ob) | ||
| { | ||
| var obj = (CLRObject)GetManagedObject(ob); | ||
| var self = obj.inst; | ||
|
|
||
| MethodInfo methodInfo; | ||
| if (!TryGetMethodInfo(self.GetType(), "Count", out methodInfo)) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| return (int)methodInfo.Invoke(self, null); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Implements __contains__ for dictionary types. | ||
| /// </summary> | ||
| public static int sq_contains(IntPtr ob, IntPtr v) | ||
| { | ||
| var obj = (CLRObject)GetManagedObject(ob); | ||
| var self = obj.inst; | ||
|
|
||
| MethodInfo methodInfo; | ||
| if (!TryGetMethodInfo(self.GetType(), "ContainsKey", out methodInfo)) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| var parameters = methodInfo.GetParameters(); | ||
| object arg; | ||
| if (!Converter.ToManaged(v, parameters[0].ParameterType, out arg, false)) | ||
| { | ||
| Exceptions.SetError(Exceptions.TypeError, | ||
| $"invalid parameter type for sq_contains: should be {Converter.GetTypeByAlias(v)}, found {parameters[0].ParameterType}"); | ||
| } | ||
|
|
||
| return (bool)methodInfo.Invoke(self, new[] { arg }) ? 1 : 0; | ||
| } | ||
|
|
||
| private static bool TryGetMethodInfo(Type type, string alias, out MethodInfo methodInfo) | ||
| { | ||
| var key = Tuple.Create(type, alias); | ||
|
|
||
| if (!methodsByType.TryGetValue(key, out methodInfo)) | ||
| { | ||
| Exceptions.SetError(Exceptions.TypeError, | ||
| $"{nameof(type)} does not define {alias} method"); | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
|
|
||
| public static class DictionaryObjectExtension | ||
| { | ||
| public static bool IsDictionary(this Type type) | ||
| { | ||
| var iEnumerableType = typeof(IEnumerable<>); | ||
| var keyValuePairType = typeof(KeyValuePair<,>); | ||
|
Martin-Molinero marked this conversation as resolved.
|
||
|
|
||
| var interfaces = type.GetInterfaces(); | ||
| foreach (var i in interfaces) | ||
| { | ||
| if (i.IsGenericType && | ||
| i.GetGenericTypeDefinition() == iEnumerableType) | ||
| { | ||
| var arguments = i.GetGenericArguments(); | ||
| if (arguments.Length != 1) continue; | ||
|
|
||
| var a = arguments[0]; | ||
| if (a.IsGenericType && | ||
| a.GetGenericTypeDefinition() == keyValuePairType && | ||
| a.GetGenericArguments().Length == 2) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
| } | ||
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
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
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,106 @@ | ||
| using System.Collections; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
|
||
| namespace Python.Test | ||
| { | ||
| /// <summary> | ||
| /// Supports units tests for dictionary __contains__ and __len__ | ||
| /// </summary> | ||
| public class PublicDictionaryTest | ||
| { | ||
| public IDictionary<string, int> items; | ||
|
|
||
| public PublicDictionaryTest() | ||
| { | ||
| items = new int[5] { 0, 1, 2, 3, 4 } | ||
| .ToDictionary(k => k.ToString(), v => v); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public class ProtectedDictionaryTest | ||
| { | ||
| protected IDictionary<string, int> items; | ||
|
|
||
| public ProtectedDictionaryTest() | ||
| { | ||
| items = new int[5] { 0, 1, 2, 3, 4 } | ||
| .ToDictionary(k => k.ToString(), v => v); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public class InternalDictionaryTest | ||
| { | ||
| internal IDictionary<string, int> items; | ||
|
|
||
| public InternalDictionaryTest() | ||
| { | ||
| items = new int[5] { 0, 1, 2, 3, 4 } | ||
| .ToDictionary(k => k.ToString(), v => v); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public class PrivateDictionaryTest | ||
| { | ||
| private IDictionary<string, int> items; | ||
|
|
||
| public PrivateDictionaryTest() | ||
| { | ||
| items = new int[5] { 0, 1, 2, 3, 4 } | ||
| .ToDictionary(k => k.ToString(), v => v); | ||
| } | ||
| } | ||
|
|
||
| public class InheritedDictionaryTest : IDictionary<string, int> | ||
| { | ||
| private readonly IDictionary<string, int> items; | ||
|
|
||
| public InheritedDictionaryTest() | ||
| { | ||
| items = new int[5] { 0, 1, 2, 3, 4 } | ||
| .ToDictionary(k => k.ToString(), v => v); | ||
| } | ||
|
|
||
| public int this[string key] | ||
| { | ||
| get { return items[key]; } | ||
| set { items[key] = value; } | ||
| } | ||
|
|
||
| public ICollection<string> Keys => items.Keys; | ||
|
|
||
| public ICollection<int> Values => items.Values; | ||
|
|
||
| public int Count => items.Count; | ||
|
|
||
| public bool IsReadOnly => false; | ||
|
|
||
| public void Add(string key, int value) => items.Add(key, value); | ||
|
|
||
| public void Add(KeyValuePair<string, int> item) => items.Add(item); | ||
|
|
||
| public void Clear() => items.Clear(); | ||
|
|
||
| public bool Contains(KeyValuePair<string, int> item) => items.Contains(item); | ||
|
|
||
| public bool ContainsKey(string key) => items.ContainsKey(key); | ||
|
|
||
| public void CopyTo(KeyValuePair<string, int>[] array, int arrayIndex) | ||
| { | ||
| items.CopyTo(array, arrayIndex); | ||
| } | ||
|
|
||
| public IEnumerator<KeyValuePair<string, int>> GetEnumerator() => items.GetEnumerator(); | ||
|
|
||
| public bool Remove(string key) => items.Remove(key); | ||
|
|
||
| public bool Remove(KeyValuePair<string, int> item) => items.Remove(item); | ||
|
|
||
| public bool TryGetValue(string key, out int value) => items.TryGetValue(key, out value); | ||
|
|
||
| IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.