File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -212,6 +212,25 @@ public OperableObject(int num)
212212 return ( a . Num >= b ) ;
213213 }
214214
215+ public static bool operator >= ( OperableObject a , PyObject b )
216+ {
217+ using ( Py . GIL ( ) )
218+ {
219+ // Assuming b is a tuple, take the first element.
220+ int bNum = ( int ) b . ToArray ( ) [ 0 ] . AsManagedObject ( typeof ( int ) ) ;
221+ return a . Num >= bNum ;
222+ }
223+ }
224+ public static bool operator <= ( OperableObject a , PyObject b )
225+ {
226+ using ( Py . GIL ( ) )
227+ {
228+ // Assuming b is a tuple, take the first element.
229+ int bNum = ( int ) b . ToArray ( ) [ 0 ] . AsManagedObject ( typeof ( int ) ) ;
230+ return a . Num <= bNum ;
231+ }
232+ }
233+
215234 public static bool operator < ( int a , OperableObject b )
216235 {
217236 return ( a < b . Num ) ;
@@ -391,6 +410,27 @@ public void ForwardOperatorOverloads()
391410" ) ;
392411 }
393412
413+ [ Test ]
414+ public void TupleComparisonOperatorOverloads ( )
415+ {
416+ string name = string . Format ( "{0}.{1}" ,
417+ typeof ( OperableObject ) . DeclaringType . Name ,
418+ typeof ( OperableObject ) . Name ) ;
419+ string module = MethodBase . GetCurrentMethod ( ) . DeclaringType . Namespace ;
420+ PythonEngine . Exec ( $@ "
421+ from { module } import *
422+ cls = { name }
423+ a = cls(2)
424+ b = (1, 2)
425+
426+ c = a >= b
427+ assert c == (a.Num >= b[0])
428+
429+ c = a <= b
430+ assert c == (a.Num <= b[0])
431+ " ) ;
432+ }
433+
394434
395435 [ Test ]
396436 public void ReverseOperatorOverloads ( )
You can’t perform that action at this time.
0 commit comments