Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Fix comparison for classes implementing __eq__. #51

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/zope/security/_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ proxy_richcompare(SecurityProxy* self, PyObject* other, int op)
{
PyObject *result = NULL;

if (Proxy_Check(other)) {
other = ((SecurityProxy*)(other))->proxy.proxy_object;
}

result = PyObject_RichCompare(self->proxy.proxy_object, other, op);
if (result == Py_True || result == Py_False)
return result;
Expand Down
12 changes: 6 additions & 6 deletions src/zope/security/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,32 +177,32 @@ def __delattr__(self, name):
def __lt__(self, other):
# no check
wrapped = super().__getattribute__('_wrapped')
return wrapped < other
return wrapped < getObjectPy(other)

def __le__(self, other):
# no check
wrapped = super().__getattribute__('_wrapped')
return wrapped <= other
return wrapped <= getObjectPy(other)

def __eq__(self, other):
# no check
wrapped = super().__getattribute__('_wrapped')
return wrapped == other
return wrapped == getObjectPy(other)

def __ne__(self, other):
# no check
wrapped = super().__getattribute__('_wrapped')
return wrapped != other
return wrapped != getObjectPy(other)

def __ge__(self, other):
# no check
wrapped = super().__getattribute__('_wrapped')
return wrapped >= other
return wrapped >= getObjectPy(other)

def __gt__(self, other):
# no check
wrapped = super().__getattribute__('_wrapped')
return wrapped > other
return wrapped > getObjectPy(other)

def __hash__(self):
# no check
Expand Down
9 changes: 8 additions & 1 deletion src/zope/security/tests/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1638,9 +1638,16 @@ def testCallOK(self):
def testCallFail(self):
self.shouldFail(self.p, None)

def testRichCompareOK(self):
def testRichCompareProxyWithObjectOK(self):
self.assertTrue(self.p == self.x)

def testRichCompareObjectWithProxyOK(self):
# Proxy does not get unwrapped as `Something` class defines `__eq__`.
self.assertFalse(self.x == self.p)

def testRichCompareWithProxyWithProxyOK(self):
self.assertTrue(self.p == self.p)

# def testRichCompareFail(self):
# self.shouldFail(lambda: self.p == self.x)

Expand Down