Skip to content

Commit a3c8be7

Browse files
committed
Try comparison with python object if conversion to managed is not possible
1 parent a685c20 commit a3c8be7

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/runtime/Types/ClassBase.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,27 @@ public static NewReference tp_richcompare(BorrowedReference ob, BorrowedReferenc
149149
case Runtime.Py_GE:
150150
co1 = (CLRObject)GetManagedObject(ob)!;
151151
co2 = GetManagedObject(other) as CLRObject;
152-
if (co1 == null || co2 == null)
152+
153+
object co2Inst = null;
154+
// The object comparing against is not a managed object. It could still be a Python object
155+
// that can be compared against (e.g. comparing against a Python string)
156+
if (co2 == null)
157+
{
158+
if (other != null)
159+
{
160+
using var pyCo2 = new PyObject(other);
161+
if (Converter.ToManagedValue(pyCo2, typeof(object), out var result, false))
162+
{
163+
co2Inst = result;
164+
}
165+
}
166+
}
167+
else
168+
{
169+
co2Inst = co2.inst;
170+
}
171+
172+
if (co1 == null || co2Inst == null)
153173
{
154174
return Exceptions.RaiseTypeError("Cannot get managed object");
155175
}
@@ -161,7 +181,7 @@ public static NewReference tp_richcompare(BorrowedReference ob, BorrowedReferenc
161181
}
162182
try
163183
{
164-
int cmp = co1Comp.CompareTo(co2.inst);
184+
int cmp = co1Comp.CompareTo(co2Inst);
165185

166186
BorrowedReference pyCmp;
167187
if (cmp < 0)

0 commit comments

Comments
 (0)