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
Simplify PyObj casting, logic of if blocks.
  • Loading branch information
christabella committed Jan 12, 2021
commit 7a2b5e296475d72854f7b52ea1ba6652131c5836
4 changes: 2 additions & 2 deletions src/embed_tests/TestOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public OperableObject(int num)
using (Py.GIL())
{
// Assuming b is a tuple, take the first element.
Comment thread
lostmsu marked this conversation as resolved.
int bNum = (int)b.ToArray()[0].AsManagedObject(typeof(int));
int bNum = b[0].As<int>();
return a.Num >= bNum;
}
}
Expand All @@ -226,7 +226,7 @@ public OperableObject(int num)
using (Py.GIL())
{
// Assuming b is a tuple, take the first element.
int bNum = (int)b.ToArray()[0].AsManagedObject(typeof(int));
int bNum = b[0].As<int>();
return a.Num <= bNum;
}
}
Expand Down
19 changes: 5 additions & 14 deletions src/runtime/classbase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,10 @@ public static IntPtr tp_richcompare(IntPtr ob, IntPtr other, int op)
// otherwise fallback to checking if an IComparable interface is handled.
if (cls.richcompare.TryGetValue(op, out var methodObject))
{
IntPtr args = other;
var free = false;
if (true)
{
// Wrap the `other` argument of a binary comparison operator in a PyTuple.
args = Runtime.PyTuple_New(1);
Runtime.XIncref(other);
Runtime.PyTuple_SetItem(args, 0, other);
free = true;
}
// Wrap the `other` argument of a binary comparison operator in a PyTuple.
IntPtr args = Runtime.PyTuple_New(1);
Runtime.XIncref(other);
Runtime.PyTuple_SetItem(args, 0, other);

IntPtr value;
try
Expand All @@ -107,10 +101,7 @@ public static IntPtr tp_richcompare(IntPtr ob, IntPtr other, int op)
}
finally
{
if (free)
{
Runtime.XDecref(args); // Free args pytuple
}
Runtime.XDecref(args); // Free args pytuple
}
return value;
}
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/operatormethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ public static void FixupSlots(IntPtr pyType, Type clrType)
Debug.Assert(_opType != null);
foreach (var method in clrType.GetMethods(flags))
{
// We don't want to override slots for either non-operators or
// We only want to override slots for operators excluding
// comparison operators, which are handled by ClassBase.tp_richcompare.
if (!IsOperatorMethod(method) || IsComparisonOp(method))
if (!OpMethodMap.ContainsKey(method.Name))
{
continue;
}
Expand Down