diff --git a/CHANGES b/CHANGES index 7a5e102..8f17c2a 100644 --- a/CHANGES +++ b/CHANGES @@ -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 diff --git a/Doc/reference/ldap.rst b/Doc/reference/ldap.rst index a3ee63c..601824b 100644 --- a/Doc/reference/ldap.rst +++ b/Doc/reference/ldap.rst @@ -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 diff --git a/Lib/ldap/ldapobject.py b/Lib/ldap/ldapobject.py index 36bf034..8fa71c3 100644 --- a/Lib/ldap/ldapobject.py +++ b/Lib/ldap/ldapobject.py @@ -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. @@ -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) ) diff --git a/Tests/t_ldapobject.py b/Tests/t_ldapobject.py index 33c2c5a..7686826 100644 --- a/Tests/t_ldapobject.py +++ b/Tests/t_ldapobject.py @@ -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): """