Skip to content
Merged
Show file tree
Hide file tree
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
Add operator overload and rename pynargs, clrnargs
  • Loading branch information
christabella committed Dec 23, 2020
commit c2be3f130fd8620ce10e1457d2193c6bd706c48b
29 changes: 29 additions & 0 deletions src/embed_tests/TestOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public OperableObject(int num)
Num = num;
}

public static OperableObject operator +(int a, OperableObject b)
{
return new OperableObject(a + b.Num);
}
public static OperableObject operator +(OperableObject a, OperableObject b)
{
return new OperableObject(a.Num + b.Num);
Expand All @@ -46,6 +50,10 @@ public OperableObject(int num)
return new OperableObject(a.Num + b);
}

public static OperableObject operator -(int a, OperableObject b)
{
return new OperableObject(a - b.Num);
}
public static OperableObject operator -(OperableObject a, OperableObject b)
{
return new OperableObject(a.Num - b.Num);
Expand All @@ -55,6 +63,11 @@ public OperableObject(int num)
return new OperableObject(a.Num - b);
}


public static OperableObject operator *(int a, OperableObject b)
{
return new OperableObject(a * b.Num);
}
public static OperableObject operator *(OperableObject a, OperableObject b)
{
return new OperableObject(a.Num * b.Num);
Expand All @@ -64,6 +77,10 @@ public OperableObject(int num)
return new OperableObject(a.Num * b);
}

public static OperableObject operator /(int a, OperableObject b)
{
return new OperableObject(a / b.Num);
}
public static OperableObject operator /(OperableObject a, OperableObject b)
{
return new OperableObject(a.Num / b.Num);
Expand All @@ -74,6 +91,10 @@ public OperableObject(int num)
}


public static OperableObject operator &(int a, OperableObject b)
{
return new OperableObject(a & b.Num);
}
public static OperableObject operator &(OperableObject a, OperableObject b)
{
return new OperableObject(a.Num & b.Num);
Expand All @@ -84,6 +105,10 @@ public OperableObject(int num)
}


public static OperableObject operator |(int a, OperableObject b)
{
return new OperableObject(a | b.Num);
}
public static OperableObject operator |(OperableObject a, OperableObject b)
{
return new OperableObject(a.Num | b.Num);
Expand All @@ -94,6 +119,10 @@ public OperableObject(int num)
}


public static OperableObject operator ^(int a, OperableObject b)
{
return new OperableObject(a ^ b.Num);
}
public static OperableObject operator ^(OperableObject a, OperableObject b)
{
return new OperableObject(a.Num ^ b.Num);
Expand Down
28 changes: 14 additions & 14 deletions src/runtime/methodbinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
Runtime.XDecref(valueList);
}

var pynargs = (int)Runtime.PyTuple_Size(args);
var numPyArgs = (int)Runtime.PyTuple_Size(args);
var isGeneric = false;
if (info != null)
{
Expand All @@ -343,14 +343,14 @@ 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, isOperator, out paramsArray, out defaultArgList, out kwargsMatched, out defaultsNeeded))
if (!MatchesArgumentCount(numPyArgs, pi, kwargDict, isOperator, out paramsArray, out defaultArgList, out kwargsMatched, out defaultsNeeded))
{
continue;
}
var outs = 0;
int clrnargs = pi.Length;
isOperator = isOperator && pynargs == clrnargs - 1; // Handle mismatched arg numbers due to Python operator being bound.
var margs = TryConvertArguments(pi, paramsArray, args, pynargs, kwargDict, defaultArgList,
int numClrArgs = pi.Length;
isOperator = isOperator && numPyArgs == numClrArgs - 1; // Handle mismatched arg numbers due to Python operator being bound.
var margs = TryConvertArguments(pi, paramsArray, args, numPyArgs, kwargDict, defaultArgList,
needsResolution: _methods.Length > 1, // If there's more than one possible match.
isOperator: isOperator,
outs: out outs);
Expand Down Expand Up @@ -669,7 +669,7 @@ 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="pynargs">Number of positional args passed from Python.</param>
/// <param name="numPyArgs">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>
Expand All @@ -678,7 +678,7 @@ static Type TryComputeClrArgumentType(Type parameterType, IntPtr argument, bool
/// <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 pynargs, ParameterInfo[] parameters,
static bool MatchesArgumentCount(int numPyArgs, ParameterInfo[] parameters,
Dictionary<string, IntPtr> kwargDict,
bool isOperator,
out bool paramsArray,
Expand All @@ -688,15 +688,15 @@ static bool MatchesArgumentCount(int pynargs, ParameterInfo[] parameters,
{
defaultArgList = null;
var match = false;
int clrnargs = parameters.Length;
paramsArray = clrnargs > 0 ? Attribute.IsDefined(parameters[clrnargs - 1], typeof(ParamArrayAttribute)) : false;
int numClrArgs = parameters.Length;
paramsArray = numClrArgs > 0 ? Attribute.IsDefined(parameters[numClrArgs - 1], typeof(ParamArrayAttribute)) : false;
kwargsMatched = 0;
defaultsNeeded = 0;
if (pynargs == clrnargs && kwargDict.Count == 0)
if (numPyArgs == numClrArgs && kwargDict.Count == 0)
{
match = true;
}
else if (pynargs < clrnargs && (!paramsArray || pynargs == clrnargs - 1))
else if (numPyArgs < numClrArgs && (!paramsArray || numPyArgs == numClrArgs - 1))
{
match = true;
// operator methods will have 2 CLR args but only one Python arg,
Expand All @@ -711,7 +711,7 @@ static bool MatchesArgumentCount(int pynargs, ParameterInfo[] parameters,
// a corresponding keyword arg or a default param, unless the method
// method accepts a params array (which cannot have a default value)
defaultArgList = new ArrayList();
for (var v = pynargs; v < clrnargs; v++)
for (var v = numPyArgs; v < numClrArgs; v++)
{
if (kwargDict.ContainsKey(parameters[v].Name))
{
Expand All @@ -736,8 +736,8 @@ static bool MatchesArgumentCount(int pynargs, ParameterInfo[] parameters,
}
}
}
else if (pynargs > clrnargs && clrnargs > 0 &&
Attribute.IsDefined(parameters[clrnargs - 1], typeof(ParamArrayAttribute)))
else if (numPyArgs > numClrArgs && numClrArgs > 0 &&
Attribute.IsDefined(parameters[numClrArgs - 1], typeof(ParamArrayAttribute)))
{
// This is a `foo(params object[] bar)` style method
match = true;
Expand Down