Skip to content

Operator overloads support #1324

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 25 commits into from
Jan 5, 2021
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5130aaa
Overload operators except for bit shifts and default args.
christabella Dec 16, 2020
0d0ca87
Remove params array tests since they are unrelated to operator overlo…
christabella Dec 17, 2020
253f9c7
Fix type bug; rename variables pynargs etc.
christabella Dec 17, 2020
8cdb61c
Remove unused variables and add comments.
christabella Dec 17, 2020
c26e589
Add comments and remove unused in operatormethod.cs
christabella Dec 18, 2020
3222a54
Address review by @lostmsu; Add forward operator test.
christabella Dec 20, 2020
550ff31
Add forward operator tests (OpObj, int).
christabella Dec 21, 2020
eab8edc
Address @amos402's comment on raising branch.
christabella Dec 21, 2020
c2be3f1
Add operator overload and rename pynargs, clrnargs
christabella Dec 21, 2020
581a047
Revert variable renames
christabella Dec 22, 2020
e11327f
Update AUTHORS and CHANGELOG
christabella Dec 22, 2020
35be4bc
Revert rename to pynargs
christabella Dec 23, 2020
f19c281
Address @tminka's comments.
christabella Dec 23, 2020
d7f52d2
Remove whitespace
christabella Dec 23, 2020
5855a1b
Fix nits
christabella Dec 24, 2020
e7da0bc
Support reverse binary operations
christabella Dec 28, 2020
a376838
Use reverse instead of forward (semantics)
christabella Dec 29, 2020
6923a78
Address @tminka's comments
christabella Dec 30, 2020
4c992d8
Support unary neg and pos operators
christabella Dec 30, 2020
8cce61d
Remove isOperator from MatchesArgumentCount (simplify), add test.
christabella Jan 4, 2021
09a2047
Revert "Remove isOperator from MatchesArgumentCount (simplify)"
christabella Jan 4, 2021
41bd07f
Properly remove isOperator from MatchesArgumentCount (@tminka comment)
christabella Jan 4, 2021
10ccf1e
Update changelog.
christabella Jan 4, 2021
5682e0c
Add ones complement and modulo tests (@tminka comment)
christabella Jan 4, 2021
5f45c70
Merge branch 'master' into feat/operator-overloads
lostmsu Jan 5, 2021
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
Prev Previous commit
Next Next commit
Fix type bug; rename variables pynargs etc.
  • Loading branch information
christabella committed Dec 23, 2020
commit 253f9c79909b38a2cf9dddbb857acb7729113e8c
39 changes: 22 additions & 17 deletions src/runtime/methodbinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,9 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
int kwargsMatched;
int defaultsNeeded;
bool isOperator = OperatorMethod.IsOperatorMethod(mi); // e.g. op_Addition is defined for OperableObject
if (!MatchesArgumentCount(pynargs, pi, kwargDict, out paramsArray, out defaultArgList, out kwargsMatched, out defaultsNeeded))
if (!MatchesArgumentCount(pynargs, pi, kwargDict, isOperator, out paramsArray, out defaultArgList, out kwargsMatched, out defaultsNeeded))
{
if (isOperator)
{
defaultArgList = null;
}
else { continue; }
continue;
}
var outs = 0;
int clrnargs = pi.Length;
Expand Down Expand Up @@ -545,6 +541,7 @@ static object[] TryConvertArguments(ParameterInfo[] pi, bool paramsArray,
// After we've obtained the first argument from Python, we need to skip the first argument of the CLR
// because operator method is a bound method in Python
paramIndex++; // Leave the first .NET param as null (margs).
parameter = pi[paramIndex];
}

bool isOut;
Expand Down Expand Up @@ -672,39 +669,42 @@ static Type TryComputeClrArgumentType(Type parameterType, IntPtr argument, bool
/// <summary>
/// Check whether the number of Python and .NET arguments match, and compute additional arg information.
/// </summary>
/// <param name="positionalArgumentCount">Number of positional args passed from Python.</param>
/// <param name="pynargs">Number of positional args passed from Python.</param>
/// <param name="parameters">Parameters of the specified .NET method.</param>
/// <param name="kwargDict">Keyword args passed from Python.</param>
/// <param name="isOperator">True if the parameters' method is an operator.</param>
/// <param name="paramsArray">True if the final param of the .NET method is an array (`params` keyword).</param>
/// <param name="defaultArgList">List of default values for arguments.</param>
/// <param name="kwargsMatched">Number of kwargs from Python that are also present in the .NET method.</param>
/// <param name="defaultsNeeded">Number of non-null defaultsArgs.</param>
/// <returns></returns>
static bool MatchesArgumentCount(int positionalArgumentCount, ParameterInfo[] parameters,
static bool MatchesArgumentCount(int pynargs, ParameterInfo[] parameters,
Dictionary<string, IntPtr> kwargDict,
bool isOperator,
out bool paramsArray,
out ArrayList defaultArgList,
out int kwargsMatched,
out int defaultsNeeded)
{
defaultArgList = null;
var match = false;
paramsArray = parameters.Length > 0 ? Attribute.IsDefined(parameters[parameters.Length - 1], typeof(ParamArrayAttribute)) : false;
int clrnargs = parameters.Length;
paramsArray = clrnargs > 0 ? Attribute.IsDefined(parameters[clrnargs - 1], typeof(ParamArrayAttribute)) : false;
var kwargCount = kwargDict.Count;
kwargsMatched = 0;
defaultsNeeded = 0;

if (positionalArgumentCount == parameters.Length && kwargDict.Count == 0)
if (pynargs == clrnargs && kwargDict.Count == 0)
{
match = true;
}
else if (positionalArgumentCount < parameters.Length && (!paramsArray || positionalArgumentCount == parameters.Length - 1))
else if (pynargs < clrnargs && (!paramsArray || pynargs == clrnargs - 1))
{
// every parameter past 'positionalArgumentCount' must have either
// a corresponding keyword argument or a default parameter
// a corresponding keyword argument or a default parameter, unless
// the method is an operator or accepts a params array.
match = true;
defaultArgList = new ArrayList();
for (var v = positionalArgumentCount; v < parameters.Length; v++)
for (var v = pynargs; v < clrnargs; v++)
{
if (kwargDict.ContainsKey(parameters[v].Name))
{
Expand All @@ -723,14 +723,19 @@ static bool MatchesArgumentCount(int positionalArgumentCount, ParameterInfo[] pa
defaultArgList.Add(parameters[v].GetDefaultValue());
defaultsNeeded++;
}
else if (!paramsArray)
else if (!isOperator && !paramsArray)
{
match = false;
}
}
if (isOperator && defaultArgList.Count == 0)
{
// If no default arguments were provided for an operable object.
defaultArgList = null;
}
}
else if (positionalArgumentCount > parameters.Length && parameters.Length > 0 &&
Attribute.IsDefined(parameters[parameters.Length - 1], typeof(ParamArrayAttribute)))
else if (pynargs > clrnargs && clrnargs > 0 &&
Attribute.IsDefined(parameters[clrnargs - 1], typeof(ParamArrayAttribute)))
{
// This is a `foo(params object[] bar)` style method
match = true;
Expand Down