Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ad2b377

Browse files
committed
Use reverse instead of forward (semantics) and add comparison operators
1 parent e7da0bc commit ad2b377

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

src/runtime/methodbinder.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -351,13 +351,13 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
351351
int clrnargs = pi.Length;
352352
isOperator = isOperator && pynargs == clrnargs - 1; // Handle mismatched arg numbers due to Python operator being bound.
353353
// Preprocessing pi to remove either the first or second argument.
354-
bool isForward = isOperator && OperatorMethod.IsForward((MethodInfo)mi); // Only cast if isOperator.
355-
if (isOperator && isForward) {
354+
bool isReverse = isOperator && OperatorMethod.IsReverse((MethodInfo)mi); // Only cast if isOperator.
355+
if (isOperator && !isReverse) {
356356
// The first Python arg is the right operand, while the bound instance is the left.
357357
// We need to skip the first (left operand) CLR argument.
358358
pi = pi.Skip(1).Take(1).ToArray();
359359
}
360-
else if (isOperator && !isForward) {
360+
else if (isOperator && isReverse) {
361361
// The first Python arg is the left operand.
362362
// We need to take the first CLR argument.
363363
pi = pi.Take(1).ToArray();
@@ -377,10 +377,10 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
377377
{
378378
// Postprocessing to extend margs.
379379
var margsTemp = new object[2];
380-
// If forward, the bound instance is the left operand.
381-
int boundOperandIndex = isForward ? 0 : 1;
382-
// If forward, the passed instance is the right operand.
383-
int passedOperandIndex = isForward ? 1 : 0;
380+
// If reverse, the passed instance is the left operand.
381+
int passedOperandIndex= isReverse ? 0 : 1;
382+
// If reverse, the bound instance is the right operand.
383+
int boundOperandIndex = isReverse ? 1 : 0;
384384
margsTemp[boundOperandIndex] = co.inst;
385385
margsTemp[passedOperandIndex] = margs[0];
386386
margs = margsTemp;

src/runtime/operatormethod.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ static OperatorMethod()
4646
["op_LeftShift"] = new SlotDefinition("__lshift__", TypeOffset.nb_lshift),
4747
["op_RightShift"] = new SlotDefinition("__rshift__", TypeOffset.nb_rshift),
4848
["op_Modulus"] = new SlotDefinition("__mod__", TypeOffset.nb_remainder),
49-
["op_OneComplement"] = new SlotDefinition("__invert__", TypeOffset.nb_invert)
49+
["op_OneComplement"] = new SlotDefinition("__invert__", TypeOffset.nb_invert),
50+
["op_GreaterThan"] = new SlotDefinition("__gt__", TypeOffset.tp_richcompare),
51+
["op_GreaterThanOrEqual"] = new SlotDefinition("__ge__", TypeOffset.tp_richcompare),
52+
["op_LessThan"] = new SlotDefinition("__lt__", TypeOffset.tp_richcompare),
53+
["op_LessThanOrEqual"] = new SlotDefinition("__le__", TypeOffset.tp_richcompare)
5054
};
5155
}
5256

@@ -137,15 +141,15 @@ public static string ReversePyMethodName(string pyName)
137141
}
138142

139143
/// <summary>
140-
/// Check if the method is performing a forward or reverse operation.
144+
/// Check if the method is performing a reverse operation.
141145
/// </summary>
142146
/// <param name="method">The operator method.</param>
143147
/// <returns></returns>
144-
public static bool IsForward(MethodInfo method)
148+
public static bool IsReverse(MethodInfo method)
145149
{
146150
Type declaringType = method.DeclaringType;
147151
Type leftOperandType = method.GetParameters()[0].ParameterType;
148-
return leftOperandType == declaringType;
152+
return leftOperandType != declaringType;
149153
}
150154

151155
public static void FilterMethods(MethodInfo[] methods, out MethodInfo[] forwardMethods, out MethodInfo[] reverseMethods)
@@ -154,12 +158,12 @@ public static void FilterMethods(MethodInfo[] methods, out MethodInfo[] forwardM
154158
List<MethodInfo> reverseMethodsList = new List<MethodInfo>();
155159
foreach (var method in methods)
156160
{
157-
if (IsForward(method))
161+
if (IsReverse(method))
158162
{
159-
forwardMethodsList.Add(method);
163+
reverseMethodsList.Add(method);
160164
} else
161165
{
162-
reverseMethodsList.Add(method);
166+
forwardMethodsList.Add(method);
163167
}
164168

165169
}

0 commit comments

Comments
 (0)