[pypy-commit] pypy default: Fix a corner case for weakref.__ne__()
arigo
pypy.commits at gmail.com
Tue Nov 29 05:40:07 EST 2016
Author: Armin Rigo <arigo at tunes.org>
Branch:
Changeset: r88729:ebe85c51ad48
Date: 2016-11-29 11:39 +0100
http://bitbucket.org/pypy/pypy/changeset/ebe85c51ad48/
Log: Fix a corner case for weakref.__ne__()
diff --git a/pypy/module/_weakref/interp__weakref.py b/pypy/module/_weakref/interp__weakref.py
--- a/pypy/module/_weakref/interp__weakref.py
+++ b/pypy/module/_weakref/interp__weakref.py
@@ -217,7 +217,7 @@
return self.space.w_None
return w_obj
- def descr__eq__(self, space, w_ref2):
+ def compare(self, space, w_ref2, invert):
if not isinstance(w_ref2, W_Weakref):
return space.w_NotImplemented
ref1 = self
@@ -225,11 +225,18 @@
w_obj1 = ref1.dereference()
w_obj2 = ref2.dereference()
if w_obj1 is None or w_obj2 is None:
- return space.is_(ref1, ref2)
- return space.eq(w_obj1, w_obj2)
+ w_res = space.is_(ref1, ref2)
+ else:
+ w_res = space.eq(w_obj1, w_obj2)
+ if invert:
+ w_res = space.not_(w_res)
+ return w_res
+
+ def descr__eq__(self, space, w_ref2):
+ return self.compare(space, w_ref2, invert=False)
def descr__ne__(self, space, w_ref2):
- return space.not_(space.eq(self, w_ref2))
+ return self.compare(space, w_ref2, invert=True)
def getlifeline(space, w_obj):
lifeline = w_obj.getweakref()
diff --git a/pypy/module/_weakref/test/test_weakref.py b/pypy/module/_weakref/test/test_weakref.py
--- a/pypy/module/_weakref/test/test_weakref.py
+++ b/pypy/module/_weakref/test/test_weakref.py
@@ -150,6 +150,14 @@
assert not (ref1 == [])
assert ref1 != []
+ def test_ne(self):
+ import _weakref
+ class X(object):
+ pass
+ ref1 = _weakref.ref(X())
+ assert ref1.__eq__(X()) is NotImplemented
+ assert ref1.__ne__(X()) is NotImplemented
+
def test_getweakrefs(self):
import _weakref, gc
class A(object):
More information about the pypy-commit
mailing list