Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
MethodBinderEvents.CoerceBind
  • Loading branch information
generalloki authored and filmor committed Dec 8, 2025
commit cf01b188810befe0355501433cbe4c196d528375
28 changes: 26 additions & 2 deletions src/runtime/MethodBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
namespace Python.Runtime
{
using MaybeMethodBase = MaybeMethodBase<MethodBase>;

public delegate void MethodBinderCoerceBindDelegate(
Dictionary<string, PyObject> arguments,
MethodBase[] methods,
ref MethodBase? foundBinding);

/// <summary>
/// A MethodBinder encapsulates information about a (possibly overloaded)
/// managed method, and is responsible for selecting the right method given
Expand Down Expand Up @@ -516,7 +522,7 @@ public MismatchedMethod(Exception exception, MethodBase mb)
}
}

return new Binding(mi, target, margs, outs);
return CoerceResult(new Binding(mi, target, margs, outs));
}
else if (matchGenerics && isGeneric)
{
Expand All @@ -528,7 +534,7 @@ public MismatchedMethod(Exception exception, MethodBase mb)
MethodInfo[] overloads = MatchParameters(methods, types);
if (overloads.Length != 0)
{
return Bind(inst, args, kwargDict, overloads, matchGenerics: false);
return CoerceResult(Bind(inst, args, kwargDict, overloads, matchGenerics: false));
}
}
if (mismatchedMethods.Count > 0)
Expand All @@ -537,6 +543,19 @@ public MismatchedMethod(Exception exception, MethodBase mb)
Exceptions.SetError(aggregateException);
}
return null;

Binding? CoerceResult(Binding? binding)
{
if (binding is not null)
{
var foundMethod = binding.info;
MethodBinderEvents.CoerceBind?.Invoke(kwargDict, methods, ref foundMethod);
if (foundMethod is null)
return null;
}

return binding;
}
}

static AggregateException GetAggregateException(IEnumerable<MismatchedMethod> mismatchedMethods)
Expand Down Expand Up @@ -1068,4 +1087,9 @@ static internal class ParameterInfoExtensions
}
}
}

public static class MethodBinderEvents
{
public static MethodBinderCoerceBindDelegate? CoerceBind;
}
}