Skip to content

Commit eaec666

Browse files
gfmcknightslozier
authored andcommitted
Make weakref proxies delegate comparison operators (#710)
* Make weakref proxies delegate comparison operators Add slots for all comparison operators which delegate to the object being proxied. * Remove EqualsWorker()
1 parent 8c309aa commit eaec666

3 files changed

Lines changed: 24 additions & 46 deletions

File tree

Src/IronPython.Modules/_weakref.Generated.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ public sealed partial class weakproxy {
5757

5858
#endregion
5959

60-
//[SlotField] public static PythonTypeSlot __cmp__ = new SlotWrapper(Symbols.Cmp, ProxyType);
60+
[SlotField] public static PythonTypeSlot __eq__ = new SlotWrapper("__eq__", ProxyType);
61+
[SlotField] public static PythonTypeSlot __ne__ = new SlotWrapper("__ne__", ProxyType);
62+
[SlotField] public static PythonTypeSlot __lt__ = new SlotWrapper("__lt__", ProxyType);
63+
[SlotField] public static PythonTypeSlot __gt__ = new SlotWrapper("__gt__", ProxyType);
64+
[SlotField] public static PythonTypeSlot __le__ = new SlotWrapper("__le__", ProxyType);
65+
[SlotField] public static PythonTypeSlot __ge__ = new SlotWrapper("__ge__", ProxyType);
6166
[SlotField] public static PythonTypeSlot __divmod__ = new SlotWrapper("__divmod__", ProxyType);
6267
[SlotField] public static PythonTypeSlot __float__ = new SlotWrapper("__float__", ProxyType);
6368
[SlotField] public static PythonTypeSlot __index__ = new SlotWrapper("__index__", ProxyType);
@@ -126,7 +131,12 @@ public sealed partial class weakcallableproxy {
126131

127132
#endregion
128133

129-
//[SlotField] public static PythonTypeSlot __cmp__ = new SlotWrapper(Symbols.Cmp, CallableProxyType);
134+
[SlotField] public static PythonTypeSlot __eq__ = new SlotWrapper("__eq__", CallableProxyType);
135+
[SlotField] public static PythonTypeSlot __ne__ = new SlotWrapper("__ne__", CallableProxyType);
136+
[SlotField] public static PythonTypeSlot __lt__ = new SlotWrapper("__lt__", CallableProxyType);
137+
[SlotField] public static PythonTypeSlot __gt__ = new SlotWrapper("__gt__", CallableProxyType);
138+
[SlotField] public static PythonTypeSlot __le__ = new SlotWrapper("__le__", CallableProxyType);
139+
[SlotField] public static PythonTypeSlot __ge__ = new SlotWrapper("__ge__", CallableProxyType);
130140
[SlotField] public static PythonTypeSlot __divmod__ = new SlotWrapper("__divmod__", CallableProxyType);
131141
[SlotField] public static PythonTypeSlot __float__ = new SlotWrapper("__float__", CallableProxyType);
132142
[SlotField] public static PythonTypeSlot __index__ = new SlotWrapper("__index__", CallableProxyType);

Src/IronPython.Modules/_weakref.cs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -446,27 +446,6 @@ object IProxyObject.Target {
446446

447447
public const object __hash__ = null;
448448

449-
private bool EqualsWorker(weakproxy other) {
450-
return PythonOps.EqualRetBool(_context, GetObject(), other.GetObject());
451-
}
452-
453-
/// <summary>
454-
/// Special equality function because IStructuralEquatable.Equals is not allowed to throw.
455-
/// </summary>
456-
[return: MaybeNotImplemented]
457-
public object __eq__(object other) {
458-
if (!(other is weakproxy)) return NotImplementedType.Value;
459-
460-
return ScriptingRuntimeHelpers.BooleanToObject(EqualsWorker((weakproxy)other));
461-
}
462-
463-
[return: MaybeNotImplemented]
464-
public object __ne__(object other) {
465-
if (!(other is weakproxy)) return NotImplementedType.Value;
466-
467-
return ScriptingRuntimeHelpers.BooleanToObject(!EqualsWorker((weakproxy)other));
468-
}
469-
470449
int IStructuralEquatable.GetHashCode(IEqualityComparer comparer) {
471450
object obj;
472451
if (TryGetObject(out obj)) {
@@ -705,20 +684,6 @@ object IProxyObject.Target {
705684

706685
public const object __hash__ = null;
707686

708-
/// <summary>
709-
/// Special equality function because IStructuralEquatable.Equals is not allowed to throw.
710-
/// </summary>
711-
public bool __eq__(object other) {
712-
weakcallableproxy wrp = other as weakcallableproxy;
713-
if (wrp != null) return GetObject().Equals(wrp.GetObject());
714-
715-
return PythonOps.EqualRetBool(_context, GetObject(), other);
716-
}
717-
718-
public bool __ne__(object other) {
719-
return !__eq__(other);
720-
}
721-
722687
int IStructuralEquatable.GetHashCode(IEqualityComparer comparer) {
723688
object obj;
724689
if (TryGetObject(out obj)) {

Tests/modules/misc/test__weakref.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,10 @@ def __eq__(self, *args, **kwargs): return True
137137
x, y = helper_func()
138138
gc.collect()
139139

140-
self.assertTrue(not x==3)
140+
self.assertRaises(ReferenceError, lambda: x==3)
141141
self.assertRaises(ReferenceError, lambda: x==y)
142142

143-
# https://github.com/IronLanguages/ironpython3/issues/697
144-
@unittest.expectedFailure
145-
def test_equals(self):
143+
def test_comparison(self):
146144
global called
147145
class C:
148146
for method, op in [('__eq__', '=='), ('__gt__', '>'), ('__lt__', '<'), ('__ge__', '>='), ('__le__', '<='), ('__ne__', '!=')]:
@@ -153,10 +151,15 @@ def %s(self, *args, **kwargs):
153151
return True
154152
""" % (method, op))
155153

156-
a = C()
157-
x = _weakref.proxy(a)
158-
for op in ('==', '>', '<', '>=', '<=', '!='):
159-
self.assertEqual(eval('a ' + op + ' 3'), True); self.assertEqual(called, op); called = None
160-
self.assertEqual(eval('x ' + op + ' 3'), True); self.assertEqual(called, op); called = None
154+
class D(C):
155+
def __call__(self):
156+
pass
157+
158+
A = [ C(), D() ]
159+
X = [ _weakref.proxy(a) for a in A ]
160+
for (a, x) in zip(A, X):
161+
for op in ('==', '>', '<', '>=', '<=', '!='):
162+
self.assertEqual(eval('a ' + op + ' 3'), True); self.assertEqual(called, op); called = None
163+
self.assertEqual(eval('x ' + op + ' 3'), True); self.assertEqual(called, op); called = None
161164

162165
run_test(__name__)

0 commit comments

Comments
 (0)