Skip to content
Merged
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
Add eq and ineq operators, disallow reverse for comp
  • Loading branch information
christabella committed Jan 12, 2021
commit ae7e774c4564de86b69c9f46ac6767db199eae80
26 changes: 26 additions & 0 deletions src/embed_tests/TestOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,32 @@ public OperableObject(int num)
return new OperableObject(a.Num ^ b);
}

public static bool operator ==(int a, OperableObject b)
{
return (a == b.Num);
}
public static bool operator ==(OperableObject a, OperableObject b)
{
return (a.Num == b.Num);
}
public static bool operator ==(OperableObject a, int b)
{
return (a.Num == b);
}

public static bool operator !=(int a, OperableObject b)
{
return (a != b.Num);
}
public static bool operator !=(OperableObject a, OperableObject b)
{
return (a.Num != b.Num);
}
public static bool operator !=(OperableObject a, int b)
{
return (a.Num != b);
}

public static bool operator <=(int a, OperableObject b)
{
return (a <= b.Num);
Expand Down
7 changes: 4 additions & 3 deletions src/runtime/methodbinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,16 +354,17 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
int kwargsMatched;
int defaultsNeeded;
bool isOperator = OperatorMethod.IsOperatorMethod(mi);
int clrnargs = pi.Length;
// Binary operator methods will have 2 CLR args but only one Python arg
// (unary operators will have 1 less each), since Python operator methods are bound.
isOperator = isOperator && pynargs == clrnargs - 1;
isOperator = isOperator && pynargs == pi.Length - 1;
bool isReverse = isOperator && OperatorMethod.IsReverse((MethodInfo)mi); // Only cast if isOperator.
if (isReverse && OperatorMethod.IsComparisonOp((MethodInfo)mi))
continue; // Comparison operators in Python have no reverse mode.
if (!MatchesArgumentCount(pynargs, pi, kwargDict, out paramsArray, out defaultArgList, out kwargsMatched, out defaultsNeeded) && !isOperator)
{
continue;
}
// Preprocessing pi to remove either the first or second argument.
bool isReverse = isOperator && OperatorMethod.IsReverse((MethodInfo)mi); // Only cast if isOperator.
if (isOperator && !isReverse) {
// The first Python arg is the right operand, while the bound instance is the left.
// We need to skip the first (left operand) CLR argument.
Expand Down