Skip to content

Commit 7a2b5e2

Browse files
committed
Simplify PyObj casting, logic of if blocks.
1 parent 6d4dec7 commit 7a2b5e2

File tree

3 files changed

+9
-18
lines changed

3 files changed

+9
-18
lines changed

src/embed_tests/TestOperator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public OperableObject(int num)
217217
using (Py.GIL())
218218
{
219219
// Assuming b is a tuple, take the first element.
220-
int bNum = (int)b.ToArray()[0].AsManagedObject(typeof(int));
220+
int bNum = b[0].As<int>();
221221
return a.Num >= bNum;
222222
}
223223
}
@@ -226,7 +226,7 @@ public OperableObject(int num)
226226
using (Py.GIL())
227227
{
228228
// Assuming b is a tuple, take the first element.
229-
int bNum = (int)b.ToArray()[0].AsManagedObject(typeof(int));
229+
int bNum = b[0].As<int>();
230230
return a.Num <= bNum;
231231
}
232232
}

src/runtime/classbase.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,10 @@ public static IntPtr tp_richcompare(IntPtr ob, IntPtr other, int op)
8989
// otherwise fallback to checking if an IComparable interface is handled.
9090
if (cls.richcompare.TryGetValue(op, out var methodObject))
9191
{
92-
IntPtr args = other;
93-
var free = false;
94-
if (true)
95-
{
96-
// Wrap the `other` argument of a binary comparison operator in a PyTuple.
97-
args = Runtime.PyTuple_New(1);
98-
Runtime.XIncref(other);
99-
Runtime.PyTuple_SetItem(args, 0, other);
100-
free = true;
101-
}
92+
// Wrap the `other` argument of a binary comparison operator in a PyTuple.
93+
IntPtr args = Runtime.PyTuple_New(1);
94+
Runtime.XIncref(other);
95+
Runtime.PyTuple_SetItem(args, 0, other);
10296

10397
IntPtr value;
10498
try
@@ -107,10 +101,7 @@ public static IntPtr tp_richcompare(IntPtr ob, IntPtr other, int op)
107101
}
108102
finally
109103
{
110-
if (free)
111-
{
112-
Runtime.XDecref(args); // Free args pytuple
113-
}
104+
Runtime.XDecref(args); // Free args pytuple
114105
}
115106
return value;
116107
}

src/runtime/operatormethod.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ public static void FixupSlots(IntPtr pyType, Type clrType)
104104
Debug.Assert(_opType != null);
105105
foreach (var method in clrType.GetMethods(flags))
106106
{
107-
// We don't want to override slots for either non-operators or
107+
// We only want to override slots for operators excluding
108108
// comparison operators, which are handled by ClassBase.tp_richcompare.
109-
if (!IsOperatorMethod(method) || IsComparisonOp(method))
109+
if (!OpMethodMap.ContainsKey(method.Name))
110110
{
111111
continue;
112112
}

0 commit comments

Comments
 (0)