-
Notifications
You must be signed in to change notification settings - Fork 772
Fix kwarg func resolution #1136
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
lostmsu
merged 9 commits into
pythonnet:master
from
jmlidbetter:fix_kwarg_func_resolution
Nov 18, 2020
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9fb753a
Fix function resolution when calling overloads with keyword arguments
jmlidbetter fee4373
Updates CHANGELOG.md
jmlidbetter 8581715
Switches Tuple for struct
jmlidbetter 86d21f1
Use LINQ Min and Max methods; don't create second list for methods be…
jmlidbetter d453159
Removes all secondary lists
jmlidbetter a1b6c76
Updates CHANGELOG
jmlidbetter 1d01c45
Merge branch 'master' into fix_kwarg_func_resolution
filmor 980c231
Merge branch 'master' into fix_kwarg_func_resolution
jmlidbetter 8d0fc2c
Merge branch 'master' into fix_kwarg_func_resolution
filmor 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
Switches Tuple for struct
- Loading branch information
commit 8581715173766f3f1d9a5396665c430d45f8eef9
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -278,6 +278,23 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info) | |
| return Bind(inst, args, kw, info, null); | ||
| } | ||
|
|
||
| private readonly struct MatchedMethod { | ||
| public MatchedMethod(int kwargsMatched, int defaultsNeeded, object[] margs, int outs, MethodBase mb) | ||
| { | ||
| KwargsMatched = kwargsMatched; | ||
| DefaultsNeeded = defaultsNeeded; | ||
| ManagedArgs = margs; | ||
| Outs = outs; | ||
| Method = mb; | ||
| } | ||
|
|
||
| public int KwargsMatched { get; } | ||
| public int DefaultsNeeded { get; } | ||
| public object[] ManagedArgs { get; } | ||
| public int Outs { get; } | ||
| public MethodBase Method { get; } | ||
| } | ||
|
|
||
| internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, MethodInfo[] methodinfo) | ||
| { | ||
| // loop to find match, return invoker w/ or /wo error | ||
|
|
@@ -310,10 +327,7 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth | |
| _methods = GetMethods(); | ||
| } | ||
|
|
||
| // List of tuples containing methods that have matched based on arguments. | ||
| // Format of tuple is: Number of kwargs matched, defaults needed, margs, outs, method base for matched method | ||
| List<Tuple<int, int, object[], int, MethodBase>> argMatchedMethods = | ||
| new List<Tuple<int, int, object[], int, MethodBase>>(_methods.Length); | ||
| List<MatchedMethod> argMatchedMethods = new List<MatchedMethod>(_methods.Length); | ||
|
|
||
| // TODO: Clean up | ||
| foreach (MethodBase mi in _methods) | ||
|
|
@@ -342,35 +356,33 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth | |
| continue; | ||
| } | ||
|
|
||
| var matchedMethodTuple = Tuple.Create(kwargsMatched, defaultsNeeded, margs, outs, mi); | ||
| argMatchedMethods.Add(matchedMethodTuple); | ||
| var matchedMethod = new MatchedMethod(kwargsMatched, defaultsNeeded, margs, outs, mi); | ||
| argMatchedMethods.Add(matchedMethod); | ||
| } | ||
| if (argMatchedMethods.Count() > 0) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||
| { | ||
| // Order matched methods by number of kwargs matched and get the max possible number | ||
| // of kwargs matched | ||
| var bestKwargMatchCount = argMatchedMethods.OrderBy((x) => x.Item1).Reverse().ToArray()[0].Item1; | ||
| var bestKwargMatchCount = argMatchedMethods.OrderBy((x) => x.KwargsMatched).Reverse().ToArray()[0].KwargsMatched; | ||
|
lostmsu marked this conversation as resolved.
Outdated
|
||
|
|
||
| List<Tuple<int, int, object[], int, MethodBase>> bestKwargMatches = | ||
| new List<Tuple<int, int, object[], int, MethodBase>>(argMatchedMethods.Count()); | ||
| foreach (Tuple<int, int, object[], int, MethodBase> argMatchedTuple in argMatchedMethods) | ||
| List<MatchedMethod> bestKwargMatches = new List<MatchedMethod>(argMatchedMethods.Count()); | ||
| foreach (MatchedMethod testMatch in argMatchedMethods) | ||
| { | ||
| if (argMatchedTuple.Item1 == bestKwargMatchCount) | ||
| if (testMatch.KwargsMatched == bestKwargMatchCount) | ||
| { | ||
| bestKwargMatches.Add(argMatchedTuple); | ||
| bestKwargMatches.Add(testMatch); | ||
| } | ||
| } | ||
|
|
||
| // Order by the number of defaults required and find the smallest | ||
| var fewestDefaultsRequired = bestKwargMatches.OrderBy((x) => x.Item2).ToArray()[0].Item2; | ||
| var fewestDefaultsRequired = bestKwargMatches.OrderBy((x) => x.DefaultsNeeded).ToArray()[0].DefaultsNeeded; | ||
|
lostmsu marked this conversation as resolved.
Outdated
|
||
|
|
||
| List<Tuple<int, int, object[], int, MethodBase>> bestDefaultsMatches = | ||
| new List<Tuple<int, int, object[], int, MethodBase>>(bestKwargMatches.Count()); | ||
| foreach (Tuple<int, int, object[], int, MethodBase> testTuple in bestKwargMatches) | ||
| List<MatchedMethod> bestDefaultsMatches = new List<MatchedMethod>(bestKwargMatches.Count()); | ||
| foreach (MatchedMethod testMatch in bestKwargMatches) | ||
| { | ||
| if (testTuple.Item2 == fewestDefaultsRequired) | ||
| if (testMatch.DefaultsNeeded == fewestDefaultsRequired) | ||
| { | ||
| bestDefaultsMatches.Add(testTuple); | ||
| bestDefaultsMatches.Add(testMatch); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -388,10 +400,10 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth | |
| // in the case of (a) we're done by default. For (b) regardless of which | ||
| // method we choose, all arguments are specified _and_ can be converted | ||
| // from python to C# so picking any will suffice | ||
| Tuple<int, int, object[], int, MethodBase> bestMatch = bestDefaultsMatches.ToArray()[0]; | ||
| var margs = bestMatch.Item3; | ||
| var outs = bestMatch.Item4; | ||
| var mi = bestMatch.Item5; | ||
| MatchedMethod bestMatch = bestDefaultsMatches.ToArray()[0]; | ||
|
lostmsu marked this conversation as resolved.
Outdated
|
||
| var margs = bestMatch.ManagedArgs; | ||
| var outs = bestMatch.Outs; | ||
| var mi = bestMatch.Method; | ||
|
|
||
| object target = null; | ||
| if (!mi.IsStatic && inst != IntPtr.Zero) | ||
|
|
||
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.