@@ -128,14 +128,13 @@ public static NewReference tp_richcompare(BorrowedReference ob, BorrowedReferenc
128128 }
129129
130130 co1 = ( CLRObject ) GetManagedObject ( ob ) ! ;
131- co2 = GetManagedObject ( other ) as CLRObject ;
132- if ( null == co2 )
131+ var o2 = GetSecondCompareOperandInstance ( other ) ;
132+ if ( null == o2 )
133133 {
134134 return new NewReference ( pyfalse ) ;
135135 }
136136
137137 object o1 = co1 . inst ;
138- object o2 = co2 . inst ;
139138
140139 if ( Equals ( o1 , o2 ) )
141140 {
@@ -148,26 +147,7 @@ public static NewReference tp_richcompare(BorrowedReference ob, BorrowedReferenc
148147 case Runtime . Py_GT :
149148 case Runtime . Py_GE :
150149 co1 = ( CLRObject ) GetManagedObject ( ob ) ! ;
151- co2 = GetManagedObject ( other ) as CLRObject ;
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- }
150+ var co2Inst = GetSecondCompareOperandInstance ( other ) ;
171151
172152 if ( co1 == null || co2Inst == null )
173153 {
@@ -228,6 +208,32 @@ public static NewReference tp_richcompare(BorrowedReference ob, BorrowedReferenc
228208 }
229209 }
230210
211+ private static object GetSecondCompareOperandInstance ( BorrowedReference other )
212+ {
213+ var co2 = GetManagedObject ( other ) as CLRObject ;
214+
215+ object co2Inst = null ;
216+ // The object comparing against is not a managed object. It could still be a Python object
217+ // that can be compared against (e.g. comparing against a Python string)
218+ if ( co2 == null )
219+ {
220+ if ( other != null )
221+ {
222+ using var pyCo2 = new PyObject ( other ) ;
223+ if ( Converter . ToManagedValue ( pyCo2 , typeof ( object ) , out var result , false ) )
224+ {
225+ co2Inst = result ;
226+ }
227+ }
228+ }
229+ else
230+ {
231+ co2Inst = co2 . inst ;
232+ }
233+
234+ return co2Inst ;
235+ }
236+
231237 /// <summary>
232238 /// Standard iteration support for instances of reflected types. This
233239 /// allows natural iteration over objects that either are IEnumerable
0 commit comments