Skip to content

Commit 6196140

Browse files
committed
Add eq and ineq operators, disallow reverse for comp
1 parent 187816e commit 6196140

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
@@ -343,16 +343,17 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
343343
int kwargsMatched;
344344
int defaultsNeeded;
345345
bool isOperator = OperatorMethod.IsOperatorMethod(mi);
346-
int clrnargs = pi.Length;
347346
// Binary operator methods will have 2 CLR args but only one Python arg
348347
// (unary operators will have 1 less each), since Python operator methods are bound.
349-
isOperator = isOperator && pynargs == clrnargs - 1;
348+
isOperator = isOperator && pynargs == pi.Length - 1;
349+
bool isReverse = isOperator && OperatorMethod.IsReverse((MethodInfo)mi); // Only cast if isOperator.
350+
if (isReverse && OperatorMethod.IsComparisonOp((MethodInfo)mi))
351+
continue; // Comparison operators in Python have no reverse mode.
350352
if (!MatchesArgumentCount(pynargs, pi, kwargDict, out paramsArray, out defaultArgList, out kwargsMatched, out defaultsNeeded) && !isOperator)
351353
{
352354
continue;
353355
}
354356
// Preprocessing pi to remove either the first or second argument.
355-
bool isReverse = isOperator && OperatorMethod.IsReverse((MethodInfo)mi); // Only cast if isOperator.
356357
if (isOperator && !isReverse) {
357358
// The first Python arg is the right operand, while the bound instance is the left.
358359
// We need to skip the first (left operand) CLR argument.

0 commit comments

Comments
 (0)