Skip to content

Commit 4527dc9

Browse files
committed
Add implementation of PyObject_Compare for Python 3.
1 parent f8db1b8 commit 4527dc9

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

src/runtime/runtime.cs

+37-2
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,11 @@ internal static int AtExit() {
290290
#if (PYTHON32 || PYTHON33 || PYTHON34)
291291
internal static IntPtr PyBytesType;
292292
internal static IntPtr PyNotImplemented;
293-
internal static int Py_EQ = 2;
294-
internal static int Py_NE = 3;
293+
internal const int Py_LT = 0;
294+
internal const int Py_LE = 1;
295+
internal const int Py_EQ = 2;
296+
internal const int Py_NE = 3;
297+
internal const int Py_GT = 4;
295298
internal static IntPtr _PyObject_NextNotImplemented;
296299
#endif
297300

@@ -849,10 +852,42 @@ internal unsafe static extern IntPtr
849852
internal unsafe static extern IntPtr
850853
PyObject_CallObject(IntPtr pointer, IntPtr args);
851854

855+
#if (PYTHON32 || PYTHON33 || PYTHON34)
856+
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
857+
ExactSpelling=true, CharSet=CharSet.Ansi)]
858+
internal unsafe static extern int
859+
PyObject_RichCompareBool(IntPtr value1, IntPtr value2, int opid);
860+
861+
internal static int PyObject_Compare(IntPtr value1, IntPtr value2) {
862+
int res;
863+
res = PyObject_RichCompareBool(value1, value2, Py_LT);
864+
if (-1 == res)
865+
return -1;
866+
else if (1 == res)
867+
return -1;
868+
869+
res = PyObject_RichCompareBool(value1, value2, Py_EQ);
870+
if (-1 == res)
871+
return -1;
872+
else if (1 == res)
873+
return 0;
874+
875+
res = PyObject_RichCompareBool(value1, value2, Py_GT);
876+
if (-1 == res)
877+
return -1;
878+
else if (1 == res)
879+
return 1;
880+
881+
Exceptions.SetError(Exceptions.SystemError, "Error comparing objects");
882+
return -1;
883+
}
884+
#else
852885
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
853886
ExactSpelling=true, CharSet=CharSet.Ansi)]
854887
internal unsafe static extern int
855888
PyObject_Compare(IntPtr value1, IntPtr value2);
889+
#endif
890+
856891

857892
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
858893
ExactSpelling=true, CharSet=CharSet.Ansi)]

0 commit comments

Comments
 (0)