|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Reflection; |
| 4 | + |
| 5 | +namespace Python.Runtime |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Implements a Python type for managed KeyValuePairEnumerable (dictionaries). |
| 9 | + /// This type is essentially the same as a ClassObject, except that it provides |
| 10 | + /// sequence semantics to support natural dictionary usage (__contains__ and __len__) |
| 11 | + /// from Python. |
| 12 | + /// </summary> |
| 13 | + internal class KeyValuePairEnumerableObject : ClassObject |
| 14 | + { |
| 15 | + private static Dictionary<Tuple<Type, string>, MethodInfo> methodsByType = new Dictionary<Tuple<Type, string>, MethodInfo>(); |
| 16 | + private static List<string> requiredMethods = new List<string> { "Count", "ContainsKey" }; |
| 17 | + |
| 18 | + internal static bool VerifyMethodRequirements(Type type) |
| 19 | + { |
| 20 | + foreach (var requiredMethod in requiredMethods) |
| 21 | + { |
| 22 | + var method = type.GetMethod(requiredMethod); |
| 23 | + if (method == null) |
| 24 | + { |
| 25 | + method = type.GetMethod($"get_{requiredMethod}"); |
| 26 | + if (method == null) |
| 27 | + { |
| 28 | + return false; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + var key = Tuple.Create(type, requiredMethod); |
| 33 | + methodsByType.Add(key, method); |
| 34 | + } |
| 35 | + |
| 36 | + return true; |
| 37 | + } |
| 38 | + |
| 39 | + internal KeyValuePairEnumerableObject(Type tp) : base(tp) |
| 40 | + { |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + internal override bool CanSubclass() => false; |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Implements __len__ for dictionary types. |
| 48 | + /// </summary> |
| 49 | + public static int mp_length(IntPtr ob) |
| 50 | + { |
| 51 | + var obj = (CLRObject)GetManagedObject(ob); |
| 52 | + var self = obj.inst; |
| 53 | + |
| 54 | + var key = Tuple.Create(self.GetType(), "Count"); |
| 55 | + var methodInfo = methodsByType[key]; |
| 56 | + |
| 57 | + return (int)methodInfo.Invoke(self, null); |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Implements __contains__ for dictionary types. |
| 62 | + /// </summary> |
| 63 | + public static int sq_contains(IntPtr ob, IntPtr v) |
| 64 | + { |
| 65 | + var obj = (CLRObject)GetManagedObject(ob); |
| 66 | + var self = obj.inst; |
| 67 | + |
| 68 | + var key = Tuple.Create(self.GetType(), "ContainsKey"); |
| 69 | + var methodInfo = methodsByType[key]; |
| 70 | + |
| 71 | + var parameters = methodInfo.GetParameters(); |
| 72 | + object arg; |
| 73 | + if (!Converter.ToManaged(v, parameters[0].ParameterType, out arg, false)) |
| 74 | + { |
| 75 | + Exceptions.SetError(Exceptions.TypeError, |
| 76 | + $"invalid parameter type for sq_contains: should be {Converter.GetTypeByAlias(v)}, found {parameters[0].ParameterType}"); |
| 77 | + } |
| 78 | + |
| 79 | + return (bool)methodInfo.Invoke(self, new[] { arg }) ? 1 : 0; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public static class KeyValuePairEnumerableObjectExtension |
| 84 | + { |
| 85 | + public static bool IsKeyValuePairEnumerable(this Type type) |
| 86 | + { |
| 87 | + var iEnumerableType = typeof(IEnumerable<>); |
| 88 | + var keyValuePairType = typeof(KeyValuePair<,>); |
| 89 | + |
| 90 | + var interfaces = type.GetInterfaces(); |
| 91 | + foreach (var i in interfaces) |
| 92 | + { |
| 93 | + if (i.IsGenericType && |
| 94 | + i.GetGenericTypeDefinition() == iEnumerableType) |
| 95 | + { |
| 96 | + var arguments = i.GetGenericArguments(); |
| 97 | + if (arguments.Length != 1) continue; |
| 98 | + |
| 99 | + var a = arguments[0]; |
| 100 | + if (a.IsGenericType && |
| 101 | + a.GetGenericTypeDefinition() == keyValuePairType && |
| 102 | + a.GetGenericArguments().Length == 2) |
| 103 | + { |
| 104 | + return KeyValuePairEnumerableObject.VerifyMethodRequirements(type); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return false; |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments