Skip to content

Commit f3ff4a3

Browse files
jdufresneencukou
authored andcommitted
Return a bool in .compare_s() and .compare_ext_s()
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.
1 parent 1eb07ff commit f3ff4a3

File tree

4 files changed

+34
-16
lines changed

4 files changed

+34
-16
lines changed

CHANGES

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
----------------------------------------------------------------
2+
UNRELEASED
3+
4+
Lib/
5+
* LDAPObject.compare_s() and LDAPObject.compare_ext_s() now return a bool
6+
instead of 1 or 0.
7+
18
----------------------------------------------------------------
29
Released 3.0.0 2018-03-12
310

Doc/reference/ldap.rst

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -704,17 +704,16 @@ and wait for and return with the server's result, or with
704704

705705
.. py:method:: LDAPObject.compare(dn, attr, value) -> int
706706
707-
.. py:method:: LDAPObject.compare_s(dn, attr, value) -> tuple
707+
.. py:method:: LDAPObject.compare_s(dn, attr, value) -> bool
708708
709709
.. py:method:: LDAPObject.compare_ext(dn, attr, value [, serverctrls=None [, clientctrls=None]]) -> int
710710
711-
.. py:method:: LDAPObject.compare_ext_s(dn, attr, value [, serverctrls=None [, clientctrls=None]]) -> tuple
711+
.. py:method:: LDAPObject.compare_ext_s(dn, attr, value [, serverctrls=None [, clientctrls=None]]) -> bool
712712
713-
Perform an LDAP comparison between the attribute named *attr* of
714-
entry *dn*, and the value *value*. The synchronous forms
715-
returns :py:const:`0` for false, or :py:const:`1` for true.
716-
The asynchronous forms returns the message ID of the initiated request,
717-
and the result of the asynchronous compare can be obtained using
713+
Perform an LDAP comparison between the attribute named *attr* of entry *dn*,
714+
and the value *value*. The synchronous forms returns ``True`` or ``False``.
715+
The asynchronous forms returns the message ID of the initiated request, and
716+
the result of the asynchronous compare can be obtained using
718717
:py:meth:`result()`.
719718

720719
Note that the asynchronous technique yields the answer

Lib/ldap/ldapobject.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -495,14 +495,14 @@ def sasl_bind_s(self,dn,mechanism,cred,serverctrls=None,clientctrls=None):
495495
def compare_ext(self,dn,attr,value,serverctrls=None,clientctrls=None):
496496
"""
497497
compare_ext(dn, attr, value [,serverctrls=None[,clientctrls=None]]) -> int
498-
compare_ext_s(dn, attr, value [,serverctrls=None[,clientctrls=None]]) -> int
498+
compare_ext_s(dn, attr, value [,serverctrls=None[,clientctrls=None]]) -> bool
499499
compare(dn, attr, value) -> int
500-
compare_s(dn, attr, value) -> int
501-
Perform an LDAP comparison between the attribute named attr of
502-
entry dn, and the value value. The synchronous form returns 0
503-
for false, or 1 for true. The asynchronous form returns the
504-
message id of the initiates request, and the result of the
505-
asynchronous compare can be obtained using result().
500+
compare_s(dn, attr, value) -> bool
501+
Perform an LDAP comparison between the attribute named attr of entry
502+
dn, and the value value. The synchronous form returns True or False.
503+
The asynchronous form returns the message id of the initiates request,
504+
and the result of the asynchronous compare can be obtained using
505+
result().
506506
507507
Note that this latter technique yields the answer by raising
508508
the exception objects COMPARE_TRUE or COMPARE_FALSE.
@@ -520,9 +520,9 @@ def compare_ext_s(self,dn,attr,value,serverctrls=None,clientctrls=None):
520520
try:
521521
ldap_res = self.result3(msgid,all=1,timeout=self.timeout)
522522
except ldap.COMPARE_TRUE:
523-
return 1
523+
return True
524524
except ldap.COMPARE_FALSE:
525-
return 0
525+
return False
526526
raise ldap.PROTOCOL_ERROR(
527527
'Compare operation returned wrong result: %r' % (ldap_res)
528528
)

Tests/t_ldapobject.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,18 @@ def test_dse_bytes(self):
646646
[self.server.suffix.encode('utf-8')]
647647
)
648648

649+
def test_compare_s_true(self):
650+
base = self.server.suffix
651+
l = self._ldap_conn
652+
result = l.compare_s('cn=Foo1,%s' % base, 'cn', 'Foo1')
653+
self.assertIs(result, True)
654+
655+
def test_compare_s_false(self):
656+
base = self.server.suffix
657+
l = self._ldap_conn
658+
result = l.compare_s('cn=Foo1,%s' % base, 'cn', 'Foo2')
659+
self.assertIs(result, False)
660+
649661

650662
class Test01_ReconnectLDAPObject(Test00_SimpleLDAPObject):
651663
"""

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy