Skip to content

Commit

Permalink
Return a bool in .compare_s() and .compare_ext_s()
Browse files Browse the repository at this point in the history
Previously, LDAPObject.compare_s() and LDAPObject.compare_ext_s()
returned 1 for true and 0 for false. As the return value is intended for
a bool context, return a bool instead.
  • Loading branch information
Jon Dufresne authored and Petr Viktorin committed May 25, 2018
1 parent 1eb07ff commit f3ff4a3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
7 changes: 7 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
----------------------------------------------------------------
UNRELEASED

Lib/
* LDAPObject.compare_s() and LDAPObject.compare_ext_s() now return a bool
instead of 1 or 0.

----------------------------------------------------------------
Released 3.0.0 2018-03-12

Expand Down
13 changes: 6 additions & 7 deletions Doc/reference/ldap.rst
Original file line number Diff line number Diff line change
Expand Up @@ -704,17 +704,16 @@ and wait for and return with the server's result, or with

.. py:method:: LDAPObject.compare(dn, attr, value) -> int
.. py:method:: LDAPObject.compare_s(dn, attr, value) -> tuple
.. py:method:: LDAPObject.compare_s(dn, attr, value) -> bool
.. py:method:: LDAPObject.compare_ext(dn, attr, value [, serverctrls=None [, clientctrls=None]]) -> int
.. py:method:: LDAPObject.compare_ext_s(dn, attr, value [, serverctrls=None [, clientctrls=None]]) -> tuple
.. py:method:: LDAPObject.compare_ext_s(dn, attr, value [, serverctrls=None [, clientctrls=None]]) -> bool
Perform an LDAP comparison between the attribute named *attr* of
entry *dn*, and the value *value*. The synchronous forms
returns :py:const:`0` for false, or :py:const:`1` for true.
The asynchronous forms returns the message ID of the initiated request,
and the result of the asynchronous compare can be obtained using
Perform an LDAP comparison between the attribute named *attr* of entry *dn*,
and the value *value*. The synchronous forms returns ``True`` or ``False``.
The asynchronous forms returns the message ID of the initiated request, and
the result of the asynchronous compare can be obtained using
:py:meth:`result()`.

Note that the asynchronous technique yields the answer
Expand Down
18 changes: 9 additions & 9 deletions Lib/ldap/ldapobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,14 +495,14 @@ def sasl_bind_s(self,dn,mechanism,cred,serverctrls=None,clientctrls=None):
def compare_ext(self,dn,attr,value,serverctrls=None,clientctrls=None):
"""
compare_ext(dn, attr, value [,serverctrls=None[,clientctrls=None]]) -> int
compare_ext_s(dn, attr, value [,serverctrls=None[,clientctrls=None]]) -> int
compare_ext_s(dn, attr, value [,serverctrls=None[,clientctrls=None]]) -> bool
compare(dn, attr, value) -> int
compare_s(dn, attr, value) -> int
Perform an LDAP comparison between the attribute named attr of
entry dn, and the value value. The synchronous form returns 0
for false, or 1 for true. The asynchronous form returns the
message id of the initiates request, and the result of the
asynchronous compare can be obtained using result().
compare_s(dn, attr, value) -> bool
Perform an LDAP comparison between the attribute named attr of entry
dn, and the value value. The synchronous form returns True or False.
The asynchronous form returns the message id of the initiates request,
and the result of the asynchronous compare can be obtained using
result().
Note that this latter technique yields the answer by raising
the exception objects COMPARE_TRUE or COMPARE_FALSE.
Expand All @@ -520,9 +520,9 @@ def compare_ext_s(self,dn,attr,value,serverctrls=None,clientctrls=None):
try:
ldap_res = self.result3(msgid,all=1,timeout=self.timeout)
except ldap.COMPARE_TRUE:
return 1
return True
except ldap.COMPARE_FALSE:
return 0
return False
raise ldap.PROTOCOL_ERROR(
'Compare operation returned wrong result: %r' % (ldap_res)
)
Expand Down
12 changes: 12 additions & 0 deletions Tests/t_ldapobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,18 @@ def test_dse_bytes(self):
[self.server.suffix.encode('utf-8')]
)

def test_compare_s_true(self):
base = self.server.suffix
l = self._ldap_conn
result = l.compare_s('cn=Foo1,%s' % base, 'cn', 'Foo1')
self.assertIs(result, True)

def test_compare_s_false(self):
base = self.server.suffix
l = self._ldap_conn
result = l.compare_s('cn=Foo1,%s' % base, 'cn', 'Foo2')
self.assertIs(result, False)


class Test01_ReconnectLDAPObject(Test00_SimpleLDAPObject):
"""
Expand Down

0 comments on commit f3ff4a3

Please sign in to comment.