Skip to content

Commit

Permalink
Deduplicate in PythonExtensionBinder instead
Browse files Browse the repository at this point in the history
  • Loading branch information
slozier committed Feb 24, 2023
1 parent e9bb7e5 commit ee64134
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
7 changes: 4 additions & 3 deletions Src/IronPython/Runtime/Binding/PythonExtensionBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public PythonExtensionBinder(PythonBinder binder, ExtensionMethodSet extensionMe
public override MemberGroup GetMember(MemberRequestKind actionKind, Type type, string name) {
var res = base.GetMember(actionKind, type, name);
if (res.Count == 0) {
List<MemberTracker> trackers = new List<MemberTracker>();
// GetExtensionMethods may return duplicate MethodInfo so use a HashSet - https://github.com/IronLanguages/ironpython2/issues/810
HashSet<MemberTracker> trackers = null;

foreach (var method in _extMethodSet.GetExtensionMethods(name)) {
var parameters = method.GetParameters();
Expand All @@ -34,11 +35,11 @@ public override MemberGroup GetMember(MemberRequestKind actionKind, Type type, s
var paramType = parameters[0].ParameterType;

if (IsApplicableExtensionMethod(type, paramType)) {
trackers.Add(MemberTracker.FromMemberInfo(method, paramType));
(trackers ??= new HashSet<MemberTracker>()).Add(MemberTracker.FromMemberInfo(method, paramType));
}
}

if (trackers.Count > 0) {
if (trackers is not null) {
return new MemberGroup(trackers.ToArray());
}
}
Expand Down
9 changes: 3 additions & 6 deletions Src/IronPython/Runtime/ExtensionMethodSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,15 @@ public IEnumerable<MethodInfo> GetExtensionMethods(string/*!*/ name) {
lock (this) {
EnsureLoaded();

var yieldedMethods = new HashSet<MethodInfo>();

foreach (var keyValue in _loadedAssemblies) {
AssemblyLoadInfo info = keyValue.Value;

Debug.Assert(info.Types != null);
foreach (var type in info.Types) {
if (type.ExtensionMethods.TryGetValue(name, out var methods)) {
List<MethodInfo> methods;
if (type.ExtensionMethods.TryGetValue(name, out methods)) {
foreach (var method in methods) {
if (yieldedMethods.Add(method)) {
yield return method;
}
yield return method;
}
}
}
Expand Down

0 comments on commit ee64134

Please sign in to comment.