Skip to content

Commit 1d373da

Browse files
tiranencukou
authored andcommitted
Fix TypeError in compare_s, test and document error behavior
Pass ``(ldap_res,)`` tuple to string formatting instead of ``ldap_res``. This changes fixes ``TypeError: not all arguments converted during string formatting``. #271 Fixes: #270 Signed-off-by: Christian Heimes <cheimes@redhat.com>
1 parent 95e1d1a commit 1d373da

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

Doc/reference/ldap.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,9 @@ and wait for and return with the server's result, or with
699699
and the value *value*. The synchronous forms returns ``True`` or ``False``.
700700
The asynchronous forms returns the message ID of the initiated request, and
701701
the result of the asynchronous compare can be obtained using
702-
:py:meth:`result()`.
702+
:py:meth:`result()`. The operation can fail with an exception, e.g.
703+
:py:exc:`ldap.NO_SUCH_OBJECT` when *dn* does not exist or
704+
:py:exc:`ldap.UNDEFINED_TYPE` for an invalid attribute.
703705

704706
Note that the asynchronous technique yields the answer
705707
by raising the exception objects :py:exc:`ldap.COMPARE_TRUE` or

Lib/ldap/ldapobject.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ def compare_ext_s(self,dn,attr,value,serverctrls=None,clientctrls=None):
526526
except ldap.COMPARE_FALSE:
527527
return False
528528
raise ldap.PROTOCOL_ERROR(
529-
'Compare operation returned wrong result: %r' % (ldap_res)
529+
'Compare operation returned wrong result: %r' % (ldap_res,)
530530
)
531531

532532
def compare(self,dn,attr,value):

Tests/t_ldapobject.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,18 @@ def test_compare_s_false(self):
661661
result = l.compare_s('cn=Foo1,%s' % base, 'cn', b'Foo2')
662662
self.assertIs(result, False)
663663

664+
def test_compare_s_notfound(self):
665+
base = self.server.suffix
666+
l = self._ldap_conn
667+
with self.assertRaises(ldap.NO_SUCH_OBJECT):
668+
result = l.compare_s('cn=invalid,%s' % base, 'cn', b'Foo2')
669+
670+
def test_compare_s_invalidattr(self):
671+
base = self.server.suffix
672+
l = self._ldap_conn
673+
with self.assertRaises(ldap.UNDEFINED_TYPE):
674+
result = l.compare_s('cn=Foo1,%s' % base, 'invalidattr', b'invalid')
675+
664676

665677
class Test01_ReconnectLDAPObject(Test00_SimpleLDAPObject):
666678
"""

0 commit comments

Comments
 (0)