FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Py3 comparison operators by ArvidJB · Pull Request #291 · pythonnet/pythonnet · GitHub

133 changes: 97 additions & 36 deletions src/runtime/classbase.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -69,44 +69,105 @@ public virtual IntPtr type_subscript(IntPtr idx)
//====================================================================
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35)
public static IntPtr tp_richcompare(IntPtr ob, IntPtr other, int op) {
if (op != Runtime.Py_EQ && op != Runtime.Py_NE)
CLRObject co1;
CLRObject co2;
switch (op)
{
Runtime.XIncref(Runtime.PyNotImplemented);
return Runtime.PyNotImplemented;
case Runtime.Py_EQ:
case Runtime.Py_NE:
IntPtr pytrue = Runtime.PyTrue;
IntPtr pyfalse = Runtime.PyFalse;

// swap true and false for NE
if (op != Runtime.Py_EQ)
{
pytrue = Runtime.PyFalse;
pyfalse = Runtime.PyTrue;
}

if (ob == other)
{
Runtime.XIncref(pytrue);
return pytrue;
}

co1 = GetManagedObject(ob) as CLRObject;
co2 = GetManagedObject(other) as CLRObject;
if (null == co2)
{
Runtime.XIncref(pyfalse);
return pyfalse;
}

Object o1 = co1.inst;
Object o2 = co2.inst;

if (Object.Equals(o1, o2))
{
Runtime.XIncref(pytrue);
return pytrue;
}

Runtime.XIncref(pyfalse);
return pyfalse;
case Runtime.Py_LT:
case Runtime.Py_LE:
case Runtime.Py_GT:
case Runtime.Py_GE:
co1 = GetManagedObject(ob) as CLRObject;
co2 = GetManagedObject(other) as CLRObject;
if(co1 == null || co2 == null)
return Exceptions.RaiseTypeError("Cannot get managed object");
var co1Comp = co1.inst as IComparable;
if (co1Comp == null)
return Exceptions.RaiseTypeError("Cannot convert object of type " + co1.GetType() + " to IComparable");
try
{
var cmp = co1Comp.CompareTo(co2.inst);

IntPtr pyCmp;
if (cmp < 0)
{
if (op == Runtime.Py_LT || op == Runtime.Py_LE)
{
pyCmp = Runtime.PyTrue;
}
else
{
pyCmp = Runtime.PyFalse;
}
}
else if (cmp == 0)
{
if (op == Runtime.Py_LE || op == Runtime.Py_GE)
{
pyCmp = Runtime.PyTrue;
}
else
{
pyCmp = Runtime.PyFalse;
}
}
else
{
if (op == Runtime.Py_GE || op == Runtime.Py_GT) {
pyCmp = Runtime.PyTrue;
}
else {
pyCmp = Runtime.PyFalse;
}
}
Runtime.XIncref(pyCmp);
return pyCmp;
}
catch (ArgumentException e)
{
return Exceptions.RaiseTypeError(e.Message);
}
default:
Runtime.XIncref(Runtime.PyNotImplemented);
return Runtime.PyNotImplemented;
}

IntPtr pytrue = Runtime.PyTrue;
IntPtr pyfalse = Runtime.PyFalse;

// swap true and false for NE
if (op != Runtime.Py_EQ)
{
pytrue = Runtime.PyFalse;
pyfalse = Runtime.PyTrue;
}

if (ob == other) {
Runtime.XIncref(pytrue);
return pytrue;
}

CLRObject co1 = GetManagedObject(ob) as CLRObject;
CLRObject co2 = GetManagedObject(other) as CLRObject;
if (null == co2) {
Runtime.XIncref(pyfalse);
return pyfalse;
}

Object o1 = co1.inst;
Object o2 = co2.inst;

if (Object.Equals(o1, o2)) {
Runtime.XIncref(pytrue);
return pytrue;
}

Runtime.XIncref(pyfalse);
return pyfalse;
}
#else
public static int tp_compare(IntPtr ob, IntPtr other)
Expand Down
1 change: 1 addition & 0 deletions src/runtime/runtime.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ internal static int AtExit()
internal const int Py_EQ = 2;
internal const int Py_NE = 3;
internal const int Py_GT = 4;
internal const int Py_GE = 5;
internal static IntPtr _PyObject_NextNotImplemented;
#endif

Expand Down
52 changes: 49 additions & 3 deletions src/tests/test_class.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from System.Collections import Hashtable
from Python.Test import ClassTest
import sys, os, string, unittest, types
import clr
import types
import unittest

import Python.Test as Test
import System
import six
from Python.Test import ClassTest
from System.Collections import Hashtable

if six.PY3:
DictProxyType = type(object.__dict__)
Expand Down Expand Up @@ -198,6 +201,49 @@ def __setitem__(self, key, value):

self.assertTrue(table.Count == 3)

def testComparisons(self):
from System import DateTimeOffset

d1 = DateTimeOffset.Parse("2016-11-14")
d2 = DateTimeOffset.Parse("2016-11-15")

self.assertEqual(d1 == d2, False)
self.assertEqual(d1 != d2, True)

if six.PY3:
self.assertEqual(d1 < d2, True)
self.assertEqual(d1 <= d2, True)
self.assertEqual(d1 >= d2, False)
self.assertEqual(d1 > d2, False)

self.assertEqual(d1 == d1, True)
self.assertEqual(d1 != d1, False)

if six.PY3:
self.assertEqual(d1 < d1, False)
self.assertEqual(d1 <= d1, True)
self.assertEqual(d1 >= d1, True)
self.assertEqual(d1 > d1, False)

self.assertEqual(d2 == d1, False)
self.assertEqual(d2 != d1, True)

if six.PY3:
self.assertEqual(d2 < d1, False)
self.assertEqual(d2 <= d1, False)
self.assertEqual(d2 >= d1, True)
self.assertEqual(d2 > d1, True)

if six.PY3:
self.assertRaises(TypeError, lambda: d1 < None)
self.assertRaises(TypeError, lambda: d1 < System.Guid())

if six.PY3:
# ClassTest does not implement IComparable
c1 = ClassTest()
c2 = ClassTest()
self.assertRaises(TypeError, lambda: c1 < c2)


class ClassicClass:
def kind(self):
Expand Down

Back | FazBrowse Home | New Git URL