Skip to content

Commit 3c4ea26

Browse files
committed
Add eq and ineq operators, disallow reverse for comp
1 parent 26a6dcb commit 3c4ea26

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/embed_tests/TestOperator.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,32 @@ public OperableObject(int num)
149149
return new OperableObject(a.Num ^ b);
150150
}
151151

152+
public static bool operator ==(int a, OperableObject b)
153+
{
154+
return (a == b.Num);
155+
}
156+
public static bool operator ==(OperableObject a, OperableObject b)
157+
{
158+
return (a.Num == b.Num);
159+
}
160+
public static bool operator ==(OperableObject a, int b)
161+
{
162+
return (a.Num == b);
163+
}
164+
165+
public static bool operator !=(int a, OperableObject b)
166+
{
167+
return (a != b.Num);
168+
}
169+
public static bool operator !=(OperableObject a, OperableObject b)
170+
{
171+
return (a.Num != b.Num);
172+
}
173+
public static bool operator !=(OperableObject a, int b)
174+
{
175+
return (a.Num != b);
176+
}
177+
152178
public static bool operator <=(int a, OperableObject b)
153179
{
154180
return (a <= b.Num);

src/runtime/methodbinder.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,16 +354,17 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
354354
int kwargsMatched;
355355
int defaultsNeeded;
356356
bool isOperator = OperatorMethod.IsOperatorMethod(mi);
357-
int clrnargs = pi.Length;
358357
// Binary operator methods will have 2 CLR args but only one Python arg
359358
// (unary operators will have 1 less each), since Python operator methods are bound.
360-
isOperator = isOperator && pynargs == clrnargs - 1;
359+
isOperator = isOperator && pynargs == pi.Length - 1;
360+
bool isReverse = isOperator && OperatorMethod.IsReverse((MethodInfo)mi); // Only cast if isOperator.
361+
if (isReverse && OperatorMethod.IsComparisonOp((MethodInfo)mi))
362+
continue; // Comparison operators in Python have no reverse mode.
361363
if (!MatchesArgumentCount(pynargs, pi, kwargDict, out paramsArray, out defaultArgList, out kwargsMatched, out defaultsNeeded) && !isOperator)
362364
{
363365
continue;
364366
}
365367
// Preprocessing pi to remove either the first or second argument.
366-
bool isReverse = isOperator && OperatorMethod.IsReverse((MethodInfo)mi); // Only cast if isOperator.
367368
if (isOperator && !isReverse) {
368369
// The first Python arg is the right operand, while the bound instance is the left.
369370
// We need to skip the first (left operand) CLR argument.

0 commit comments

Comments
 (0)