@@ -69,44 +69,105 @@ public virtual IntPtr type_subscript(IntPtr idx)
6969 //====================================================================
7070#if ( PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 )
7171 public static IntPtr tp_richcompare ( IntPtr ob , IntPtr other , int op ) {
72- if ( op != Runtime . Py_EQ && op != Runtime . Py_NE )
72+ CLRObject co1 ;
73+ CLRObject co2 ;
74+ switch ( op )
7375 {
74- Runtime . XIncref ( Runtime . PyNotImplemented ) ;
75- return Runtime . PyNotImplemented ;
76+ case Runtime . Py_EQ :
77+ case Runtime . Py_NE :
78+ IntPtr pytrue = Runtime . PyTrue ;
79+ IntPtr pyfalse = Runtime . PyFalse ;
80+
81+ // swap true and false for NE
82+ if ( op != Runtime . Py_EQ )
83+ {
84+ pytrue = Runtime . PyFalse ;
85+ pyfalse = Runtime . PyTrue ;
86+ }
87+
88+ if ( ob == other )
89+ {
90+ Runtime . XIncref ( pytrue ) ;
91+ return pytrue ;
92+ }
93+
94+ co1 = GetManagedObject ( ob ) as CLRObject ;
95+ co2 = GetManagedObject ( other ) as CLRObject ;
96+ if ( null == co2 )
97+ {
98+ Runtime . XIncref ( pyfalse ) ;
99+ return pyfalse ;
100+ }
101+
102+ Object o1 = co1 . inst ;
103+ Object o2 = co2 . inst ;
104+
105+ if ( Object . Equals ( o1 , o2 ) )
106+ {
107+ Runtime . XIncref ( pytrue ) ;
108+ return pytrue ;
109+ }
110+
111+ Runtime . XIncref ( pyfalse ) ;
112+ return pyfalse ;
113+ case Runtime . Py_LT :
114+ case Runtime . Py_LE :
115+ case Runtime . Py_GT :
116+ case Runtime . Py_GE :
117+ co1 = GetManagedObject ( ob ) as CLRObject ;
118+ co2 = GetManagedObject ( other ) as CLRObject ;
119+ if ( co1 == null || co2 == null )
120+ return Exceptions . RaiseTypeError ( "Cannot get managed object" ) ;
121+ var co1Comp = co1 . inst as IComparable ;
122+ if ( co1Comp == null )
123+ return Exceptions . RaiseTypeError ( "Cannot convert object of type " + co1 . GetType ( ) + " to IComparable" ) ;
124+ try
125+ {
126+ var cmp = co1Comp . CompareTo ( co2 . inst ) ;
127+
128+ IntPtr pyCmp ;
129+ if ( cmp < 0 )
130+ {
131+ if ( op == Runtime . Py_LT || op == Runtime . Py_LE )
132+ {
133+ pyCmp = Runtime . PyTrue ;
134+ }
135+ else
136+ {
137+ pyCmp = Runtime . PyFalse ;
138+ }
139+ }
140+ else if ( cmp == 0 )
141+ {
142+ if ( op == Runtime . Py_LE || op == Runtime . Py_GE )
143+ {
144+ pyCmp = Runtime . PyTrue ;
145+ }
146+ else
147+ {
148+ pyCmp = Runtime . PyFalse ;
149+ }
150+ }
151+ else
152+ {
153+ if ( op == Runtime . Py_GE || op == Runtime . Py_GT ) {
154+ pyCmp = Runtime . PyTrue ;
155+ }
156+ else {
157+ pyCmp = Runtime . PyFalse ;
158+ }
159+ }
160+ Runtime . XIncref ( pyCmp ) ;
161+ return pyCmp ;
162+ }
163+ catch ( ArgumentException e )
164+ {
165+ return Exceptions . RaiseTypeError ( e . Message ) ;
166+ }
167+ default :
168+ Runtime . XIncref ( Runtime . PyNotImplemented ) ;
169+ return Runtime . PyNotImplemented ;
76170 }
77-
78- IntPtr pytrue = Runtime . PyTrue ;
79- IntPtr pyfalse = Runtime . PyFalse ;
80-
81- // swap true and false for NE
82- if ( op != Runtime . Py_EQ )
83- {
84- pytrue = Runtime . PyFalse ;
85- pyfalse = Runtime . PyTrue ;
86- }
87-
88- if ( ob == other ) {
89- Runtime . XIncref ( pytrue ) ;
90- return pytrue ;
91- }
92-
93- CLRObject co1 = GetManagedObject ( ob ) as CLRObject ;
94- CLRObject co2 = GetManagedObject ( other ) as CLRObject ;
95- if ( null == co2 ) {
96- Runtime . XIncref ( pyfalse ) ;
97- return pyfalse ;
98- }
99-
100- Object o1 = co1 . inst ;
101- Object o2 = co2 . inst ;
102-
103- if ( Object . Equals ( o1 , o2 ) ) {
104- Runtime . XIncref ( pytrue ) ;
105- return pytrue ;
106- }
107-
108- Runtime . XIncref ( pyfalse ) ;
109- return pyfalse ;
110171 }
111172#else
112173 public static int tp_compare ( IntPtr ob , IntPtr other )
0 commit comments